Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

react make setstate synchronous

class MyComponent extends React.Component {

    function setStateSynchronous(stateUpdate) {
        return new Promise(resolve => {
            this.setState(stateUpdate, () => resolve());
        });
    }

    async function foo() {
        // state.count has value of 0
        await setStateSynchronous(state => ({count: state.count+1}));
        // execution will only resume here once state has been applied
        console.log(this.state.count);  // output will be 1
    }
} 
Comment

react setState synchronous

// Correct
this.setState((state, props) => ({
  counter: state.counter + props.increment
}));
Comment

PREVIOUS NEXT
Code Example
Javascript :: what is bom in javascript 
Javascript :: canvas js in react 
Javascript :: how to change port in next js 
Javascript :: angular loop through array 
Javascript :: vue on page link or anchor 
Javascript :: angular input decimal pipe 
Javascript :: what does find return javascript 
Javascript :: javascript problems 
Javascript :: discord.js vs discord.py 
Javascript :: how to link to a different component in reactjs without react router 
Javascript :: map duplicate keys JS 
Javascript :: multiselect checkbox 
Javascript :: find last item in an array JS 
Javascript :: jq json 
Javascript :: encrpting data in javascript 
Javascript :: working with multiple db in single query mongodb 
Javascript :: angular chart 
Javascript :: react lifecycle hooks 
Javascript :: passing ref to child component 
Javascript :: Plugin "react" was conflicted between "package.json » eslint-config-react-app 
Javascript :: == vs === javascript 
Javascript :: express middleware status code always 200 
Javascript :: Div draggable x axe only 
Javascript :: crud application in mean stack 
Javascript :: capacitorjs get zip code example 
Javascript :: react state deconstructed 
Javascript :: build a javascript to easily change website colours theme 
Javascript :: javascript get the first day of the month and last day 
Javascript :: onclick confirm jquery anchor tag 
Javascript :: feathersjs mysql example 
ADD CONTENT
Topic
Content
Source link
Name
8+6 =