Because this component changes frequently, I decided to use useRef to store values (for example, a counter). On an event (such as onclick), the log shows that the number did increment by 1 each time the button was clicked, but the value isn't updated on the screen (inside the div). It's showing 0. What am I missing here?
Intended output: on click button, add() increases counter, and display value in <div>.
const counter = useRef(0);
function add() {
counter.current += 1;
console.log(counter.current); // this shows that the number did increment
}
return (
<div>
<div>{counter.current}</div> {/* this shows '0' */}
<button onClick={() => add()}>Add</button>
</div>
);
useStatein this caseuseState?useStatejust the above comments say