I was trying to use the componentWillUnmount
functionality of React and to change the value of a prop from a child component by click event. I think I am willing to use the "lift the state up" and want to use the componentWillUnmount
property of React.
export default class App extends React.Component {
constructor(props){
super(props);
this.state={
show:true
}
}
render() {
const toggleDisplay=()=>{
this.setState({show:!this.state.show});
}
return (
<div className="App">
{this.show&&<ChildrenUnmounting onclickfun={toggleDisplay} />}
</div>
);
}
}
class ChildrenUnmounting extends React.Component {
constructor(props) {
super(props);
alert("constructor is called!");
this.state={
onclickfun:props.onclickfun,
}
alert("constructor is called!");
}
render() {
return (
<>
{alert("render is called!")}
<h1>This content is shown!</h1>
<button onClick={this.state.onclickfun}>
Toggle View
</button>
</>
);
}
componentWillUnmount() {
alert("componentWillUnmount is called!");
}
}