-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcomponentWillUnmountExample.js
executable file
·51 lines (40 loc) · 1.44 KB
/
componentWillUnmountExample.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import React, { Component } from 'react';
import ReactDOM from 'react-dom';
function ComponentHw() {
return <h1>HELLO WORLD</h1>
}
function ComponentSlogan() {
return <h1>PAKISTAN ZINDABAD</h1>
}
class App extends Component {
state = {
number: 0,
}
componentDidMount() {
//after 3 second this setTimeout will remove <App> from browser DOM
// and render this new <Slogan/> on browser DOM
setTimeout(() => {
ReactDOM.render(<ComponentSlogan />, document.getElementById('root'))
}, 3000)
}
componentWillUnmount() {
// when <App/> component will be unmounted after 3 seconds
//this function will invoke autometically
alert("component unmount event invoked")
}
render() {
return (
//Always remember componentWillUnmount event will be invoke
//only when there is a change at browser DOM
// and in our app browser DOM we have rendered <App/> through
// index.js file. so to change on browser DOM we again need to
// run ReactDOM.render which in this example is componentDidMount
// method will run
//conditional rendering below will not invoke componentWillUnmount() method
// because actually <App/> is rendered and below component is just a child of
// <App/> component
<ComponentHw />
)
}
}
export default App