Questions tagged [setstate]

Use for questions relating to the [setstate] method and component/widget state management in [reactjs] and [flutter].

setState is a component method in and which updates the local component/"widget" state and triggers a re-render of the component based on the new state.

Resources

2626 questions
620
votes
37 answers

How to update nested state properties in React

I'm trying to organize my state by using nested property like this: this.state = { someProperty: { flag:true } } But updating state like this, this.setState({ someProperty.flag: false }); doesn't work. How can this be done correctly?
Alex Yong
  • 7,425
  • 8
  • 24
  • 41
419
votes
33 answers

Can't perform a React state update on an unmounted component

Problem I am writing an application in React and was unable to avoid a super common pitfall, which is calling setState(...) after componentWillUnmount(...). I looked very carefully at my code and tried to put some guarding clauses in place, but the…
Igor Soloydenko
  • 11,067
  • 11
  • 47
  • 90
304
votes
9 answers

setState() or markNeedsBuild called during build

class MyHome extends StatefulWidget { @override State createState() => new MyHomePage2(); } class MyHomePage2 extends State { List items = new List(); buildlist(String s) { setState(() { print("entered…
Divyang Shah
  • 3,577
  • 5
  • 14
  • 27
293
votes
7 answers

When to use React setState callback

When a react component state changes, the render method is called. Hence for any state change, an action can be performed in the render methods body. Is there a particular use case for the setState callback then?
Sahil Jain
  • 3,649
  • 2
  • 14
  • 16
279
votes
9 answers

Can I execute a function after setState is finished updating?

I am very new to ReactJS (as in, just started today). I don't quite understand how setState works. I am combining React and Easel JS to draw a grid based on user input. Here is my JS bin: http://jsbin.com/zatula/edit?js,output Here is the code: …
monalisa717
  • 3,187
  • 3
  • 18
  • 17
266
votes
11 answers

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

I am trying to refactor the following code from my render view: to a version where the bind is within the constructor. The…
user3611459
  • 3,311
  • 6
  • 16
  • 18
199
votes
12 answers

React setState not updating state

So I have this: let total = newDealersDeckTotal.reduce(function(a, b) { return a + b; }, 0); console.log(total, 'tittal'); //outputs correct total setTimeout(() => { this.setState({ dealersOverallTotal: total }); },…
The worm
  • 5,580
  • 14
  • 36
  • 49
188
votes
5 answers

Does React keep the order for state updates?

I know that React may perform state updates asynchronously and in batch for performance optimization. Therefore you can never trust the state to be updated after having called setState. But can you trust React to update the state in the same order…
darksmurf
  • 3,747
  • 6
  • 22
  • 38
128
votes
13 answers

React setState not Updating Immediately

I'm working on a todo application. This is a very simplified version of the offending code. I have a checkbox:

Writing Item

Here's the function…
lost9123193
  • 10,460
  • 26
  • 73
  • 113
58
votes
5 answers

Flutter setState to another class?

I have a root class RootPage which is a StatefulWidget which is always in view. I would like to change the body in RootPage which is controlled by RootPage's currentPage Widget from different classes such as my FeedPage class and any other class…
Jayden
  • 1,029
  • 3
  • 9
  • 15
57
votes
3 answers

Set state when testing functional component with useState() hook

When I tested class component with enzyme I could do wrapper.setState({}) to set state. How can I do the same now, when I am testing function component with useState() hook? For example in my component I have: const [mode, setMode] = useState("my…
Anna
  • 2,911
  • 6
  • 29
  • 42
51
votes
3 answers

How to dispose of my Stateful Widget completely?

I call my stateful widget page and get some info from the server. If no info found it warns the user that there isn't any info. From the drawer back button, I go back to the previous page. If I keep repeat back and forth very fast I get an error on…
Nick
  • 4,163
  • 13
  • 38
  • 63
31
votes
7 answers

Does this.setState return promise in react

I made my componentWillMount() async. Now I can using await with the setState. Here is the sample code: componentWillMount = async() => { const { fetchRooms } = this.props await this.setState({ }) fetchRooms() } So question here is…
Profer
  • 553
  • 8
  • 40
  • 81
30
votes
4 answers

Reactjs-setState previous state is the first argument, props as the second argument

I have read in this official article these lines: this.props and this.state may be updated asynchronously, you should not rely on their values for calculating the next state. Can anyone please explain to me what the following code is trying to…
QuickCoder
  • 415
  • 1
  • 4
  • 7
29
votes
2 answers

How do I call setState from another Component in ReactJs

I have two react components and I'd like to call setState to set a state in the one Component but called in the other one. How do I do that?
Dennis Schweizer
  • 393
  • 1
  • 3
  • 6
1
2 3
99 100