Course contentsShow
← React
Lesson 704 of 2,39619. useRefFree lesson

Reading and Writing ref.current

Access ref.current to read the value; assign to ref.current to update it—no setter function needed.

Reading and Writing ref.current

What you'll learn: How to access and update the value stored inside a ref using the .current property.


Unlike state, which requires setState to update, refs give you direct access to their value through the .current property. You can both read from it and write to it directly—no special functions needed.

The .current Property

When you create a ref with useRef(initialValue), React gives you an object with a single property called current. This property holds whatever value you passed in (or undefined if you didn't pass anything).

const countRef = useRef(0);

// Reading
console.log(countRef.current); // 0

// Writing
countRef.current = 5;
console.log(countRef.current); // 5

// Writing again
countRef.current = countRef.current + 1;
console.log(countRef.current); // 6

No Setter Function Required

This is fundamentally different from state. With useState, you must call setCount(newValue). With refs, you just assign directly:

// State: must use setter
const [count, setCount] = useState(0);
setCount(5); // ✓ correct
count = 5;   // ✗ wrong! won't work

// Ref: direct assignment
const countRef = useRef(0);
countRef.current = 5; // ✓ correct

Think of ref.current like a box you can open, look inside, and replace the contents anytime you want—without telling React you did it.

Always Access Through .current

Never try to reassign the ref itself. You're mutating the contents of the box, not replacing the box:

countRef.current = 10; // ✓ correct
countRef = { current: 10 }; // ✗ wrong!

Key Takeaway: Access a ref's value by reading ref.current; update it by assigning to ref.current directly—React doesn't need to know about these changes.