question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Get `props.children` DOM handle: `React.findDOMNode(child)`

See original GitHub issue

When trying to use React.findDOMNode on a props.children item(ReactElement), an error is thrown.

Uncaught Error: Invariant Violation: Element appears to be neither ReactComponent nor DOMNode

Here is a small demo that shows off the problem.

React.Children.forEach(this.props.children, (child) => {
    console.log(React.findDOMNode(child));        
});

The parent component is mounted console.log('isMounted', this.isMounted()); -> isMounted true.


Related to #1602 and #1373.

Additionally the behavior of didMount and didUpdate handlers are currently undefined in regards to when they fire in relation to their children and therefore refs. For example componentDidUpdate is not guaranteed to fire after the children has fully mounted.

From sebmarkbage, Source

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:23 (4 by maintainers)

github_iconTop GitHub Comments

15reactions
MadLittleModscommented, Jun 29, 2015

@spicyj Thank you for the info and correction.

Here is a working demo with React.cloneElement. React.addons.cloneWithProps is deprecated:

// `render`
React.Children.map(this.props.children, (child) => {
    return React.cloneElement(child, {
        ref: `item-${child.props.key}`
    });
});

// ...

// `componentDidMount` or `componentDidUpdate`
React.Children.forEach(this.props.children, (child) => {        
    console.log(React.findDOMNode(this.refs[`item-${child.props.key}`]));        
});
1reaction
gaearoncommented, Jun 21, 2022

I strongly recommend against doing invasive transformations like this. This kind of code is brittle and difficult to use and understand. Wrap the children in a <div> and get a ref to that div explicitly instead.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting DOM node from React child element - Stack Overflow
findDOMNode method that was introduced in v0.13.0 I am able to get the DOM node of each child component that was passed into...
Read more >
Find Specific Elements from the DOM in React - Pluralsight
findDOMNode () is used to find a specific node from the DOM tree and access its various properties, such as its inner HTML,...
Read more >
ReactDOM – React
findDOMNode is an escape hatch used to access the underlying DOM node. In most cases, use of this escape hatch is discouraged because...
Read more >
[Solved]-Getting DOM node from React child element-Reactjs
You can then access the child components via this.refs[childIdx] , and retrieve their DOM nodes via ReactDOM.findDOMNode(this.refs[childIdx]) .
Read more >
`props.children` DOM handle: `React.findDOMNode(child ... - JSFiddle
Support the development of JSFiddle and get extra features ✌ ; 1. var Example = React.createClass({ ; 2. propTypes: { ;...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found