Course contentsShow
← React
Lesson 494 of 2,39614. Uncontrolled ComponentsFree lesson

When Refs Become Available

The ref.current property is null during initial render and populated after the component mounts to the DOM.

When Refs Become Available

You'll learn: Why ref.current starts as null and when it actually points to the DOM element.

When you create a ref with useRef() and attach it to an element, React doesn't fill in the ref.current property immediately. Understanding when refs become available is crucial to avoiding common bugs.

The Timing Problem

During the initial render, your component's JSX is being processed but the actual DOM elements don't exist yet. React is still figuring out what needs to be created. Only after the component mounts (gets added to the real DOM) does the ref.current property get populated with the actual DOM node.

Think of it like this: Imagine ordering furniture online. You get a tracking number (the ref) immediately, but the actual furniture (the DOM element) doesn't arrive at your house until later. You can't sit on the furniture using just the tracking number—you have to wait for delivery.

What This Means in Practice

If you try to access ref.current directly in your component body during render, it will be null the first time:

function MyInput() {
  const inputRef = useRef(null);
  
  console.log(inputRef.current); // null on first render!
  
  return <input ref={inputRef} />;
}

The ref only gets its value after React commits the changes to the DOM. That's why you typically access refs inside event handlers (which run after render) or effects (which run after the DOM is updated).

Key Takeaway: ref.current is null during initial render and only gets populated after the component mounts to the DOM—always account for this timing when accessing refs.