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
28
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
  • 26,422
  • 75
  • 217
  • 337
26
votes
4 answers

SetState of an array of Objects in React

Ok, so I'm so frustrated finding the right solution so I'm posting the problem here. Giving an answer would help me a lot, coz I'm stuck! the state tree looks like this this.state = { itemList : [{ _id : 1234, …
Saikat Dey
  • 389
  • 1
  • 4
  • 13
24
votes
6 answers

Is it correct to use await setState()?

my function looks like this: this.setState(prevState => ({ time : prevState.time + 1 }), function() { doSomethingWithNewState(this.state.time) }) is it correct to use await in this situation? like this: await this.setState(prevState => ({ time…
lalezky
  • 374
  • 1
  • 3
  • 11
22
votes
2 answers

how to update multiple state at once using react hook react.js

I would like to know if I can use setState hook multiple times in same function. For example, like this import React, { useEffect, useState } from 'react'; function(props) { const [color, setColor] = useState(0) const [size, setSize]=…
Nomura Nori
  • 4,689
  • 8
  • 47
  • 85
22
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
  • 14,519
  • 21
  • 52
  • 77
20
votes
5 answers

how to set state array using react hooks

Thanks in advance. I have a state array as below. I need to add an item to state array, I came across that we need not do state mutation. How do i set state with prevState. const [messages, setMessages] = React.useState( [ { …
Ankit Jayaprakash
  • 1,040
  • 3
  • 15
  • 31
20
votes
3 answers

React batch updates for multiple setState() calls inside useEffect hook

On this answer by Dan Abramov here on SO, I've found out the following: Does React keep the order for state updates? Currently (React 16 and earlier), only updates inside React event handlers are batched by default. There is an unstable API to…
cbdeveloper
  • 27,898
  • 37
  • 155
  • 336
19
votes
4 answers

How to periodically set state?

I am trying to build a countdown app for special days for example 11 d 2 h 30 m 23s to the new year but I can't reload my state every second so it just shows me the second that I loaded the page I don't know how to dynamically reload the page. …
isa türk
  • 707
  • 1
  • 10
  • 20
19
votes
2 answers

ReactJS concurrent SetState race condition

I have a component structure like this Idea is that actions on components in block E are processed by a function of component A to state of A and than…
akaprog
  • 191
  • 1
  • 1
  • 3
18
votes
3 answers

React: Setting State for Deeply Nested Objects w/ Hooks

I'm working with a deeply nested state object in React. My code base dictates that we try to stick with function components and so every time I want to update a key/value pair inside that nested object, I have to use a hook to set the state. I…
snejame
  • 602
  • 3
  • 9
  • 19
18
votes
1 answer

How to setState in react-testing-library

Overview: I refactoring script tests before I was used Enzyme to test, but now, I want to use @testing-library/react Problem: I can't find a solution for setState in @testing-library/react
Huy Ho
  • 283
  • 1
  • 3
  • 7
18
votes
4 answers

How to change the background color of a button dynamically in onPressed()

I have a list of Raised buttons, I want the background color of the selected button to change in its onPressed() I tried changing the color in setState but it doesn't do anything. This is the function that generates the list of Buttons List
Ghada Shebl
  • 539
  • 2
  • 6
  • 12
18
votes
4 answers

How to change multiple properties of a state in react (at the same time)?

I have this state in my main parent component: this.state = { playableCards: [], openedCard: null, offeredChips: 0, activePlayer: 0, // first player is 0, second player is 1 players: [ { name: "player1", …
catandmouse
  • 11,309
  • 23
  • 92
  • 150
17
votes
2 answers

Using this.setState in the callback of this.setState in React JS?

Is it possible to call this.setState in the callback of this.setState? I am making a Roguelike Dungeon and have a setup where in the callback of this.setState a helper function is used, that calls this.setState again. My game freezes at this…
chemook78
  • 1,168
  • 3
  • 17
  • 38
16
votes
4 answers

Can we pass setState as props from one component to other and change parent state from child component in React?

class App extends Component { constructor() { super(); this.state = { name: 'React' }; this.setState=this.setState.bind(this) } render() { return (

viswa ram
  • 461
  • 2
  • 4
  • 9