“React machen SetState synchron” 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 machen SetState synchron”

Fragen ähnlich wie “React machen SetState synchron”

Weitere verwandte Antworten zu “React machen SetState synchron” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen