Thing is that you can update the current state value by passing the new value to the update function or by passing a callback function. The second technique is safe to use.
Below, is the code for updating the current state value based on the previous state.
import React, { useState } from "react";
const Count = () => {
const [value, setValue] = useState(0);
const increment= () => {
setCount((prevValue) => {
return prevValue + 1;
});
};
const decrement = () => {
setCount((prevValue) => {
return prevValue - 1;
});
};
return (
<div>
<strong>Count: {value}</strong>
<button onClick={increment}>Increment</button>
<button onClick={decrement}>Decrement</button>
</div>
);
}?