Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

A React Router Link fails because useContext returns null

ErrorTypeError: t.useContext(...) is null

Short answer

A context value is read through the React instance that created it. If your app and the library (or two federated apps) load two separate copies of react, the library asks a different copy than the one your Router wrote to, and gets null. List the installed copies, then make the bundle use only one.

  • Applies to React 19, react-router 7, webpack 5, npm 10, Node.js 22
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    List every installed copy of react. More than one line with a different path or version is the clue.

    npm ls react react-dom
  2. 2Match what you see, then jump to its fix

    1. 1npm ls react shows react twice, one nested under a libraryTwo copies of react are installed in node_modulesFix
    2. 2npm ls react-dom shows a different version from reactreact-dom and react are different versions or different copiesFix
    3. 3The app is split with Module Federation and each remote has react in its own bundleA federated app loads its own react instead of the shared oneFix
    4. 4Only one component fails and npm ls shows a single copyThe hook is called where the rules of hooks do not allow itFix

What does the error look like?

  • TypeError: t.useContext(...) is null
  • Hooks can only be called inside the body of a function component.

Cause 1 of 4

Two copies of react are installed in node_modules

1Check

A library that lists react as a dependency instead of a peer dependency installs its own copy. The same happens with npm link, where the library folder has its own node_modules.

npm ls react
npm ls react-dom

2Fix

Make the dependency tree resolve to one copy: update or replace the library that bundles its own react, and with npm link point the library at the app's react.

# from the library folder, with myapp and mylib as siblings
npm link ../myapp/node_modules/react

3Verify

Run npm ls react again: one version remains.

npm ls react

Confirmed by react.dev

Did this fix it?

Cause 2 of 4

react-dom and react are different versions or different copies

1Check

Mismatched react and react-dom copies cause hooks to resolve against a different instance.

npm ls react-dom

2Fix

Install matching versions in the app so both resolve to the same react.

npm install react@latest react-dom@latest

3Verify

Restart the dev server and load the route with the Link.

npm ls react react-dom

Confirmed by react.dev

Did this fix it?

Cause 3 of 4

A federated app loads its own react instead of the shared one

1Check

Module Federation shares a library only if it is declared in shared. A singleton allows exactly one version in the share scope, so libraries with global state run as one instance.

grep -n -A6 "shared" webpack.config.js

2Fix

Declare react and react-dom as shared singletons in every host and remote.

new ModuleFederationPlugin({
  shared: {
    react: { singleton: true },
    "react-dom": { singleton: true },
  },
});

3Verify

Rebuild all apps and load the page: the Link renders and no hook error appears.

npx webpack --mode development

Confirmed by webpack.js.org

Did this fix it?

Cause 4 of 4

The hook is called where the rules of hooks do not allow it

1Check

Run ESLint with eslint-plugin-react-hooks enabled, which flags this. Hooks work only at the top level of a function component or a custom Hook, not in loops, conditions, event handlers or class components.

npx eslint src

2Fix

Move the hook call to the top of the component, before any early return.

function Nav() {
  const theme = useContext(ThemeContext); // top level
  if (!theme) return null;
  return <nav />;
}

3Verify

Run the lint command again and reload the page.

npx eslint src

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Run npm ls react after every dependency update.
  • Treat react as a peer dependency in any shared library you publish.
  • In federated setups, share react and react-dom as singletons in every build.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.