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

1836 questions
493
votes
30 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
  • 5,935
  • 7
  • 20
  • 36
224
votes
10 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
  • 2,749
  • 6
  • 14
  • 17
218
votes
5 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
  • 2,759
  • 2
  • 11
  • 14
218
votes
7 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
  • 2,479
  • 3
  • 15
  • 15
201
votes
21 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
  • 7,161
  • 7
  • 39
  • 73
165
votes
4 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,029
  • 5
  • 20
  • 31
121
votes
5 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
  • 1,510
  • 4
  • 10
  • 22
118
votes
10 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
  • 3,578
  • 10
  • 26
  • 44
75
votes
11 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
  • 7,498
  • 18
  • 49
  • 87
38
votes
2 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
  • 1,844
  • 1
  • 19
  • 32
31
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
  • 599
  • 1
  • 6
  • 14
26
votes
2 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
  • 2,593
  • 9
  • 28
  • 50
23
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
  • 1,463
  • 3
  • 21
  • 53
19
votes
2 answers

Flutter: Does it matter what code is in setState()?

When we want a StatefulWidget to rebuild we call setState() but does it really matter if the code we type is inside that function or outside of it? Is this: class _ShoppingListState extends State { Set _shoppingCart = new…
Jus10
  • 10,189
  • 16
  • 43
  • 72
17
votes
3 answers

Flutter GestureDetector, onTap gets triggered automatically, how to?

I have a stateful widget that has a simple grid and each grid cell has a container inside it. I would like to click on a cell / container and have its content changed. The problem is that the GestureDetector -> onTap method gets triggered on app…
Patrioticcow
  • 23,784
  • 68
  • 202
  • 327
1
2 3
99 100