1

After doing something like:

ReactDOM.render(<ConfirmForm />, document.getElementById('confirm-form'));

how can I obtain the confirm-form dom element from inside the ConfirmForm element?

Inside the componentDidMount method I can do

ReactDOM.findDOMNode(this).parentElement

but inside the render method I can't use this.

Thanks

Oscar Fanelli
  • 3,337
  • 2
  • 28
  • 40
  • 1
    DOM nodes are unstable inside `render` (they're changing after all), so you shouldn't access them in `render`, but you certainly have access to `this`—otherwise `this.props` wouldn't work. Get the DOM node in `componentDidMount` and save it off somewhere. – Michelle Tilley Oct 10 '15 at 20:32
  • `render` is unreliable for this. You should use the `componentDidUpdate` lifecycle method, see here https://facebook.github.io/react/docs/component-specs.html – kraf Oct 11 '15 at 06:20
  • I was thinking to pass the element id via props on `ReactDOM.render` call.. what do you think about it? – Oscar Fanelli Oct 11 '15 at 08:30
  • 1
    You could simply do `ReactDOM.render(, document.getElementById('confirm-form'));` However, in react terms, I cannot think of any valid reason why you would need to access the parent DOM-element directly. Anything you wish to learn about this component is better accessed in react's virtual DOM. – wintvelt Oct 12 '15 at 21:43

0 Answers0