Ram01
Ram01

Reputation: 191

react passcode button click

I am trying to solve a react problem. I followed the below youtube tutorial. I am trying in code sandbox. for some reason when I click the button I am getting below error. Can you let me know how to fix it. Providing my code snippet below.

https://codesandbox.io/p/sandbox/passcode-w8dw3s?file=%2Fsrc%2FApp.tsx

https://www.youtube.com/watch?v=x7yoUNV3qJI&list=PL6x5Q-Sj_Bla3_wMqhETxMBjFml0XJNPI&index=9

return (
    <>
      {/* {JSON.stringify(pressedNumbers)} */}
      <div>
        {error}
        {isCorrrect ? (
          <div>success!</div>
        ) : (
          <div className="number-pad">
            {numbers.map((number, idx) => (
              // ...cur needs to be array of numbers
              // idx we don't do any sorting on the array
              <button
                key={idx}
                onClick={(cur) => setPressedNumbers(...cur, number)}
              >
                {number}
              </button>
            ))}
          </div>
        )}
      </div>
    </>
  );

Upvotes: 1

Views: 43

Answers (1)

Steve James
Steve James

Reputation: 362

onClick={() => setPressedNumbers((cur) => [...cur, number])}

Try this.because pressedNumbers is an array but you are directly spreading instead of updating it.

Upvotes: 2

Related Questions