Tutorials Logic, IN info@tutorialslogic.com

React Hook called conditionally Rules of Hooks Fix: Tutorial, Examples, FAQs & Interview Tips

What is This Error?

The "React Hook is called conditionally" error occurs when you call a React Hook inside a conditional statement, loop, or nested function. React requires that Hooks are always called in the same order on every render "” this is one of the fundamental Rules of Hooks.

Common Causes

  • Calling a Hook inside an if/else statement
  • Calling a Hook inside a for or while loop
  • Calling a Hook inside a nested function
  • Calling a Hook after an early return statement
  • Calling a Hook inside a try/catch block

Quick Fix (TL;DR)

Quick Solution

Quick Solution
// ❌ Problem "” Hook inside condition
function App({ isLoggedIn }) {
  if (isLoggedIn) {
    const [user, setUser] = useState(null); // Error!
  }
}

// ✅ Solution "” always call Hook at top level
function App({ isLoggedIn }) {
  const [user, setUser] = useState(null); // Always called
  if (!isLoggedIn) return null; // Condition after hooks
}

Common Scenarios & Solutions

Problem

Problem
function UserProfile({ showDetails }) {
  if (showDetails) {
    const [details, setDetails] = useState({}); // ❌ Conditional hook!
    useEffect(() => { fetchDetails(); }, []); // ❌ Conditional hook!
  }
  return <div>Profile</div>;
}

Solution

Solution
function UserProfile({ showDetails }) {
  // ✅ Always call hooks at the top level
  const [details, setDetails] = useState({});
  
  useEffect(() => {
    if (showDetails) { // ✅ Condition inside the hook
      fetchDetails().then(setDetails);
    }
  }, [showDetails]);

  return <div>{showDetails && <p>{details.bio}</p>}</div>;
}

Problem

Problem
function Dashboard({ user }) {
  if (!user) return <p>Please log in</p>; // Early return
  
  const [data, setData] = useState([]); // ❌ Hook after return!
  useEffect(() => { fetchData(); }, []); // ❌ Hook after return!
  
  return <div>{data.map(item => <p>{item}</p>)}</div>;
}

Solution

Solution
function Dashboard({ user }) {
  // ✅ All hooks BEFORE any return
  const [data, setData] = useState([]);
  
  useEffect(() => {
    if (user) fetchData().then(setData); // Condition inside effect
  }, [user]);
  
  if (!user) return <p>Please log in</p>; // Return AFTER hooks
  
  return <div>{data.map(item => <p>{item}</p>)}</div>;
}

Problem

Problem
function ItemList({ items }) {
  return items.map(item => {
    const [selected, setSelected] = useState(false); // ❌ Hook in loop!
    return <div onClick={() => setSelected(!selected)}>{item}</div>;
  });
}

Solution

Solution
// ✅ Extract to a separate component
function Item({ item }) {
  const [selected, setSelected] = useState(false); // ✅ Top level
  return (
    <div onClick={() => setSelected(!selected)}>
      {item} {selected ? '✓' : ''}
    </div>
  );
}

function ItemList({ items }) {
  return items.map(item => <Item key={item.id} item={item} />);
}

Problem

Problem
function App({ isAdmin }) {
  if (isAdmin) {
    const adminData = useAdminData(); // ❌ Conditional custom hook!
  }
}

Solution

Solution
// ✅ Always call the hook, pass condition as parameter
function App({ isAdmin }) {
  const adminData = useAdminData(isAdmin); // Hook decides internally
}

// Inside useAdminData:
function useAdminData(isAdmin) {
  const [data, setData] = useState(null);
  useEffect(() => {
    if (isAdmin) fetchAdminData().then(setData); // Condition inside
  }, [isAdmin]);
  return data;
}

Best Practices to Avoid This Error

  • Always call Hooks at the top level - Before any conditions, loops, or returns
  • Put conditions inside Hooks - Not the other way around
  • Extract components for loops - Each list item should be its own component
  • Move early returns after all Hooks - All hooks must run before any return
  • Use eslint-plugin-react-hooks - Automatically catches Rules of Hooks violations
  • Pass conditions as parameters - Let custom hooks handle conditions internally
  • Only call Hooks in React functions - Not in regular JS functions or class components

Related Errors

Frequently Asked Questions

React relies on the order of Hook calls to associate state with the correct Hook. If Hooks are called conditionally, the order can change between renders, causing React to mix up which state belongs to which Hook.

Always call the Hook, but put the condition inside it. For useEffect, add the condition inside the callback. For custom hooks, pass the condition as a parameter.

No. Instead, extract the loop body into a separate component and use Hooks inside that component. Each component instance has its own Hook state.

It's an ESLint plugin that enforces the Rules of Hooks. It catches conditional hooks, missing dependencies in useEffect, and other common Hook mistakes before runtime.

No. All Hook calls must come before any return statement. Move your early returns to after all Hook declarations.

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.