All Projects β†’ NoamELB β†’ Shouldcomponentupdate Children

NoamELB / Shouldcomponentupdate Children

Licence: mit
React "Shallow Equal" HOC implementation to optimize shouldComponentUpdate with children / React elements πŸ‡βž°

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Shouldcomponentupdate Children

React Bps
πŸ”± Create breakpoints to your component props
Stars: ✭ 64 (-76.47%)
Mutual labels:  hoc, props
next-utils
πŸ₯© 🍳 A set of Next.js HoC utilities to make your life easier
Stars: ✭ 30 (-88.97%)
Mutual labels:  hoc
react-formular
This libraray is an experimental approach to bind forms and its inputs and editors together using the new React Context API. It aims to be fully customizable and composable. ItΒ΄s only a set of Higher-Order-Components. Because of the decoupled nature, Middlewares makes it easy to build custom Validations, Security Guards and other data interceptors.
Stars: ✭ 43 (-84.19%)
Mutual labels:  hoc
react-known-props
About 700 props React recognizes
Stars: ✭ 45 (-83.46%)
Mutual labels:  props
react-translations-provider
React render props component for setting translations
Stars: ✭ 35 (-87.13%)
Mutual labels:  props
addhoc
Handy little helper to create proper React HOC functions complete with hoisted statics and forwarded refs
Stars: ✭ 40 (-85.29%)
Mutual labels:  hoc
think
Instagram clone using React Native with funcional components, React Hooks, React Navigation 4x and Reactotron debugger
Stars: ✭ 20 (-92.65%)
Mutual labels:  hoc
React Click Outside
Higher Order Component that provides click outside functionality
Stars: ✭ 266 (-2.21%)
Mutual labels:  hoc
prop-types
Fluent prop validation for Vue
Stars: ✭ 97 (-64.34%)
Mutual labels:  props
react-portal-hoc
A stupid HOC to make a stupid portal so you can make stupid modals
Stars: ✭ 14 (-94.85%)
Mutual labels:  hoc
hoc-element-table
πŸ“¦ A Vue 3.x Table Component built on Webpack 5
Stars: ✭ 26 (-90.44%)
Mutual labels:  hoc
react-drip-form
β˜• HoC based React forms state manager, Support for validation and normalization.
Stars: ✭ 66 (-75.74%)
Mutual labels:  hoc
with-wrapper
React HOC for wrapper components.
Stars: ✭ 35 (-87.13%)
Mutual labels:  hoc
ReactEd
An extension to assist with development of react and redux applications.
Stars: ✭ 48 (-82.35%)
Mutual labels:  props
react-tabllist
React-based customizable style table or list components that support event and callback functions.
Stars: ✭ 20 (-92.65%)
Mutual labels:  props
vue-on-rails
Easy way to mount/destroy Vue.js components with Ruby on Rails and Turbolinks 5
Stars: ✭ 17 (-93.75%)
Mutual labels:  props
TrackAssemblyTool
A Garry's mod tool for assembing a prop-segmented track
Stars: ✭ 17 (-93.75%)
Mutual labels:  props
styled-svg
A styled-components generator for SVG files to use in react
Stars: ✭ 37 (-86.4%)
Mutual labels:  props
Seapig
🌊🐷 Utility for generalized composition of React components
Stars: ✭ 269 (-1.1%)
Mutual labels:  props
watermark-enhancer
Add watermark to react components in a more elegant way
Stars: ✭ 66 (-75.74%)
Mutual labels:  hoc

shouldComponentUpdate-Children

A PureComponent alternative that will actually improve your application performance!

"Shallow Equal" HOC implementation to optimize shouldComponentUpdate with children / React elements.

See live example here: codepen.io/NoamELB/pen/RLoxLv

Usage

Install

npm i -S shouldcomponentupdate-children

Option 1: As an HOC when exporting a component:

import {useShallowEqual} from 'shouldcomponentupdate-children';

class MyComponent extends React.Component {
    ....
}
const MyPerformantComponent = useShallowEqual(MyComponent);

export default MyPerformantComponent;

