Since componentWillUnmount is now a legacy lifecycle method in React v17, what would be a good way to notify a parent component when it's child component is unmounted?
My thinking and from previous answers a possible approach would be to pass a function as a callback prop from the parent to the child component, but where in the child component would the callback prop then be called? In one of the lifecycle methods?
Parent component
class App extends Component {
constructor(props) {
super(props);
}
childUnmounted(){
console.log('child unmounted')
}
render() {
return (
<div>
<Child unmounted={this.childUnmounted}/>
</div>
);
}
}
child component
class Child extends Component {
constructor(props) {
super(props);
}
unmounted(){
this.props.childUnmounted()
}
render() {
return (
<div>
<p>Child component<p>
</div>
);
}
}