-
Notifications
You must be signed in to change notification settings - Fork 0
/
hook_effect.js
63 lines (54 loc) · 1.23 KB
/
hook_effect.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
52
53
54
55
56
57
58
59
60
61
62
63
// ! Comparison b/w class components & hook useEffect
// * CLASS
class ClassExample extends React.Component {
constructor(props) {
super(props);
this.state = {
name: 'Nathan'
}
}
componentDidMount() {
console.log(`didMount triggered: Hello I'm ${this.state.name}`);
}
componentDidUpdate(prevProps, prevState) {
console.log(`didUpdate triggered: Hello I'm ${this.state.name}`);
}
render() {
return (
<div>
<p>{`Hello I'm ${this.state.name}`}</p>
<button
onClick={() => {
this.setState({ name: 'Bobolita' })
}}
>
Click & Change Me
</button>
</div>
);
}
}
// * Functional Component
const FunctExample = props => {
const [name, setName] = useState('Nathan');
// ! The second argument is referred as dependecy
useEffect(() => {
console.log(`Hello I'm ${name}`);
// * Executing ComponentWillUnmount -> clean-up
return () => {
console.log('componentWillUnmount effect...');
}
}, [name]);
return (
<div>
<p>{`Hello I'm ${name}`}</p>
<button
onClick={() => {
setName('Bobolita')
}}
>
Click & Change Me
</button>
</div>
)
}