Refer to the link

  • ShowComponentUpdate reference
  • PrueComponent
  • React.memo
  • useMemo

React.PureComponent

React.PureComponent is similar to React.Component. The difference between them is that React.Component doesn’t implement shouldComponentUpdate(), but React.PureComponent implements it with a shallow prop and state comparison.

React.PureComponent is similar to react.component.react.component.react.pureComponent. The difference is that react.componentDoesn’t implement shouldComponentUpdate() itself, but react.pureComponent simply compares prop to state;

RenderItem extends PureComponent{render(){RenderItem extends PureComponent{render(){RenderItem extends PureComponent{render(){ console.log('render',this.props.value); return <div>{this.props.value}</div> } } class User extends Component{ constructor(props){ super(props); This. State = {list: [1,2,3,4]}} componentDidMount(){console.log(this.props); } render(){ return ( <> <div onClick={()=>this.setState({list: [... this. State. The list, this state. The list. The length + 1]})} > refresh array, Add elements < / div > {this. State. The list. The map (item = > < RenderItem key = value = {item} {item} / >)}}} < / a >)Copy the code

React.memo

React.memo is a higher order component.

If your component renders the same result given the same props, you can wrap it in a call to React.memo for a performance boost in some cases by memoizing the result. This means that React will skip rendering the component, and reuse the last rendered result.

React.memo only checks for prop changes. If your function component wrapped in React.memo has a useState, useReducer or useContext Hook in its implementation, it will still rerender when state or context change.

By default it will only shallowly compare complex objects in the props object. If you want control over the comparison, you can also provide a custom comparison function as the second argument.

React.memo is a higher-order component;

If your components render the same props, you can use a component called react. Memo to wrap around them and cache the results to improve performance. React passes by the render component and reuses the result of the last render.

The react. memo will only check prop changes. If the wrapped method component implements a useStte,useReducer, or useContext hook, it will still render when state/context changes.

Usually it simply compares complex data in the props object. If you want full control over the comparison method, you can provide a custom method as a second argument;

// Same as before... import React ,{memo, Component, PureComponent,} from 'react'; class RenderItem2 extends Component{ render(){ console.log('render',this.props.value); return <div>{this.props.value}</div> } } const RenderItem = memo(RenderItem2);Copy the code

Implement shouldComponentUpdate yourself

Use shouldComponentUpdate() to let React know if a component’s output is not affected by the current change in state or props. The default behavior is to re-render on every state change, and in the vast majority of cases you should rely on the default behavior.

shouldComponentUpdate() is invoked before rendering when new props or state are being received. Defaults to true. This method is not called for the initial render or when forceUpdate() is used.

This method only exists as a performance optimization. Do not rely on it to “prevent” a rendering, as this can lead to bugs. Consider using the built-in PureComponent instead of writing shouldComponentUpdate() by hand. PureComponent performs a shallow comparison of props and state, And that reduces the chance that you’ll skip a necessary update.

If you are confident you want to write it by hand, you may compare this.props with nextProps and this.state with nextState and return false to tell React the update can be skipped. Note that returning false does not prevent child components from re-rendering when their state changes.

We do not recommend doing deep equality checks or using JSON.stringify() in shouldComponentUpdate(). It is very inefficient and will harm performance.

Currently, if shouldComponentUpdate() returns false, then UNSAFE_componentWillUpdate(), render(), and componentDidUpdate() will not be invoked. In the future React may treat shouldComponentUpdate() as a hint rather than a strict directive, and returning false may still result in a re-rendering of the component.

ShouldComponentUpdate () lets React know if the output of the component matches the effects of the current prop/state change. By default, every state change will be rerendered, and in most cases you should follow the default behavior.

ShouldComponentUpdate () is called before each rendering, when the new props/ state has been received. Return true by default. This is not called on first render/force render;

This method only exists for performance optimization, do not rely on it to do “block render” operations, Consider using react.pureComponent to write a shouldComponentUpdate(). PureComponent is a simple way to reduce the number of times you need to update prop/state;

If you consider writing shouldComponentUpdate(), you want to compare this.props with nextProps, this.state with nextState, and return false to tell React that the update should pass by. Remember that when the child stTE changes, even returning false does not prevent the child from rerendering;

We do not recommend using depth alignment/using json.stringify () for alignment. They are very inefficient and degrade performance;

ShouldComponentUpdate () returns false,UNSAFE_componentWillUpdate(),render(),componentDidUpdate() is not triggered. In the future shouldComponentUpdate() will be treated as a hint and still be a strict instruction, and returning false will also re-render the component;

shouldComponentUpdate(nextProps,nextState):boolean{ return nextProps.value ! == this.props.value; }Copy the code

useMemo hook

Returns a memoized value.

Pass a “create” function and an array of dependencies. useMemo will only recompute the memoized value when one of the dependencies has changed. This optimization helps to avoid expensive calculations on every render.

Remember that the function passed to useMemo runs during rendering. Don’t do anything there that you wouldn’t normally do while rendering. For example, side effects belong in useEffect, not useMemo.

If no array is provided, a new value will be computed on every render.

You may rely on useMemo as a performance optimization, not as a semantic guarantee. In the future, React may choose to “forget” some previously memoized values and recalculate them on next render, To free memory for offscreen components. Write your code so that it still works without useMemo — and then add it to optimize performance.

Returns a cached value

Pass in a “create” creation method and a set of dependencies. Using useMode only recalculates cached values if one of the dependent values changes. Caching helps us avoid a lot of expensive calculations every time we render.

Remember that methods passed into useMemo are executed at render time. Don’t do anything you wouldn’t normally do in a render method. For example, side effect needs to be used effect, not useMemo

If no dependent array is passed in, it is recalculated every time it is rendered;

You can use useMemo for performance tuning, but it is not guaranteed to be permanent; In the future React will choose to forget the previous calculation and recalculate it the next time it renders. For example, release the cache of off-screen components. Write code that can run without useMemo and use it as a performance optimization;

const RenderItem = function(props){
	const plus100 = (val)=>{
		console.log('call plus 100',val);
		return val + 100;
	}
	let valuePlus100 = useMemo(()=>plus100(props.value),[props.value]);
	
	return <div>{valuePlus100}</div>
}

Copy the code