What is useRef in React?
useRef is a hook in React that provides a mutable object called a ref, which can hold a mutable value and persists across renders.
How is useRef different from useState in React?
Unlike useState, useRef doesn’t trigger a re-render when its value changes. It’s often used for mutable values that don’t affect the rendering.
Can useRef be used to hold a mutable value that persists across renders?
Yes, the primary purpose of useRef is to hold mutable values that persist across renders without causing re-renders.
What is the common use case for useRef in React?
A common use case is accessing and interacting with the DOM, as useRef can hold a reference to a DOM element.
Can useRef be used to trigger re-renders in React?
No, changing the value of a ref created with useRef does not trigger a re-render.
Can useRef be used to persist values between function calls?
Yes, useRef values persist across renders, making them suitable for persisting values between function calls without triggering re-renders.
How can you access the current value of a ref created with useRef?
Use myRef.current to access the current value of a ref created with useRef.