Reputation: 3656
I'm using a 3rd-party component in my application. And I want to perform some actions in the parent component when it's child component updates (i.e. fires componentDidUpdate
) but it turns out parent component's componentDidUpdate
doesn't fire when the child updates internally.
Is there anyway to listen from the parent that the child component has updated? Assuming that I can't modify the child component code.
Upvotes: 3
Views: 163
Reputation: 10179
Is there any way to listen from the parent that the child component has updated?
The answer is: there is no way.
However, we absolutely could implement that functionality by leveraging componentDidUpdate()
method of the child component.
We pass down a callback function as a prop from the parent component to the child component. Then whenever the child component has updated, we will invoke that callback function in the componentDidUpdate()
method of the child component. In this way, the parent can listen when the child component has updated.
Upvotes: 3