question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[FEATURE] implement a `getState` method to get the sync state since `setState` is async.

See original GitHub issue

Do 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:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
xialvjuncommented, Mar 8, 2018

use case is just like this, What is the difference between passing an object or a function in setState?

incrementCount() {
  this.setState((prevState) => {
    // Important: read `prevState` instead of `this.state` when updating.
    return {count: prevState.count + 1}
  });
}

handleSomething() {
  // Let's say `this.state.count` starts at 0.
  this.incrementCount();
  this.incrementCount();
  this.incrementCount();

  // If you read `this.state.count` now, it would still be 0.
  // But when React re-renders the component, it will be 3.
}

Yeah, setState with a function is OK. Then how about this?

checkAndDoSomething() {
  if (this.getState().count > 5) {
    doSomething();
  }
}
incrementCount() {
  this.setState((prevState) => ({count: prevState.count + 1}))
}
incrementManyTimes(time) {
  for (let i = 0; index < time; i++) {
    this.incrementCount();
  }
  this.checkAndDoSomething();
}

And in fact, we can change incrementCount to:

incrementCount() {
  this.setState({count: this.getState().count + 1})
}

Why should we use getState rather than setState with function as argument?

  1. it has much more consistency: getState <-> setState;
  2. it has purer purpose: getState is just to get state, but setState with function as argument is to get state and use that state to set state;

@gaearon

1reaction
xialvjuncommented, Feb 11, 2018

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, like onScroll, I can not totally rely on this.state, so sometimes I attach some state just on this, like this.a=xxx;

… well, sorry, maybe this API is not that necessary.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found