forked from vasanthk/react-bits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01.props-in-initial-state.jsx
46 lines (42 loc) · 1.29 KB
/
01.props-in-initial-state.jsx
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
/**
* Props in Initial State
*
* From docs:
* Using props to generate state in getInitialState often leads to duplication of “source of truth”, i.e. where the real data is.
* This is because getInitialState is only invoked when the component is first created.
*
* The danger is that if the props on the component are changed without the component being ‘refreshed’,
* the new prop value will never be displayed because the constructor function (or getInitialState) will never update the current state of the component.
* The initialization of state from props only runs when the component is first created.
*
* @Reference:
* https://medium.com/@justintulk/react-anti-patterns-props-in-initial-state-28687846cc2e
*
*/
// Bad
class SampleComponent extends Component {
// constructor function (or getInitialState)
constructor(props) {
super(props);
this.state = {
flag: false,
inputVal: props.inputValue
};
}
render() {
return <div>{this.state.inputVal && <AnotherComponent/>}</div>
}
}
// Good
class SampleComponent extends Component {
// constructor function (or getInitialState)
constructor(props) {
super(props);
this.state = {
flag: false
};
}
render() {
return <div>{this.props.inputValue && <AnotherComponent/>}</div>
}
}