forked from vasanthk/react-bits
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03.reselect.jsx
44 lines (40 loc) · 1.21 KB
/
03.reselect.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
/**
* Use Reselect in Redux connect(mapState) -- to avoid frequent re-render.
*
* @Reference:
* https://medium.com/@esamatti/react-js-pure-render-performance-anti-pattern-fb88c101332f#.cz2ypc2ob
*/
// BAD
let App = ({otherData, resolution}) => (
<div>
<DataContainer data={otherData}/>
<ResolutionContainer resolution={resolution}/>
</div>
);
const doubleRes = (size) => ({
width: size.width * 2,
height: size.height * 2
});
App = connect(state => {
return {
otherData: state.otherData,
resolution: doubleRes(state.resolution)
}
})(App);
/**
* In this above case every time otherData in the state changes both DataContainer and ResolutionContainer
* will be rendered even when the resolution in the state does not change.
* This is because the doubleRes function will always return a new resolution object with a new identity.
* If doubleRes is written with Reselect the issue goes away:
* Reselect memoizes the last result of the function and returns it when called until new arguments are passed to it.
*/
// GOOD
import { createSelector } from 'reselect';
const doubleRes = createSelector(
r => r.width,
r => r.height,
(width, height) => ({
width: width * 2,
height: heiht * 2
})
);