liveBook · Manning

Current property

As explained in React Hooks in Action: With Suspense and Concurrent Mode

A ref current property is the mutable property on a React ref object that contains its current referenced value or DOM element.

A ref’s current property stores its mutable, current value. useRef initializes it once, then returns the same ref object across renders. section 5.1.2

Use the current property when a value must persist without driving the rendered UI:

  • Assign timer IDs to current so effects and event handlers can cancel active timers without triggering a re-render. section 5.2
  • Let React place DOM element references in current, then use those elements for actions such as focusing a field or reading an uncontrolled input. section 5.3

Persisting a timer ID

const timerRef = useRef(null);\ntimerRef.current = setInterval(() => {\n dispatch({ type: "NEXT_BOOKABLE" });\n}, 3000);\n\nfunction stopPresentation() {\n window.clearInterval(timerRef.current);\n} section 5.2

const ref = useRef(1);

See the minimal useRef call that creates an object with a current property. section 5.1.2

How React Hooks in Action: With Suspense and Concurrent Mode covers it

How deep it goes
Explains initialization, object identity across renders, assignment, and the absence of re-renders.
Examples
Demonstrates stored timer IDs, Presentation Mode, element focus, and uncontrolled text-box values.
What it assumes
Assumes familiarity with React function components and introduces ref storage through useRef.
What you can do after
Apply useRef to persist non-rendering values and access referenced DOM elements directly.
How it connects
Connects useRef with re-rendering, useState, useEffect, timers, DOM elements, and uncontrolled inputs.

Also explained in

Compare how 2 books explain current property →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of React Hooks in Action: With Suspense and Concurrent Mode. The section links point to the parts of the book it draws on.