Skip to content

How to Fix React Custom Hook State Not Shared Between Components

DodaTech Updated 2026-06-24 2 min read

In this fix, you'll learn why two components using the same custom hook have different state, why this matters for shared logic in form validation or data fetching, and how this affects reusable hook libraries in {React.

Every call to a custom hook creates its own independent state. Hooks are not singletons โ€” each component instance that calls the hook gets its own copy of the hook's internal useState and useEffect calls.

The Problem

The following code demonstrates the typical mistake:

function useCounter() {
  const [count, setCount] = useState(0);
  return { count, increment: () => setCount(c => c + 1) };
}
function ComponentA() { const { count } = useCounter(); return <p>A: {count}</p>; }
function ComponentB() { const { count } = useCounter(); return <p>B: {count}</p>; }
// A: 0, B: 0 โ€” independent counters

The Fix

Here is the corrected version:

// To share state, lift it to a common ancestor:
function App() {
  const [count, setCount] = useState(0);
  return (
    <>
      <ComponentA count={count} increment={() => setCount(c => c + 1)} />
      <ComponentB count={count} />
    </>
  );
}
// Both components display the same count

Prevention

  • Custom hooks create isolated state per call โ€” each component instance gets its own state.
  • To share state between components, lift state to a common ancestor and pass down via props.
  • Use React Context inside a custom hook for shared state without prop drilling.
  • For global shared state, consider Zustand, Jotai, or React Context.
  • Document custom hooks clearly โ€” indicate whether they create local state or read shared context.

Common Mistakes with custom hook state

  1. Forgetting deriving (Show, Eq) on custom data types needed for debugging
  2. Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
  3. Using head and tail instead of pattern matching, causing runtime errors on empty lists

These mistakes appear frequently in real-world REACT code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

### When should I use a custom hook instead of a utility function?

Use a custom hook when the logic involves React hooks (useState, useEffect, etc.). For pure functions without React features, use a regular utility function instead. Hooks are just functions that can call other hooks โ€” they run per component instance.

Can two custom hook calls share state through a module-level variable?

Technically yes, but module-level variables don't trigger React re-renders. You'd still need React state somewhere. Use module-level state only for logging or analytics that don't affect the UI.

Does React Query share state across components?

Yes โ€” React Query stores data in an external cache outside component state. Multiple components calling useQuery with the same query key share the same cached data and loading state. This is a good example of shared state via a custom hook pattern.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro