33
44
55Component memoization for React! Based on [ Dan Abramov's tweet] ( https://twitter.com/dan_abramov/status/965378278461755392 ) :)
6- Could change the way you did ` componentWillReceiveProps ` , could remember how your React Tree grow , could make things better.
6+ Could change the way you did ` componentWillReceiveProps ` , could replace ` getDerivedStateFromProps ` , could make things better.
77
88> IE11+, React 15 and React 16.3 compatible.
99
1010[ ![ NPM] ( https://nodei.co/npm/react-memoize.png?downloads=true&stars=true )] ( https://nodei.co/npm/react-memoize/ )
1111
12+ ### Memoize
13+
1214``` js
1315 import Memoize from ' react-memoize' ;
1416
@@ -22,7 +24,7 @@ Could change the way you did `componentWillReceiveProps`, could remember how you
2224 { result => < Display> {result}< / Display> }
2325 < / Memoize>
2426```
25-
27+ There is only __ one prop __ - ` compute ` , all others will be passed inside.
2628Memoize get ` compute ` function, add passes all the other props to it, streaming result to the render prop.
2729
2830If ` pure ` prop is set ReactMemoize wil behave as PureComponent, and not update children when could not.
@@ -58,6 +60,55 @@ This is like Redux without dispatching. State in context, selector aka mapStateT
5860
5961__ See it in action ->__ https://codesandbox.io/s/xjz5y3wzrz 🛠
6062
63+ ## Flow
64+ ` getDerivedStateFromProps ` gives you ability to from a new state from props, while ` componentDidUpdate ` enables you to react
65+ to the state changes.
66+
67+ __ MemoizedFlow__ is ` getDerivedStateFromState ` . Following example react to the state changes, allowing
68+ to change ordering of rows and applies a pagination.
69+
70+ "The Flow" is safe and performant way to form something from something, and rebuilt then the time calls.
71+ ``` js
72+ import {MemoizedFlow } from ' react-memoize' ;
73+
74+ class SortablePageableTable extends Component {
75+ state = {
76+ page: 0 ,
77+ perPage: 10 ,
78+ filter : I => I
79+ };
80+
81+ onSortChange = (order ) => this .setState (order)
82+ onPageChange = page => this .setState (page);
83+
84+ render () {
85+ return (
86+ < MemoizedFlow
87+ input= {{... this .props , ... this .state }}
88+ flow= {[
89+ // will react on rows or filter change
90+ ({rows, filter}) => ({rows: list .filter (filter)}),
91+ // will react on rows(from step1) change or order
92+ ({rows, order}) => ({rows: list .slice ().sort (order)}), // !! clone array before sort
93+ // will react on rows and pagination changes
94+ ({rows, page, perPage}) => ({rows: list .slice (page* perPage, (page+ 1 )* perPage)}),
95+ // will react on something else, not related
96+ ({rain, bows}) => ({rainbows: rain+ bows, rain: null , bows: null })
97+ ]}
98+ >
99+ {output => < Table {... output} onSortChange= {this .onSortChange } onPageChange= {this .onPageChange }/ > }
100+ < / MemoizedFlow>
101+ }
102+ }
103+
104+ < SortablePageableTable rows = {tableRows} / >
105+ ` ` `
106+ First step is getting ` input` , and each following is reading from a value provided before, spreading
107+ own result over it. Until the last step will be reached, and output will be provided to render prop.
108+
109+ Each step is memoized, as usual, and will always reuse value from the steps before.
110+
111+
61112## About
62113
63114React-memoize uses [memoize-state](https://github.com/theKashey/memoize-state) underneath to perform __MobX__ like memozation
0 commit comments