How do you memoize a component? in REACT

Practice



There are memoization libraries available that can be used with function components. For example, the

moize
library can memoize a component inside another component.
import moize from 'moize'
import Component from './components/Component' // this module exports a non-memoized component

const MemoizedFoo = moize.react(Component)

const Consumer = () => {
  <div>
    {'I will memoize the following entry:'}
    <MemoizedFoo/>
  </div>
}

Update: As of React v16.6.0, we have

React.memo
It provides a higher-order component that memoizes a component as long as its props do not change. To use it, simply wrap the component with React.memo before using it.
 const MemoComponent = React.memo(function MemoComponent(props) {
    /* render using props */
  });
  OR
  export default React.memo(MyFunctionComponent);

Comments

To leave a comment

If you have any suggestion, idea, thanks or comment, feel free to write. We really value feedback and are glad to hear your opinion.
To reply

Lectures and tutorial on "Scripting client side JavaScript, jqvery, BackBone"

Terms: Scripting client side JavaScript, jqvery, BackBone