[d24f17c] | 1 | [](https://badge.fury.io/js/react-immutable-pure-component)
|
---|
| 2 |
|
---|
| 3 | # ImmutablePureComponent
|
---|
| 4 |
|
---|
| 5 | Unfortunately `React.PureComponent` is not embracing `Immutable.js` to it full
|
---|
| 6 | potential. While `Immutable.js` provides [hash value](https://facebook.github.io/immutable-js/docs/#/ValueObject/hashCode),
|
---|
| 7 | witch allows for fast comparison of two different instances
|
---|
| 8 | `React.PureComonent` is only comparing addresses of those instances.
|
---|
| 9 |
|
---|
| 10 | The `ImmutablePureComponent` uses [is](https://facebook.github.io/immutable-js/docs/#/is) to compare values and
|
---|
| 11 | extends component functionality by introducing:
|
---|
| 12 | * `updateOnProps`
|
---|
| 13 | * `updateOnStates`
|
---|
| 14 |
|
---|
| 15 | With those properties you can specify list of props or states that will be
|
---|
| 16 | checked 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
|
---|
| 18 | is an `Array` of keys like in the example below. Path values are working for
|
---|
| 19 | any mix of supported collection as long as given key exists, otherwise checked
|
---|
| 20 | value is `undefined`. `Immutable.Collection`, plain Objects, Arrays, es6 Map
|
---|
| 21 | and any collection providing `get` and `has` functionality are all supported.
|
---|
| 22 |
|
---|
| 23 | ```
|
---|
| 24 | type UpdateOn<T> = Array<$Keys<T> | any[]>;
|
---|
| 25 |
|
---|
| 26 | export class ImmutablePureComponent<
|
---|
| 27 | Props,
|
---|
| 28 | State = void,
|
---|
| 29 | > extends React$Component<Props, State> {
|
---|
| 30 |
|
---|
| 31 | updateOnProps: UpdateOn<Props>;
|
---|
| 32 | updateOnStates: UpdateOn<State>;
|
---|
| 33 | }
|
---|
| 34 |
|
---|
| 35 | export default ImmutablePureComponent;
|
---|
| 36 | ```
|
---|
| 37 |
|
---|
| 38 | # immutableMemo
|
---|
| 39 |
|
---|
| 40 | With React `16.6.0` we ware introduced to `React.memo` a `React.PureComponent`
|
---|
| 41 | equivalent for functional components. And the same story goes here,
|
---|
| 42 | unfortunately `React.memo` is not fully embracing `Immutable` potential. That
|
---|
| 43 | is where `immutableMemo` steps in. This is wrapper over `React.memo` with
|
---|
| 44 | custom comparison function. `immutableMemo` accepts component as first argument
|
---|
| 45 | and optionally array of property keys or paths the same way as `updateOnProps`
|
---|
| 46 | is working for `ImmutablePureComponent`.
|
---|
| 47 |
|
---|
| 48 | ```
|
---|
| 49 | export function immutableMemo<Props>(
|
---|
| 50 | component: React$ComponentType<Props>,
|
---|
| 51 | updateOnProps?: UpdateOn<Props>,
|
---|
| 52 | ): React$ComponentType<Props>;
|
---|
| 53 | ```
|
---|
| 54 |
|
---|
| 55 | ### Example
|
---|
| 56 | In this example component will update when value of `me` is change and will
|
---|
| 57 | ignore changes of `data`, `check` or any other property. Component will also
|
---|
| 58 | update on change of first element of `buzz` or change to `type` and will ignore
|
---|
| 59 | changes to the rest of the state.
|
---|
| 60 |
|
---|
| 61 | ```js
|
---|
| 62 | class 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 |
|
---|
| 83 | let data = Immutable.Map({ check: new Map([['me', true]]) })
|
---|
| 84 |
|
---|
| 85 | ReactDOM.render(<Example data={data} onChange={() => {}}, root);
|
---|
| 86 | ```
|
---|
| 87 |
|
---|
| 88 | To 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).
|
---|