menu
techminis

A naukri.com initiative

google-web-stories
Home

>

Programming News

>

🧠Understa...
source image

Dev

2w

read

39

img
dot

Image Credit: Dev

🧠Understanding useRef in React

  • useRef is a React Hook used to create a mutable reference object for persistent values across renders.
  • It allows direct access to DOM elements and does not trigger a re-render when .current value is changed.
  • Key Characteristics of useRef include a mutable .current property, no re-render on change, and persistence across renders.
  • It is safe to mutate ref.current inside event handlers or useEffect, but generally avoided during the rendering phase.
  • Common use cases of useRef include accessing DOM nodes directly and storing mutable values like instance variables.
  • For accessing DOM nodes, useRef is used to create a reference and attach it to a JSX element for direct manipulation.
  • Ref persistent values can be updated without triggering re-renders, making it suitable for managing timers or internal states.
  • useState triggers re-renders on value changes, while useRef does not, making it ideal for non-UI related mutable values.
  • Understanding when to use useRef over useState is crucial for efficient React component development.
  • useRef is recommended for direct DOM access and storing mutable data without impacting UI rendering.

Read Full Article

like

2 Likes

For uninterrupted reading,Ā download the app