Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ReactJS: how listen that child component updates?

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.

like image 555
Stalinko Avatar asked Apr 11 '26 14:04

Stalinko


1 Answers

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.

like image 121
You Nguyen Avatar answered Apr 14 '26 15:04

You Nguyen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!