Skip to content

Rules of Hooks

React

Follow the Rules of Hooks to avoid bugs in React function components.

Official Site
01

Overview

Hooks rules: only call at top level (not in loops/conditions/nested functions). Only call from React function components or custom hooks. Hooks must be called in the same order every render. useState, useEffect, useContext, useMemo, useCallback, useRef.

bash
function Comp({id}){
  const [data,setData]=useState(null);
  useEffect(()=>{fetch(id).then(setData)},[id]);
  // OK: hooks at top level
  if(!data) return <Loading/>;
  return <View data={data}/>;
}
02

Best Practices

Never call hooks conditionally or in loops. Always exhaustive deps in useEffect. Use eslint-plugin-react-hooks. Custom hooks start with use. Hooks compose (use other hooks). State updates may be batched.)

Ready to try Rules of Hooks?

Visit the official site for the latest version and full documentation.

Visit Rules of Hooks