“React SetState Synchronous” Code-Antworten

React machen SetState synchron

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
    }
} 
Selfish Swan

React SetState Synchronous

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

Ähnliche Antworten wie “React SetState Synchronous”

Fragen ähnlich wie “React SetState Synchronous”

Weitere verwandte Antworten zu “React SetState Synchronous” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen