# React : I think state is not updating

**URL:** <https://forum.freecodecamp.org/t/react-i-think-state-is-not-updating/408199>\
**Category:** General\
**Created:** [July 6, 2020, 4:16pm UTC](https://forum.freecodecamp.org/t/react-i-think-state-is-not-updating/408199 "2020-07-06T16:16:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Advitya-sharma](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/advitya-sharma/32/151816_2.png) [@Advitya-sharma](https://forum.freecodecamp.org/u/Advitya-sharma)\
**Post date:** [July 6, 2020, 4:16pm UTC](https://forum.freecodecamp.org/t/react-i-think-state-is-not-updating/408199/1 "2020-07-06T16:16:30Z")

</div>

## console.log prints the old state

causing error in eval

eg:- eval(“12\*”)

````auto
      this.setState({
        displayFormula: this.state.displayFormula + this.state.displayTitle
      });
      console.log(this.state.displayFormula);
      const res = eval(this.state.displayFormula);
      this.setState({ displayTitle: res });
    } ```
````

---

<div class="post-metadata">

**Author:** ![lasjorg](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/lasjorg/32/280761_2.png) [@lasjorg](https://forum.freecodecamp.org/u/lasjorg)\
**Post date:** [July 6, 2020, 6:30pm UTC](https://forum.freecodecamp.org/t/react-i-think-state-is-not-updating/408199/2 "2020-07-06T18:30:13Z")

</div>

You can use the setState callback, or a lifecycle method, to log the state after a setState call. And if your setState depends on the current state you may have to use the updater function.

> [setState](https://reactjs.org/docs/react-component.html#setstate)
> 
> setState() does not always immediately update the component. It may batch or defer the update until later. This makes reading this.state right after calling setState() a potential pitfall. Instead, use componentDidUpdate or a setState callback (setState(updater, callback)), either of which are guaranteed to fire after the update has been applied. If you need to set the state based on the previous state, read about the updater argument below.

[State Updates May Be Asynchronous](https://reactjs.org/docs/state-and-lifecycle.html#state-updates-may-be-asynchronous)

If you need help with your code you will likely have to post a link to a live version, you can use something like [Codesandbox](https://codesandbox.io/).

---

<div class="post-metadata">

**Author:** ![ILM](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/ilm/32/363239_2.png) [@ILM](https://forum.freecodecamp.org/u/ILM)\
**Post date:** [June 1, 2021, 6:47pm UTC](https://forum.freecodecamp.org/t/react-i-think-state-is-not-updating/408199/4 "2021-06-01T18:47:40Z")

</div>

Closed on June 1, 2021, 6:47pm UTC


