Accessing child instances of a component in React – Part 3

in javascript •  4 years ago 

While implementing my Javascript frontend I saw that the Chromium browser warned that the state of the component had changed after rendering. Also in some cases, the component holded an old instance of its child component, because the recreation of the child happened after the render() function had been called. So I came to a more robust solution: Instead of calling the onInit() function within the constructor and render() methods of the child component, add the functions "componentDidMount" and "componentDidUpdate" to it and call onInit() from there. ComponentDidMount() and componentDidUpdate() are called by React after the component and all of its children had been rendered, so it ensures, that onInit() is called by the child when the reference of it is up to date.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!