noamelb / shouldcomponentupdate-children Goto Github PK
View Code? Open in Web Editor NEWReact "Shallow Equal" HOC implementation to optimize shouldComponentUpdate with children / React elements ๐โฐ
License: MIT License
React "Shallow Equal" HOC implementation to optimize shouldComponentUpdate with children / React elements ๐โฐ
License: MIT License
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"]
}
}
}
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
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.
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?
hey!
do you still maintain the library?
please look at this sandbox:
https://codesandbox.io/s/43lzn1nr5x
looks like updating children without updating their father doesn't work.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.