flâneur — a map of the web's best reading

React useRef Hook By Example: A Complete Guide

learnreact.design · 5,111 words · saved by 1 readers

The React.useRef Hook is used for referencing DOM nodes and persisting a mutalbe value across rerenders. This is an interactive guide to useRef with real-world examples.

React useRef Hook By Example: A Complete Guide React useRef Hook By Example: A Complete Guide Linton Ye Last Updated: 2021-03-07 table of contents What is useRef used for? Directly access DOM nodes Persist a mutable value across rerenders The mental model of useRef The "rerendering time loop" Component instances Example 1: previous value / state Example 2: stopwatch (clear interval) Example 3: Press and hold to repeat Anti-patterns Assign to ref.current in render Use ref.current in JSX and expect it to be reactive Quiz & Related concepts ref and useRef useRef vs. useState useRef vs. createRef

Explore this link on the map →

related reading