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
5
votes
2 answers

Replace of setState callback in react hook with useEffect hooks for complicated scenario not working

Hi I have a scenario where i need to replace setState callback after setting new state in react hooks with React_hooks in a particular scenario. The current code is:: const ThemeOptions = () => { const [previewType, setPreviewType] =…
user12893845
  • 176
  • 2
  • 13
5
votes
2 answers

When does React re-render after a setState call made inside an event handler

From React DOCS: https://reactjs.org/docs/state-and-lifecycle.html State Updates May Be Asynchronous React may batch multiple setState() calls into a single update for performance. This makes total sense. If you have something like the function…
cbdeveloper
  • 27,898
  • 37
  • 155
  • 336
5
votes
1 answer

React setState does not set state during a while loop

I want to avoid data multiplication, so i wanted to create a loop for calling my dataprovider for different site_id's. I created a while loop and set the state values within this while loop. What I realized that from my 2 element array (I have 2…
Reka Karolyi
  • 219
  • 4
  • 14
5
votes
1 answer

Chain React setState callbacks

I need to load three different json files in an ordered sequence and with a fetch (the reason is i'm using nextjs export and i need those files to be read dynamically, so I fetch them when needed and their content can change even after the…
Andrew P.
  • 160
  • 1
  • 8
5
votes
4 answers

Add an object with setState() to an array of objects

My component's state is an array of objects: this.state = { userFavorites: [{id: 1, title: 'A'}, {id: 2, title: 'B'}] } I need to, everytime I click in a button, update my state with another object like the ones I have in my state above; for…
Rafael Guedes
  • 319
  • 1
  • 5
  • 10
5
votes
1 answer

Pop refresh last screen using Flutter and StreamBuilder

I want to reload my list when Navigator.pop, but i can't do that. My code: class ConfigCNPJPage extends StatefulWidget { bool voltarTela; ConfigCNPJPage({Key key, this.voltarTela}) : super(key: key); @override ConfigCNPJPageState…
Rafael Nonino
  • 180
  • 4
  • 15
5
votes
4 answers

Async await on component did mount

This is my componentDidMount method. I want to set the state of the current user and then call the function when that user is set. How can I do this? componentDidMount = () => { firebase.auth().onAuthStateChanged((user) => { if (user) { …
The Walrus
  • 1,148
  • 6
  • 28
  • 46
5
votes
3 answers

React, setState with async updater parameter?

Does anybody know if in React it's possible to use an async updater parameter in setState(updater) ? I have the following code which is not working (f is called but the UI is not updated): this.setState( async (prevState) => ({ foo: await…
nino.porcino
  • 1,133
  • 1
  • 8
  • 16
5
votes
2 answers

How can I use `setState` with objects nested in an array in React JS?

With this code, I am able to successfully use setState on a simple object – when I click on "Joey" the name changes to "Igor". class Card extends React.Component { myFunc = () => {this.props.change('Igor')}; render() { …
samurai_jane
  • 795
  • 8
  • 20
5
votes
2 answers

React filter method without mutating state

My current state has this: state = { items : [{id: 1, text: 'test words'}, {id: 2, text: 'another test'}] Here's my function to remove objects from the array, trying to avoid mutating state. handleRemove(passedInId) { const myItems =…
Clay_F
  • 561
  • 1
  • 6
  • 17
5
votes
2 answers

setState not working with Switch block

I'm trying to build a little chat bot, and I seem to be nearing completion if it were not for this bug. The issue seems to be that my switch statement isn't handling the setState properly. Uquestion extends React.Component { constructor(props) { …
WriterState
  • 359
  • 3
  • 18
4
votes
3 answers

How to manage a custom widget state in SingleChildScrollView Widget

I'm trying to design this view. I already have the basic design of the cards, but i would like to know how to change the card's background color, the card's border color and add the little green square according to the width size of the current…
4
votes
4 answers

How to use setState along with dispatch from redux - it re-renders whole component and restarts updated setState

I have this component const CheckboxColumn: FC = ({ id_llamada }) => { // select pickup const dispatch = useTypedDispatch(); const [isPickupSelected, setIsPickupSelected] = useState(false); const…
Diego
  • 421
  • 3
  • 9
  • 34
4
votes
2 answers

Why setState causes too many rerender Error even though the state hasn't changed

Hi I'm learning React now and having trouble with the state.. I know when the state changes, the component re-renders and the code in UseEffect widout depth only runs once. But I can't explain exactly why infinite rendering occurs when I write…
minami
  • 207
  • 3
  • 13