Don't rerender if nothing changed in React 16.6.0!
React 16.6.0 came out today and React now provides a handy function to create a component that won't rerender if it doesn't get new props, React.memo
.
const BlueComponent = () => {
return <div>no props don't rerender</div>;
}
const MemoComponent = React.memo(BlueComponent);
BlueComponent
is a component that will re-render every time it's parent re-renders. It doesn't take props though, so it won't look any different based on new props. MemoComponent
is a component created by passing BlueComponent
to React.memo
. It will not re-render when it's parent re-renders.
Check out another example in the code sandbox below.
Read more about React 16.6.0 here.
Tweet