Accessing child instances of a component in React – Part 2

in javascript •  4 years ago 

Original creation date: 19th December 2020
Note: Originally I posted this on my Wordpress Blog (https://1337codersblog.wordpress.com), but I decided to switch to steemit, so I copied it from there.

Accessing child instances of a component in React – Part 2

I have found out, that my solution described in part 1 had a bug. I had written an “onClick” function for one of the buttons of the widgets and noticed, that it worked fine, when it was triggered first time after page load, but failed when triggering it the second time. While my debugging I found out, that somehow the children where reinstantiated without calling the “onInit” method.

It seems, that on the re-render only the “render” method were called, not the constructor of the child component. So the solution was simple: I changed the code so that the “onInit” function is called in the constructor and in the “render” method of the component. So everything works fine now.

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!