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 |
|
5 | Unfortunately `React.PureComponent` is not embracing `Immutable.js` to it full potential. So here is my solution to this problem.
|
6 | [npm package](https://www.npmjs.com/package/react-immutable-pure-component) is
|
7 | parsed with babel so feel safe to use it from package repository or just copy
|
8 | it to your project and go from here.
|
9 |
|
10 | [Here](https://monar.github.io/react-immutable-pure-component/) you will find a simple example of a problem it's solving.
|
11 |
|
12 | The `ImmutablePureComponent` extends component functionality by introducing
|
13 | `updateOnProps` and `updateOnStates`. With those properties you can specify
|
14 | list of props or states that will be checked for changes. If value is
|
15 | `undefined` (default) then all `props` and `state` will be checked, otherwise
|
16 | array of strings is expected. This way component can react only to those
|
17 | changes that are important. Useful when passing lambda function like this:
|
18 | `<Component onChange={(e) => doWhatEver(e)}/> `
|
19 |
|
20 | ```js
|
21 |
|
22 | /*
|
23 | Copyright (C) 2017 Piotr Tomasz Monarski.
|
24 | Licensed under the MIT License (MIT), see
|
25 | https://github.com/Monar/react-immutable-pure-component
|
26 | */
|
27 |
|
28 | import React from 'react';
|
29 | import { is } from 'immutable';
|
30 |
|
31 |
|
32 | export class ImmutablePureComponent extends React.Component {
|
33 |
|
34 | shouldComponentUpdate(nextProps, nextState) {
|
35 | const state = this.state || {};
|
36 |
|
37 | return !(this.updateOnProps || Object.keys(nextProps)).every((p) => is(nextProps[p], this.props[p]))
|
38 | || !(this.updateOnStates || Object.keys(nextState || {})).every((s) => is(nextState[s], state[s]));
|
39 | }
|
40 | }
|
41 |
|
42 | export default ImmutablePureComponent;
|
43 | ```
|
44 |
|