Option 2: As an HOC when importing a component:

import {useShallowEqual} from 'shouldcomponentupdate-children';
import MyComponent from './my-component';

const MyPerformantComponent = useShallowEqual(MyComponent); // use it just like you would use MyComponent

Option 3: As the shouldComponentUpdate implementation

import {shallowEqual} from 'shouldcomponentupdate-children';

class MyComponent extends React.Component {
    shouldComponentUpdate(nextProps, nextState) {
        return shallowEqual(this.props, nextProps, this.state, nextState);
    }
}
export default MyComponent;

The Problem

React will create a new instance of a React Element on each render, so generic implementations to shouldComponentUpdate will return true even if nothing had changed!

Basically, this simple shouldComponentUpdate implementation:

return this.props.children !== nextProps.children;

is almost as good as writing:

return true;

See live example here: codepen.io/NoamELB/pen/RLoxLv

Read more about it here: https://medium.com/myheritage-engineering/how-to-greatly-improve-your-react-app-performance-e70f7cbbb5f6

Our Solution

We created an HOC which uses Inheritance Inversion to extend components with a generic shouldComponentUpdate functionality.

Our generic shouldComponentUpdate implementation does the following:

  • execute the wrapped component's shouldComponentUpdate and continue only if returned true.
  • shallow equal this.state vs next state and return true if not equal.
  • shallow equal all this.props vs next props, but skip React Elements. return true if not equal.
  • if reached here - returns false

But isn't this means that if any React Element is actually changing then my component won't render?

Yes, but that is the whole point. React Elements are not something you can rely upon when implementing shouldComponentUpdate!

In order to tell a component that it should render - you can change any non-React-Element prop to indicate a state change (this can be a designated prop just for that or a prop that is actually in use inside the component).

Q&A

Why use shouldComponentUpdate?

Performance. If the change in state or props does not affect your component, you can tell React. React will not re-render your component for no reason in that case. Read more: https://facebook.github.io/react/docs/react-component.html#shouldcomponentupdate

How to use shouldComponentUpdate?

MyClass extends React.Component {
    ...
    shouldComponentUpdate(nextProps, nextState) {
       // return true or false
    }
}

It's common to want to re-render when either the state or any prop has changed - so react created a class that does it for us:

MyClass extends React.PureComponent {
    ...
    // don't need to implement shouldComponentUpdate
}

Read more: https://facebook.github.io/react/docs/react-api.html#react.purecomponent

What's the problem with PureComponents?

If "children" (or any other prop) is a React element or an array, it will send a new instance every time. This means that in most cases, the following check will always be true:

this.props.children !== nextProps.children

The result is that our component will render even if our prop didn't actually change.

The solution - shouldComponentUpdate-Children

Wrap your components with this HOC and it will perform a wiser shallow equal check. It will skip props that would have always returned true, regardless of their value:

const MyPerformantComponent = useShallowEqual(MyComponent);

How do you determine that a prop is a React Element

  1. Any prop that returns true for React.isValidElement(prop).
  2. Any array prop that have at least one item which returns true for React.isValidElement(prop[i])

Why implement as an HOC?

HOC is very useful in this specific case of Inheritance Inversion, you can use it on the outside when importing.

Let's see some nice examples of when to use on the outside:

  • Using a vendor component that has performance issues? Just wrap it with the HOC after the import.
  • Implementing shouldComponentUpdate in a given component is a refactor headache? HOC to the rescue.
  • shouldComponentUpdate is already implemented but not good enough for your usage? you know who to call.

By tha way, we exports all of the functions from the package, so you can use them directly when implementing shouldComponentUpdate:

  • shouldComponentUpdate(nextProps, nextState) - bind the "this" to the function and just use it as your shouldComponentUpdate.
  • shallowEqual(this.props, nextProps, this.state, nextState) - same but with no need to bind anything.
  • shallowEqualWithoutReactElements(thisProps, nextProps) - the actual shallow equal implementation on the props.
  • shallowEqualState(thisState, nextState) - the actual shallow equal on the state.

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].