# Calculator Help. Reset state after equals pressed?

**URL:** <https://forum.freecodecamp.org/t/calculator-help-reset-state-after-equals-pressed/446797>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [February 17, 2021, 9:13am UTC](https://forum.freecodecamp.org/t/calculator-help-reset-state-after-equals-pressed/446797 "2021-02-17T09:13:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rsheppard83](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rsheppard83/32/249067_2.png) [@rsheppard83](https://forum.freecodecamp.org/u/rsheppard83)\
**Post date:** [February 17, 2021, 9:13am UTC](https://forum.freecodecamp.org/t/calculator-help-reset-state-after-equals-pressed/446797/1 "2021-02-17T09:13:25Z")

</div>

Hi guys,

I have been working on my calculator app for a few weeks now having redone it several times. It is by far the hardest project I have done so far but feel I am really close now.

Although I might add some more features, the one problem I am really struggling with that hopefully resolves the last piece of logic is I after a user presses ‘=’ I want the currentNumber and input to reset although still stay on screen (so user can see sum and answer) until user presses a number button.

My logic seems sound to me and I don’t understand why it doesn’t work but am obviously doing something wrong or don’t fully understand how state is working.

So when user presses ‘=’ it currently should set equalPressed state to true.  
After user then presses a number it adjusts state and resets equalPressed to false.

```auto
const [equalPressed, setEqualPressed] = useState(false);

```

```auto
    // calculate current sum in the input state
    const calculateSum = () => {
        setEqualPressed(true);
        let newInput = undefined;
        
        // remove operator if input ends in one
        if (input.endsWith('</span> ')) {
            newInput = input.substring(0, input.length - 35);
            setInput(newInput);
        }
        setCurrentNumber(safeEval(newInput));
    }

```

```auto
    // add new numbers to state
    const appendNumber = num => {
        if (equalPressed) {
            setCurrentNumber(Number(num).toString());
            setInput(num);
            setEqualPressed(false);
        } else {
            setCurrentNumber(Number(`${currentNumber}${num}`).toString());
            setInput(`${input}${num}`); 
        }
    }

```

Here is the full code. Appreciate any help. Full disclosure: The design is based off a design I found on dribbble and wanted to try and recreate.

[https://codesandbox.io/s/github/rsheppard83/calculator](https://codesandbox.io/s/github/rsheppard83/calculator)

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [February 17, 2021, 9:43am UTC](https://forum.freecodecamp.org/t/calculator-help-reset-state-after-equals-pressed/446797/2 "2021-02-17T09:43:30Z")

</div>

After a quick glance, I’d say that it’s because your `setInput` callback is changing value in `App.js` which result in rerendering the button, effectively resetting its state.  
Try to move `equalPressed` into `App.js`.

---

<div class="post-metadata">

**Author:** ![rsheppard83](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/rsheppard83/32/249067_2.png) [@rsheppard83](https://forum.freecodecamp.org/u/rsheppard83)\
**Post date:** [February 17, 2021, 10:40am UTC](https://forum.freecodecamp.org/t/calculator-help-reset-state-after-equals-pressed/446797/3 "2021-02-17T10:40:55Z")

</div>

> **[calculator - CodeSandbox](https://codesandbox.io/s/github/rsheppard83/calculator)**
>
> calculator using @testing-library/jest-dom, @testing-library/react, @testing-library/user-event, react, react-dom, react-html-parser, react-scripts, web-vitals

Thank you so much it works perfectly now. This is likely the reason this project took me sooo long. I had alot of other state in the Button.js component but as they ended up getting shared with other components they naturally moved to App.js and then things that didn’t work before started to work 🙈

Thanks again!

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [August 18, 2021, 10:41pm UTC](https://forum.freecodecamp.org/t/calculator-help-reset-state-after-equals-pressed/446797/4 "2021-08-18T22:41:54Z")

</div>

Closed on August 18, 2021, 10:41pm UTC

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
