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

How to add a new key value to react js state array?

I want to add new key value to array ınto state? I tell you what I want to do below. How can I do that? 1. Start state in consruture method `this.state = { files: [] }' After I doing set state with active files this.setState({files:…
ahmet
  • 363
  • 2
  • 5
  • 16
8
votes
3 answers

Set ReactJS state asynchronously

If you do an asynchronous action that updates the state in componentWillMount (like the docs say), but the component is unmounted (the user navigates away) before that async call is complete, you end up with the async callback trying to set the…
nicholas
  • 14,184
  • 22
  • 82
  • 138
7
votes
2 answers

Why React goes Infinite when I set state in function body?

If we set the state with the same value component won't re-render, but it's not applicable when I set the state in the function body. For example, if I set the same state on the button click and the button clicked, the component does not…
Shiva
  • 476
  • 3
  • 15
7
votes
2 answers

setState() or markNeedsBuild() called during build. this overlay widget cannot be marked as needing to build

I tried to script animated dialog with 'showGeneralDialog' inside SetState() of 'fling' function called by {AbsorbPointer} (specifically {GestureDetector} onPanEnd). I attached the simple code, I tried to do it with future async await, etc but…
Liron Abutbul
  • 85
  • 1
  • 2
  • 6
7
votes
1 answer

Calling setState() during build without user interaction

What I did : I have integrated FCM (Firebase Cloud Messaging) in my flutter app. I shared the context and the setState() of each page in static variables with these two lines getting called in all the build functions of my pages…
Khalil Bz
  • 557
  • 1
  • 9
  • 24
7
votes
1 answer

Repeatedly calling setState inside componentWillUpdate or componentDidUpdate?

I'm trying to figure out the orientation of background-images in a React component that are passed in as props. I start off by creating an Image object and setting its src to the new Image: getImage() { const src = this.props.url; const…
a7dc
  • 3,323
  • 7
  • 32
  • 50
7
votes
2 answers

setTimeout in react setState

this.setState(prevState => ({ score: prevState.score + 10, rightAnswers: prevState.rightAnswers + 1, currentQuestion: setTimeout(() => { prevState.currentQuestion + 1 }, 2000) })) } On button click I…
Happy Block
  • 79
  • 1
  • 1
  • 4
7
votes
1 answer

Maximum update depth exceeded. This can happen when a component repeatedly calls setState inside componentWillUpdate or componentDidUpdate

I'm making this Conway's game of life react project and it was working just fine but when I added the last couple of buttons to clear the board and some other functionalities react gave me this error Maximum update depth exceeded. This can happen…
UWGOOSE
  • 833
  • 3
  • 11
  • 20
7
votes
3 answers

Cannot call `this.setState` with object literal bound to `partialState` because string [1] is compatible not with string literal [2]

I've been banging my head against the wall trying to get this to work, any advice? I'm using react with flow here. I am having quite a fight understand this code annotation stuff and i am learning at the same time. At first it was overwhelming but…
reiahx01
  • 101
  • 1
  • 5
7
votes
1 answer

State updates might be asynchronous

What exactly do they mean? If I understand it correctly, I can't use this.state when calculating new state, unless I pass a function as a first parameter to setState(): // Wrong this.setState({a: f(this.state)}); // Correct this.setState(prevState…
x-yuri
  • 16,722
  • 15
  • 114
  • 161
7
votes
2 answers

Diagnosing RangeError: Maximum call stack size exceeded in React KeyEscapeUtils

Background Our webapp is written with React and Redux using the official react-redux bindings. Another primary library used in this web app is PaperJS. We recently transitioned this to being a Redux app, though it has used React for a while. The…
Scotty H
  • 6,432
  • 6
  • 41
  • 94
7
votes
2 answers

setState not triggering a re-render when data has been modified

Just started working with React, and it's been going relatively smooth up until this point. I have a list, that contains X pre-defined items, which means that the list is always rendered with a given amount of rows. This data is collected from a…
Joel
  • 71
  • 1
  • 1
  • 5
6
votes
3 answers

Flutter: keep data in textfield after setstate

I have an array of textfields. A button which adds a new textfield to the array. If you add data to the textfield and add a new element by clicking the add button, the data added is gone. The button function has a setstate in there to see the new…
desancheztorres
  • 353
  • 1
  • 6
  • 13
6
votes
2 answers

How can I pass event to setState callback function?

Is it possible in React to pass an external event to the callback function of setState? Example someFunc(event) { this.setState( { value: event.target.value }, () => { …
Frederik Petersen
  • 1,025
  • 3
  • 16
  • 30
6
votes
1 answer

React with typescript - type safety with setState

So I've recently discovered that callbacks for event handlers are bad for rendering performance: https://reactjs.org/docs/handling-events.html I'm trying to heed this by grabbing the properties off the event in a class method rather than doing…
typeofChris
  • 63
  • 1
  • 6