Facebook Pixel

Should I store UI state in useRef?

No. If a value should drive what the UI shows, it belongs in useState. Using useRef for UI state is a common mistake, because mutating current does not re-render the component, so the UI never updates.

Verify This Answer

Cross-check this information using these trusted sources:

More FAQs in useRef in React Explained: Beyond Just Accessing the DOM

useRef returns a mutable object with a current property that persists across renders. Mutating current does not trigger a re-render, which is the key difference from useState.

useState triggers a re-render when it changes. useRef does not. Use useState for values that should update the UI; use useRef for values that should persist without updating the UI, like DOM references or timer ids.

Accessing DOM elements like focusing an input, and storing mutable values that should persist across renders without triggering re-renders, like a timer id, a previous value, or a flag for whether the component is mounted.

Still have questions?

Browse all our FAQs or reach out to our support team

Want to upskill yourself?

Our courses are taking a Coffee break, but your curiosity shouldn't. Stay engaged with namastedev linkedin, youtube, discord and other resources while you wait.

0
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.