Common Utility Hooks in React

From the React Hooks cheat sheet · Custom Hook Patterns · verified Nov 2026

Common Utility Hooks

Frequently used custom hooks

javascript
// Previous value (state, not a ref - refs can't be read during render)
function usePrevious(value) {
  const [current, setCurrent] = useState(value)
  const [previous, setPrevious] = useState(undefined)
  if (value !== current) {
    setPrevious(current)
    setCurrent(value)
  }
  return previous
}

// Local storage
function useLocalStorage(key, initial) {
  const [value, setValue] = useState(() => {
    const saved = localStorage.getItem(key)
    return saved ? JSON.parse(saved) : initial
  })
  
  useEffect(() => {
    localStorage.setItem(key, JSON.stringify(value))
  }, [key, value])
  
  return [value, setValue]
}

// Toggle
function useToggle(initial = false) {
  const [on, setOn] = useState(initial)
  const toggle = () => setOn(!on)
  return [on, toggle]
}
💡 Extract common logic into custom hooks
✅ Custom hooks can use other hooks
⚡ Share stateful logic, not state itself
📌 Name must start with 'use' + a capital letter (useFetch) so lint rules apply

Continue with React Hooks

Save the full cheat sheet or work through every related task.

Back to the full React Hooks cheat sheet