Coder Social home page Coder Social logo

monar / react-immutable-pure-component Goto Github PK

View Code? Open in Web Editor NEW
32.0 2.0 2.0 2.05 MB

Unfortunately React.PureComponent is not embracing Immutable.js to it full potential. So here is my solution to this problem.

License: MIT License

JavaScript 95.61% HTML 4.39%
react immutable purecomponent

react-immutable-pure-component's Introduction

npm version

ImmutablePureComponent

Unfortunately React.PureComponent is not embracing Immutable.js to it full potential. While Immutable.js provides hash value, witch allows for fast comparison of two different instances React.PureComonent is only comparing addresses of those instances.

The ImmutablePureComponent uses is to compare values and extends component functionality by introducing:

  • updateOnProps
  • updateOnStates

With those properties you can specify list of props or states that will be checked for changes. If value is undefined (default) then all props and state will be checked, otherwise array of keys or paths is expected. The path is an Array of keys like in the example below. Path values are working for any mix of supported collection as long as given key exists, otherwise checked value is undefined. Immutable.Collection, plain Objects, Arrays, es6 Map and any collection providing get and has functionality are all supported.

type UpdateOn<T> = Array<$Keys<T> | any[]>;

export class ImmutablePureComponent<
  Props,
  State = void,
> extends React$Component<Props, State> {

  updateOnProps: UpdateOn<Props>;
  updateOnStates: UpdateOn<State>;
}

export default ImmutablePureComponent;

immutableMemo

With React 16.6.0 we ware introduced to React.memo a React.PureComponent equivalent for functional components. And the same story goes here, unfortunately React.memo is not fully embracing Immutable potential. That is where immutableMemo steps in. This is wrapper over React.memo with custom comparison function. immutableMemo accepts component as first argument and optionally array of property keys or paths the same way as updateOnProps is working for ImmutablePureComponent.

export function immutableMemo<Props>(
  component: React$ComponentType<Props>,
  updateOnProps?: UpdateOn<Props>,
): React$ComponentType<Props>;

Example

In this example component will update when value of me is change and will ignore changes of data, check or any other property. Component will also update on change of first element of buzz or change to type and will ignore changes to the rest of the state.

class Example extends ImmutablePureComponent {
  state = {
    fis: { 
      buzz: Immutable.List([10, 11])
      ignore: 'this',
    },
    type: undefined,
  };

  updateOnStates = [
    ['fis', 'buzz', 0],
    'type',
  ];

  updateOnProps = [
    ['data', 'check', 'me'],
  ];

  render() {...}
}

let data = Immutable.Map({ check: new Map([['me', true]]) }) 

ReactDOM.render(<Example data={data} onChange={() => {}}, root);

To check what its all about checkout the interactive example :D

react-immutable-pure-component's People

Contributors

monar avatar neciu avatar ykzts avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

mousesprite neciu

react-immutable-pure-component's Issues

The very old @types/react in this package causes dependency issues

This package is being installed in my project as an indirect dependency and its very old @types/react version is causing type errors. Is it possible to use ^ notation instead for the package version? Or at least upgrade to a more recent version and publish a new version?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.