Skip to content
Advertisement

How to call a method once a component is rendered?

I have a parent component that conditionally renders two components. Once I am done entering data in the first component, I click done and then render the second component. I want the second component to only then parse all the data I entered in the first component.

Im a little confused which lifecycle method I am supposed to be using.

componentdidupdate will not be called unless the state of the second component is updated, correct? And componentdidmount will be called before I have finished entering data into the first component, since it is already inserted into the tree (although not rendered yet conditionally)?

Advertisement

Answer

You can use a the useEffect and set the component as a dependency.

useEffect(() => {
...
}, [yourcomponentStateValue]);
2 People found this is helpful
Advertisement