Does this.setState return promise in react

You can promisify this.setState so that you can use the React API as a promise. This is how I got it to work: class LyricsGrid extends Component { setAsyncState = (newState) => new Promise((resolve) => this.setState(newState, resolve)); Later, I call this.setAsyncState using the standard Promise API: this.setAsyncState({ lyricsCorpus, matrix, count }) .then(foo1) .then(foo2) .catch(err => … Read more

Flutter setState to another class?

You can use callbacks functions to achieve this. Please refer to the below code. import ‘package:flutter/material.dart’; class RootPage extends StatefulWidget { @override _RootPageState createState() => new _RootPageState(); } class _RootPageState extends State<RootPage> { FeedPage feedPage; Widget currentPage; @override void initState() { super.initState(); feedPage = FeedPage(this.callback); currentPage = feedPage; } void callback(Widget nextPage) { setState(() { … Read more

React setState not Updating Immediately

You should invoke your second function as a callback to setState, as setState happens asynchronously. Something like: this.setState({pencil:!this.state.pencil}, myFunction) However in your case since you want that function called with a parameter you’re going to have to get a bit more creative, and perhaps create your own function that calls the function in the props: … Read more

React setState not updating state

setState() is usually asynchronous, which means that at the time you console.log the state, it’s not updated yet. Try putting the log in the callback of the setState() method. It is executed after the state change is complete: this.setState({ dealersOverallTotal: total }, () => { console.log(this.state.dealersOverallTotal, ‘dealersOverallTotal1’); });

ReactJS: Warning: setState(…): Cannot update during an existing state transition

Looks like you’re accidentally calling the handleButtonChange method in your render method, you probably want to do onClick={() => this.handleButtonChange(false)} instead. If you don’t want to create a lambda in the onClick handler, I think you’ll need to have two bound methods, one for each parameter. In the constructor: this.handleButtonChangeRetour = this.handleButtonChange.bind(this, true); this.handleButtonChangeSingle = … Read more

Can I execute a function after setState is finished updating?

setState(updater[, callback]) is an async function: https://facebook.github.io/react/docs/react-component.html#setstate You can execute a function after setState is finishing using the second param callback like: this.setState({ someState: obj }, () => { this.afterSetStateFinished(); }); The same can be done with hooks in React functional component: https://github.com/the-road-to-learn-react/use-state-with-callback#usage Look at useStateWithCallbackLazy: import { useStateWithCallbackLazy } from ‘use-state-with-callback’; const [count, setCount] … Read more