-
Notifications
You must be signed in to change notification settings - Fork 0
/
perf7 - defaulting at the props level.js
71 lines (52 loc) · 1.27 KB
/
perf7 - defaulting at the props level.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
64
65
66
67
68
69
70
71
/**
* Defaulting values at the props level of a parent component
*/
/* With Children */
class MyComponentWithChild extends React.Component {
render() {
const { items } = this.props;
return (
<div>
<MyChildComponent
items={items || []}
/>
</div>
)
}
}
class MyChildComponent extends React.PureComponent {
render() {
const { items } = this.props;
return (
<div>
{items.map(item => <div>{item.name}</div>)}
</div>
)
}
}
React.render(<MyComponentWithChild items={items} />);
/** Map state to props version */
class MyComponent extends React.Component {
state = {};
render() {
const { items, myProp } = this.props;
return (
<div>
<span>{myProp.heading}</span>
{items.map(item => {
return (
<div>
{item.name}
</div>
)
})}
</div>
)
}
}
function mapStateToProps(state) {
return {
myProp: state.thing.config || {}
};
}
React.render(<MyComponent />);