Giter Site home page Giter Site logo

noamelb / shouldcomponentupdate-children Goto Github PK

View Code? Open in Web Editor NEW
268.0 10.0 10.0 50 KB

React "Shallow Equal" HOC implementation to optimize shouldComponentUpdate with children / React elements ๐Ÿ‡โžฐ

License: MIT License

JavaScript 100.00%
shouldcomponentupdate hoc react-elements props react

shouldcomponentupdate-children's People

Contributors

noamelb avatar tzook 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  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  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  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

shouldcomponentupdate-children's Issues

TransformError: Couldn't find preset "es2015"

When I import the package and run the app (within Expo), I have the following message

TransformError: index.cjs.js: couldn't find preset "es2015" relative to directory .../node_modules/shouldcomponentupdate-children

I've noticed that you are not using the babel-preset-env instead, submitted PR for this, but it does not solve the problem.
my .babelrc is

{
  "presets": ["babel-preset-expo"],
  "env": {
    "development": {
      "plugins": ["transform-react-jsx-source"]
    }
  }
}

Class constructor Comment cannot be invoked without 'new'

When adding the package and using the HOC on one of the components I get the following Error:

Uncaught TypeError: Class constructor Comment cannot be invoked without 'new' at new ShallowEqualEnhancer (main.bundle.js:77123) at constructClassInstance (main.bundle.js:33250) at updateClassComponent (main.bundle.js:33705) at beginWork (main.bundle.js:34095) at performUnitOfWork (main.bundle.js:36063) at workLoop (main.bundle.js:36172) at HTMLUnknownElement.callCallback (main.bundle.js:24789) at Object.invokeGuardedCallbackDev (main.bundle.js:24828) at invokeGuardedCallback (main.bundle.js:24685) at performWork (main.bundle.js:36290) main.bundle.js:31795

The above error occurred in the <ShallowEqualEnhancedComment> component: in ShallowEqualEnhancedComment (created by CommentList) in div (created by wrapperStyle) in wrapperStyle (created by wrapperStyle) ...

stack includes next.js 4 and react 16

Export non-HOC version?

I know you said in the README that you love HOCs, but I don't see the benefit of adding another level to my component tree when it's not needed. It'd be trivial to add one more little export:

export function shouldComponentUpdate(nextProps, nextState) {
    return !shallowEqualState(this.state, nextState) || !shallowEqualWithoutReactElements(this.props, nextProps);
}

Could you add it please? โค๏ธ

PS: This thing is working like magic. Much less rendering with no ill-effects so far.

Misleading function shallowEqual()

Hi and thank you for this HOC. I also read your article https://medium.com/myheritage-engineering/how-to-greatly-improve-your-react-app-performance-e70f7cbbb5f6
which has been pretty helpful.

I only have one question regarding the shallowEqual function:

export function shallowEqual(thisProps, nextProps, thisState, nextState) {
    return !shallowEqualState(thisState, nextState) || !shallowEqualWithoutReactElements(thisProps, nextProps);
}

Right now, it returns true when the component should update (something has changed), and false otherwise. On the other hand, shallowEqualState and shallowEqualWithoutReactElements both work in the opposite way: they return false when there's a change (something has changed), and true otherwise.

That said, shouldn't shallowEqual use logical AND instead of OR in its body? E.g.:

export function shallowEqual(thisProps, nextProps, thisState, nextState) {
    return shallowEqualState(thisState, nextState) && shallowEqualWithoutReactElements(thisProps, nextProps);
}

This way, if shallowEqual returns true, it means that the props/state are equal and that the component should not update. Whereas, if it returns false, it would mean that something has changed and the component should update.

And then, someone using shallowEqual would have to negate it in order to know that something has changed and therefore a component should update:

            ...
            if (!super.shouldComponentUpdate || super.shouldComponentUpdate(nextProps, nextState)) {
                shouldUpdate = !shallowEqual(this.props, nextProps, this.state, nextState); // If shallowEqual returns `false`, it means that the prev props or state are not equal to the next props or state and we should therefore update...
            }
            ...

What do you think?

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.