[FEATURE] implement a `getState` method to get the sync state since `setState` is async.
See original GitHub issueDo you want to request a feature or report a bug?
FEATURE REQUEST
What is the current behavior?
class MyComponent extends Component {
state = { a: 1 }
method1 = () => {
this.setState({ a: 2 });
this.method2();
}
method2 = () => {
// It can not log 2 in the console.
console.log(this.state.a);
}
// xxx...
}
What is the expected behavior?
class MyComponent extends Component {
state = { a: 1 }
method1 = () => {
this.setState({ a: 2 });
this.method2();
}
method2 = () => {
// With the `getState` method, it should log 2 in the console.
console.log(this.getState().a);
}
// xxx...
}
Issue Analytics
- State:
- Created 6 years ago
- Reactions:2
- Comments:6 (3 by maintainers)
Top Results From Across the Web
React setState/getState and asynchronous
Documentation tel us that setState is async. Fine, but that means we can't safely use this.state and we need an async getState as...
Read more >setState is an Asynchronous Function
This function will get called once the state has been updated, and the callback will receive the updated value of the state.
Read more >Is setState() method async
This function is used to update the state of a component, but it's important to remember that setState is asynchronous.
Read more >Why Is setState() Asynchronous In React?
Accessing the state immediately after calling setState() returns the existing value and not the updated one.
Read more >Why Don't React State Updates Reflect Immediately?
State updates in React are asynchronous because rendering is an expensive operation and making state updates synchronous may cause the browser ...
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 Free
Top 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
use case is just like this, What is the difference between passing an object or a function in setState?
Yeah,
setState
with a function is OK. Then how about this?And in fact, we can change
incrementCount
to:Why should we use
getState
rather thansetState with function as argument
?getState <-> setState
;getState
is just to get state, butsetState with function as argument
is to get state and use that state to set state;@gaearon
Well, it indeed can use the callback to ensure
this.method2
be executed after rendered. In fact I have forgotten why I want this API. I just remember once upon a time I was confused about does the component have rendered and the state have merged if the event occurred too fast, likeonScroll
, I can not totally rely onthis.state
, so sometimes I attach some state just onthis
, likethis.a=xxx;
…… well, sorry, maybe this API is not that necessary.