source: node_modules/react-immutable-pure-component/README.md

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 3.0 KB
Line 
1[![npm version](https://badge.fury.io/js/react-immutable-pure-component.svg)](https://badge.fury.io/js/react-immutable-pure-component)
2
3# ImmutablePureComponent
4
5Unfortunately `React.PureComponent` is not embracing `Immutable.js` to it full
6potential. While `Immutable.js` provides [hash value](https://facebook.github.io/immutable-js/docs/#/ValueObject/hashCode),
7witch allows for fast comparison of two different instances
8`React.PureComonent` is only comparing addresses of those instances.
9
10The `ImmutablePureComponent` uses [is](https://facebook.github.io/immutable-js/docs/#/is) to compare values and
11extends component functionality by introducing:
12* `updateOnProps`
13* `updateOnStates`
14
15With those properties you can specify list of props or states that will be
16checked for changes. If value is `undefined` (default) then all `props` and
17`state` will be checked, otherwise array of keys or paths is expected. The path
18is an `Array` of keys like in the example below. Path values are working for
19any mix of supported collection as long as given key exists, otherwise checked
20value is `undefined`. `Immutable.Collection`, plain Objects, Arrays, es6 Map
21and any collection providing `get` and `has` functionality are all supported.
22
23```
24type UpdateOn<T> = Array<$Keys<T> | any[]>;
25
26export class ImmutablePureComponent<
27 Props,
28 State = void,
29> extends React$Component<Props, State> {
30
31 updateOnProps: UpdateOn<Props>;
32 updateOnStates: UpdateOn<State>;
33}
34
35export default ImmutablePureComponent;
36```
37
38# immutableMemo
39
40With React `16.6.0` we ware introduced to `React.memo` a `React.PureComponent`
41equivalent for functional components. And the same story goes here,
42unfortunately `React.memo` is not fully embracing `Immutable` potential. That
43is where `immutableMemo` steps in. This is wrapper over `React.memo` with
44custom comparison function. `immutableMemo` accepts component as first argument
45and optionally array of property keys or paths the same way as `updateOnProps`
46is working for `ImmutablePureComponent`.
47
48```
49export function immutableMemo<Props>(
50 component: React$ComponentType<Props>,
51 updateOnProps?: UpdateOn<Props>,
52): React$ComponentType<Props>;
53```
54
55### Example
56In this example component will update when value of `me` is change and will
57ignore changes of `data`, `check` or any other property. Component will also
58update on change of first element of `buzz` or change to `type` and will ignore
59changes to the rest of the state.
60
61```js
62class Example extends ImmutablePureComponent {
63 state = {
64 fis: {
65 buzz: Immutable.List([10, 11])
66 ignore: 'this',
67 },
68 type: undefined,
69 };
70
71 updateOnStates = [
72 ['fis', 'buzz', 0],
73 'type',
74 ];
75
76 updateOnProps = [
77 ['data', 'check', 'me'],
78 ];
79
80 render() {...}
81}
82
83let data = Immutable.Map({ check: new Map([['me', true]]) })
84
85ReactDOM.render(<Example data={data} onChange={() => {}}, root);
86```
87
88To check what its all about checkout the interactive example :D
89### [Interactive example](https://codesandbox.io/s/github/Monar/react-immutable-pure-component/tree/master/example).
Note: See TracBrowser for help on using the repository browser.