I am new to react and I am experimenting with incrementing a value and storing it into local storage. I was able to write down the following code bellow, however, the last instance of the incremented number is not updated the local storage value. For example, if I press "+1" twice and the number is 10, the dom is updated twice and shows the number 12, but the value stored on local storage is 11. Why is this happening?
import { useState } from 'react'
function About () {
const localStorageValue = parseInt(localStorage.getItem('value'))
const [value, setValue] = useState(localStorageValue);
function remove() {
setValue((add) => add - 1)
localStorage.setItem('value', parseInt(value))
}
function add() {
setValue((add) => add + 1)
localStorage.setItem('value', parseInt(value))
}
return (
<>
<h1>About page</h1>
<button onClick={remove}>-1</button>
{value}
<button onClick={add}>+1</button>
</>
)
}
export default About;
