Get `props.children` DOM handle: `React.findDOMNode(child)`
See original GitHub issueWhen 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
.
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.
Issue Analytics
- State:
- Created 8 years ago
- Comments:23 (4 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@spicyj Thank you for the info and correction.
Here is a working demo with
React.cloneElement
.React.addons.cloneWithProps
is deprecated: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.