UNPKG

1.74 kBMarkdownView Raw
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 potential. So here is my solution to this problem.
6[npm package](https://www.npmjs.com/package/react-immutable-pure-component) is
7parsed with babel so feel safe to use it from package repository or just copy
8it 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
12The `ImmutablePureComponent` extends component functionality by introducing
13`updateOnProps` and `updateOnStates`. With those properties you can specify
14list 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
16array of strings is expected. This way component can react only to those
17changes 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
28import React from 'react';
29import { is } from 'immutable';
30
31
32export 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
42export default ImmutablePureComponent;
43```
44