The useRef is a hook that allows us to persist values across renders without causing a re-render when the value changes. It’s commonly used for two main purposes:
Accessing DOM Elements: We can use useRef to store a reference to a DOM element and directly interact with it (e.g., focusing an input field).
Persisting Mutable Values: We can store values (like flags or previous state) that we want to persist across renders, but without triggering a re-render when they change.
The value we store in useRef is accessible through ref.current.
const myRef = useRef(initialValue);
initialValue: This is the initial value we want to assign to the ref (it can be any data type: object, array, number, etc.).
myRef.current: We access the actual value using .current
useState: Triggers a re-render when the state value changes.
useRef: Does not trigger a re-render when the value changes.
export default function useIsMountedRef() { const isMounted = useRef(true); useEffect( () => () => { isMounted.current = false; }, [] ); return isMounted; }
useRef(true) initializes isMounted with the value true.
-> isMounted.current = true means the component is mounted when the component first renders.
Inside useEffect, the cleanup function (() => { isMounted.current = false; }) sets isMounted.current to false when the component unmounts (because useEffect with an empty dependency array [] only runs once — after the initial mount and cleanup on unmount).
So, useRef(true) tracks whether the component is mounted:
It starts with true (mounted).
Once the component unmounts, it sets the value to false.
If we’re doing something asynchronous, like an API call, and we want to avoid updating state or doing something after the component has unmounted (because that could lead to errors), we can check if the component is still mounted using this ref.
const mounted = useRef(false); useEffect(() => { if (!mounted.current) { mounted.current = true; } else { // code } }, [dependency]);
useRef(false) initializes the mounted ref to false. This is tracking whether the effect has run before.
Inside the useEffect:
This pattern can be useful for skipping certain code on the first render (like an initialization phase) and only running it on subsequent renders.
For example:
useRef keeps a mutable value across renders, but it doesn’t cause re-renders when that value changes.
In code 1, it tracks whether a component is mounted or not.
In code 2, it tracks whether the effect has already run once, allowing us to skip code on the first render.
Work with our skilled React developers to accelerate your project and boost its performance.
Hire Reactjs Developers