I've recently jumped into studying react and encountered this trouble. Why can't I use increment operators when changing the state?
function App() {
const [num, setNum] = useState(0);
const changeNum = () => {
setNum(num + 1)
}
return (
<div>
<i className='xi-heart' onClick={changeNum}></i> {num}
</div>
);
}
I want to count the number of clicks. When I use increment operator num++ instead of num + 1, it's not working. have been researching for the answer and found some (How to use the increment operator in React) but still don't understand. You should never mutate the state. Okay. Then How does num++ mutate the state directly but num+1 does not?