score:3
This happens because as this answer states React tries to batch setState
calls and process them together when it can. But this is not the case with asynchronous computations because React (and anybody in general) can't predict and reproduce the order of setState
's called asynchronously.
So in your case it fallbacks to just updating state 3 times.
score:1
You can got this answer from React.Component life cycle doc (https://reactjs.org/docs/react-component.html)
componentDidUpdate() is invoked immediately after updating occurs. This method is not called for the initial render.
Use this method shouldComponentUpdate()
. This method allows your Component to exit the Update life cycle if there is no reason to apply a new render. Out of the box, the shouldComponentUpdate() is a no-op that returns true. This means every time we start an Update in a Component, we will re-render.
I add more code
shouldComponentUpdate = function(nextProps, nextState) {
return nextState.value !== this.state.value;
}
// Change value will set after await
runAsyncFunc = async() => {
console.log('BEFORE AWAIT');
this.setState({ value: 1 });
this.setState({ value: 1 });
this.setState({ value: 1 });
await setTimeout(()=>{}, 2000);
console.log('AFTER AWAIT');
this.setState({ value: 2 });
this.setState({ value: 2 });
this.setState({ value: 2 });
}
Check my Codepen
So if you want to prevent unnecessary render custom method shouldComponentUpdate
Source: stackoverflow.com
Related Query
- setState being called multiple times after await
- UseEffect being called multiple times
- React-redux connect()'s mapStateToProps being called multiple times on react-router navigation
- React onChange handler is being called multiple times during page load
- React navigation event listeners being called multiple times
- state is immediately available when setState is called after await
- React component table is being rendered multiple times or duplicated after updating state
- React ComponentDidMount being called multiple times
- Saga is being called multiple times
- Higher-Order Component Being Called Multiple Times
- React Fetch API Being Called Multiple Times on page load
- ReactJS element not updating after setState being called
- My setState method called multiple times which causes a problem?
- React-js Fetch api being called multiple times during Login
- Why fetchDashboard function is being called multiple times
- React setState called multiple times on the same state object
- React: prevent functions from being called multiple times - prevent re-rendering
- React state is not updating immediately after setState is being called
- Stop Function being called multiple times when scrolling
- How to avoid useEffect being called multiple times
- ReactJS: Function run multiple times without being called
- React component render is called multiple times when pushing new URL
- Why componentDidMount gets called multiple times in react.js & redux?
- Lifecycle componentWillReceiveProps is called multiple times
- Child component constructor called multiple times
- componentDidMount not being called after goBack navigation call
- componentDidMount seems not to be called after setState in ReactJS
- Redux mapStateToProps called multiple times
- Why is a React Ref callback (as an arrow function or inline function) called multiple times on initial page load?
- Jest: setTimeout is being called too many times
More Query from same tag
- Parent component re-mounting when react-router switching links
- React app POST issues with CORS and fetch
- Why can I not use a variable as parameter in the require() function of node.js (browserify)?
- How to automatic redirect home page?
- How to merge object with same key in react
- redux-form + react-widgets DateTimePicker
- How to implement SSR for Material UI's media queries in NextJs?
- toggling boolean value in stateful component
- How to make User Logout After Countdown React
- Passing the state to the state in another component - React
- How to make a an onChange function with typescript that works on all input types?
- Material UI Webpack Build
- how to solve asynchronous behaviour in search Box react
- Using webpack and React to load images
- Bad Request 400 - Django REST Framework
- Is there a way to include certain data in the body of all axios requests by default?
- error:styled_components__WEBPACK_IMPORTED_MODULE_0__.default.FaChevronRight is not a function
- Webpack - image imported by url-loader & file-loader is blank
- React useContext value is not getting passed from the provider?
- How do you map json in React?
- How can I make inline styling more compact instead of using multiple variables?
- How to make a new object in an array
- i already have a Key in my component but why is it still looking for a unique key?
- React progress element has a bug on IE10
- How to use React Portal?
- State machines and UI: Rendering based on 'node-level' states instead of 'leaf' states
- Enable button if the component values have been changed in react
- Typescript React/Redux : Argument of type 'typeof MyClass' is not assignable to parameter of type 'ComponentType<...'
- react inner component's state change don't smooth
- Infinite loop while calling react setState in a second component