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 :: how to setup icomoon in react js 
Javascript :: isChecked radio button jQuery 
Javascript :: how to set window location search without reload 
Javascript :: string match 
Javascript :: car image api free 
Javascript :: how to write a javascript function 
Javascript :: react hide element 
Javascript :: jquery data 
Javascript :: make multiple api call using promise.all 
Javascript :: how to install nide js in ubuntu 
Javascript :: live search javascript 
Javascript :: javascript change input value jquery 
Javascript :: removes null and false values from an array 
Javascript :: get user agent in js 
Javascript :: axios post request with authorization header and body 
Javascript :: call multiple functions onclick react 
Javascript :: moment 
Javascript :: Como saber se existe um atributo em um objeto 
Javascript :: angularjs 1.5.6 cdn 
Javascript :: javascript rect 
Javascript :: mongo connect npm 
Javascript :: delete folder with deno 
Javascript :: capitalize name function javascript 
Javascript :: camelcase 
Javascript :: how to convert draftjs content to html 
Javascript :: js socket.emit 
Javascript :: react router link with params 
Javascript :: js get each pair of values from an array of objects 
Javascript :: javascript add to html 
Javascript :: moment format heure 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =