# ReactJS: Wrong execution order on calculator

**URL:** <https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463>\
**Category:** General\
**Created:** [July 13, 2020, 6:12pm UTC](https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463 "2020-07-13T18:12:52Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zie62](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zie62/32/148488_2.png) [@Zie62](https://forum.freecodecamp.org/u/Zie62)\
**Post date:** [July 13, 2020, 6:12pm UTC](https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463/1 "2020-07-13T18:12:52Z")

</div>

Hello, I’ve been working on the Javascript calculator project and have been doing it in React because it seemed to be relatively straightforward to do in react, however when I assign something using setState then call a function, the called function executes before the setState does, even though the function call is under the setState. More specifically, I am trying to assign numbers based on the button pressed then concat it to a string of previous inputs before converting to a float to display, but the number it adds is always the number of the button i pressed previously, not the button i pressed the current time. I only have 0 and 1 bound for now for proof of concept, but youll see if you press either button it gives you the placeholder value rather than the number assigned to the button you press. Is there any way to make the initial setState occur before the function call?

Codepen: [https://codepen.io/AugustTGuenther/pen/ExPppvy](https://codepen.io/AugustTGuenther/pen/ExPppvy)

---

<div class="post-metadata">

**Author:** ![Zie62](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zie62/32/148488_2.png) [@Zie62](https://forum.freecodecamp.org/u/Zie62)\
**Post date:** [July 13, 2020, 10:01pm UTC](https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463/2 "2020-07-13T22:01:11Z")

</div>

Bumping for others to view

---

<div class="post-metadata">

**Author:** ![miku86](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/miku86/32/95580_2.png) [@miku86](https://forum.freecodecamp.org/u/miku86)\
**Post date:** [July 14, 2020, 7:58am UTC](https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463/3 "2020-07-14T07:58:46Z")

</div>

Hey there,

`setState` is an async function.

If you want to run a function after a `setState` call, you can read about it here:

> **[React.Component – React](https://legacy.reactjs.org/docs/react-component.html)**
>
> A JavaScript library for building user interfaces

> `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 […] a `setState` callback ( `setState(updater, callback)` ), either of which are guaranteed to fire after the update has been applied.

Keep us posted if you can use this information to update your code.

---

<div class="post-metadata">

**Author:** ![Zie62](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zie62/32/148488_2.png) [@Zie62](https://forum.freecodecamp.org/u/Zie62)\
**Post date:** [July 14, 2020, 5:23pm UTC](https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463/4 "2020-07-14T17:23:16Z")

</div>

I tried both using componentDidMount as well as a setState callback but both seem to work the same as what I had before. Currently have it not using DidMount, just a setState callback and I had the exact same issue with both.

---

<div class="post-metadata">

**Author:** ![miku86](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/miku86/32/95580_2.png) [@miku86](https://forum.freecodecamp.org/u/miku86)\
**Post date:** [July 15, 2020, 7:32am UTC](https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463/5 "2020-07-15T07:32:42Z")

</div>

Because I don’t know what your specific problem is, I can’t test it.

But my best guess:

You are currently using this code:

```auto
    this.setState({
      pushed: '0'
    }, this.handleNums()) 

```

Try to use it like this:

```auto
    this.setState({
      pushed: '0'
    }, () => this.handleNums()) 

```

---

<div class="post-metadata">

**Author:** ![Zie62](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zie62/32/148488_2.png) [@Zie62](https://forum.freecodecamp.org/u/Zie62)\
**Post date:** [July 15, 2020, 4:45pm UTC](https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463/6 "2020-07-15T16:45:38Z")

</div>

Aha, that solved my issue perfectly. Thank you so much for the help 😃

---

<div class="post-metadata">

**Author:** ![miku86](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/miku86/32/95580_2.png) [@miku86](https://forum.freecodecamp.org/u/miku86)\
**Post date:** [July 16, 2020, 8:17am UTC](https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463/7 "2020-07-16T08:17:17Z")

</div>

Awesome,

glad you did it,  
you’re welcome!

I got an idea for you:

- currently you have a lot of duplicate code for handling the button, e.g. `zeroPress`, `onePress` etc. You can refactor them into one function that depends on a parameter.

Example:

Before:

```auto
  zeroPress(){
    this.setState({
      pushed: '0'
    }, () => this.handleNums()) 
  }

```

After:

```auto
  handlePress(numberPressed){
    this.setState({
      pushed: numberPressed
    }, () => this.handleNums()) 
  }

```

You can run it by using `onClick={() => this.handlePress(0)}` for the button `0`.

Let me know if you like this idea.

---

<div class="post-metadata">

**Author:** ![Zie62](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zie62/32/148488_2.png) [@Zie62](https://forum.freecodecamp.org/u/Zie62)\
**Post date:** [July 16, 2020, 8:31am UTC](https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463/8 "2020-07-16T08:31:41Z")

</div>

Dude! You are my ReactJS saviour! I was hoping I could do it without making a bunch of functions but couldn’t think of how because I didn’t quite understand arrow notation, but the 2 tips you’ve given me has at least let me understand the very simple use for a nameless function. Gonna trim it down later as well as trim down my operator handling syntax using the same premise 👍👍 glad you told me this before I did the same thing for those.

---

<div class="post-metadata">

**Author:** ![miku86](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/miku86/32/95580_2.png) [@miku86](https://forum.freecodecamp.org/u/miku86)\
**Post date:** [July 17, 2020, 7:01am UTC](https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463/9 "2020-07-17T07:01:04Z")

</div>

Thank you, I’m very flattered!

Let me know when you need some ideas how to handle stuff!

---

<div class="post-metadata">

**Author:** ![Zie62](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zie62/32/148488_2.png) [@Zie62](https://forum.freecodecamp.org/u/Zie62)\
**Post date:** [July 21, 2020, 4:42pm UTC](https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463/10 "2020-07-21T16:42:31Z")

</div>

So I got a few more kinks worked out but now im realizing a huge problem, I am having a lot of trouble integrating the minus button working as a negative sign in specific scenarios. I tried setting it up so if another operator was in use it would make the input negative, however it blocked me out of using the minus operator any time after the first calculation if i wanted to do a chain. Also, it seems Javascript automatically takes 5.0 and gets rid of the decimal which I dont want it to do for a test case. Any ideas how to tackle these two problems?

---

<div class="post-metadata">

**Author:** ![miku86](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/miku86/32/95580_2.png) [@miku86](https://forum.freecodecamp.org/u/miku86)\
**Post date:** [July 23, 2020, 6:34am UTC](https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463/11 "2020-07-23T06:34:27Z")

</div>

First, I would separate both problems:

1. the “minus sign”-issue
2. the “gets rid of decimals”-issue

Let’s have a look at #1:

- can you find examples for the problem?
- can you describe the _exact_ problem?

---

<div class="post-metadata">

**Author:** ![Zie62](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zie62/32/148488_2.png) [@Zie62](https://forum.freecodecamp.org/u/Zie62)\
**Post date:** [July 23, 2020, 4:25pm UTC](https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463/12 "2020-07-23T16:25:14Z")

</div>

An example is: If i do 5 \* -9, it works perfectly no problem. If i do 5 \* 9 - 4, the -4 will end up multiplying as -4 because i currently have it so it cannot change operators to subtract if this.state.operator != “placeholder”. I think I can put a line in where it would replace operator with placeholder after each subsequent calculation, but I’m having trouble thinking of where I could place it such that the previous operation would not be affected.

---

<div class="post-metadata">

**Author:** ![miku86](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/miku86/32/95580_2.png) [@miku86](https://forum.freecodecamp.org/u/miku86)\
**Post date:** [July 27, 2020, 10:46am UTC](https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463/13 "2020-07-27T10:46:03Z")

</div>

Hey there,

did you make some progress on that?

If you would have to explain to a kid how to calculate `5 * 9 - 4` by hand, how would you do it?

---

<div class="post-metadata">

**Author:** ![Zie62](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/zie62/32/148488_2.png) [@Zie62](https://forum.freecodecamp.org/u/Zie62)\
**Post date:** [July 27, 2020, 4:23pm UTC](https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463/14 "2020-07-27T16:23:18Z")

</div>

Been getting caught up in family matters so havent been working on it (starting back up tomorrow) but I think im going to re-write the program and try to implement it in the program while coding rather than going back and changing a bunch of stuff. I’ve learned a lot about general coding practices by your examples as well as my own thinking through of things so I think its gonna be easier that way.

---

<div class="post-metadata">

**Author:** ![miku86](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/miku86/32/95580_2.png) [@miku86](https://forum.freecodecamp.org/u/miku86)\
**Post date:** [July 27, 2020, 6:58pm UTC](https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463/15 "2020-07-27T18:58:54Z")

</div>

Glad it helped 🙂

Keep us posted!

---

<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:45pm UTC](https://forum.freecodecamp.org/t/reactjs-wrong-execution-order-on-calculator/409463/16 "2021-06-01T18:45:21Z")

</div>

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


