I would probably do something like this:
function Counter() { const [count, setCount] = React.useState(0); const [,forceUpdate] = React.useState({}); const countRef = React.useRef(0); return ( <div> <p>State Count: {count}</p> <button onClick={() => setCount(count + 1)}> Increment State Count </button> <p>Ref Count: {countRef.current}</p> <button onClick={() => { countRef.current = countRef.current + 1; forceUpdate({}); }> Increment Ref Count </button> </div> ) }
just in case someone wants a workaround of ref in React. I personally prefer using ref.
ref
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I would probably do something like this:
just in case someone wants a workaround of
ref
in React. I personally prefer usingref
.