setState not working because this._nextState is undefined
See original GitHub issueIn the constructor of my app I call an async function that does
this.setState({ loading: true })
This is the stack trace I get
util.js:9 Uncaught (in promise) TypeError: Cannot set property 'loading' of undefined
at s (util.js:9)
at App.d.setState (component.js:40)
at App.somethingAsync (app.js:47)
at new App (app.js:44)
at T (index.js:67)
at children.js:102
at b (children.js:236)
at b (children.js:231)
at _ (children.js:60)
at T (index.js:180))
In the the setState of component.js#L29 the this._nextState
is undefined and s
is also undefined.
I am using Preact with htm without any compilation, direct import in the browser with the script tag type module.
import { Component, h } from 'https://unpkg.com/preact@10.2.1/dist/preact.module.js'
import htm from 'https://unpkg.com/htm?module'
const html = htm.bind(h)
export default class App extends Component {
constructor (props) {
super(props)
this.somethingAsync = this.somethingAsync.bind(this)
this.state = {
loading: false
}
this.somethingAsync()
}
async somethingAsync () {
this.setState({ loading: true })
setTimeout(() => this.setState({ loading: false }), 100)
}
}
This is the index.js
import { h, render } from 'https://unpkg.com/preact@10.2.1/dist/preact.module.js'
import htm from 'https://unpkg.com/htm?module'
import App from './components/app.js'
const html = htm.bind(h)
render(
html`<${App} />`,
document.getElementById('app')
)
What should I do?
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
this.setState makes variable undefined? - Stack Overflow
Yes, your current problem was due to the undefined local variable ... you should not rely on their values for calculating the next...
Read more >How To Manage State on React Class Components
This tutorial will first show you how to set state using a static value, which is useful for cases where the next state...
Read more >How To Fix Undefined 'this.state' in React Class Components
If you're working with React class components, you may find yourself vexed by 'this.state' being undefined.
Read more >Dive into React codebase: Handling state changes
This is because setState works in an asynchronous way. That means after calling setState the this.state variable is not immediately changed.
Read more >Immutable Data with Immer and React setState - Code Daily
The name will no longer just be Jason but will be Jason Brown and because we ... Say we have 2 nested counters...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Hey,
Usually we dispatch async stuff from
componentDidMount
since this is a bit safer, this because in constructor we are still creating everything while an async setState could happen during the construction of this part of the tree. In ComponentDidMount we’ve already established the semantics of this and are committing it to the DOMHowever it shouldn’t error out and I’ll certainly look at it.
Ok thanks for the answer I will use the
componentDidMount
to make it work meanwhile. I have here a fully working example to test it easily.index.html