React Hooks Pitfalls and Best Practices/Call hooks only from components or custom hooks

Do not call hooks from regular utility functions.

Section: Rules of Hooks

Call hooks only from components or custom hooks

tsx
tsx
function useOnlineStatus() {
  const [isOnline, setIsOnline] = useState(true);
  return isOnline;
}
Explanation

A custom hook is just a function whose body legitimately uses other hooks.

Learn the surrounding workflow

Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.

Related commands

Same sheet · prioritizing Rules of Hooks
Call hooks only at the top level
Do not call hooks in conditions, loops, or nested functions.
OpenIn sheettsxsame section
Enable eslint-plugin-react-hooks
Catch rules-of-hooks and dependency mistakes early.
OpenIn sheetbashsame section
Stale closure pitfall
Effects and callbacks capture values from the render where they were created.
OpenIn sheettsx2 tag match
Do not memoize everything
Memoization has a cost; use it when it solves a concrete problem.
OpenIn sheettsx2 tag match
Memoize objects used in dependencies
Avoid recreating dependency objects every render when identity matters.
OpenIn sheettsx2 tag match
Derive data during render when possible
Avoid effects that only derive data from props/state.
OpenIn sheettsx2 tag match