A React Router Link fails because useContext returns null
TypeError: t.useContext(...) is nullShort 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.
Fix it fast
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-dom2Match what you see, then jump to its fix
- 1
npm ls react shows react twice, one nested under a libraryTwo copies of react are installed in node_modulesFix - 2
npm ls react-dom shows a different version from reactreact-dom and react are different versions or different copiesFix - 3
The 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
Only one component fails and npm ls shows a single copyThe hook is called where the rules of hooks do not allow itFix
- 1
What does the error look like?
TypeError: t.useContext(...) is nullHooks 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-dom2Fix
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/react3Verify
Run npm ls react again: one version remains.
npm ls reactConfirmed by react.dev
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-dom2Fix
Install matching versions in the app so both resolve to the same react.
npm install react@latest react-dom@latest3Verify
Restart the dev server and load the route with the Link.
npm ls react react-domConfirmed by react.dev
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.js2Fix
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 developmentConfirmed by webpack.js.org
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 src2Fix
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 srcConfirmed by react.dev
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Web apps
React shows Attempted import error when importing a component or function
Attempted import error:
The build found the file but not the name you asked for.
2 causes, each with a fixWeb apps
AxiosError Network Error with code ERR_NETWORK in a React app
Network Error
ERR_NETWORK means the request produced no readable response.
4 causes, each with a fixWeb apps
Axios get works with a URL string but ignores the object passed as the second argument
The server log shows a GET with no query string
axios.get takes a URL and an optional config object.
3 causes, each with a fixWeb apps
Can't resolve '../axios' in a React project
Module not found: Error: Can't resolve '../axios'
A path that starts with ./ or ../ is a file path, not the axios package.
3 causes, each with a fixWeb apps
Axios returns 401 from a React app but the login works elsewhere
Error: Request failed with status code 401
A 401 means the server received the request without valid credentials.
3 causes, each with a fixWeb apps
An axios call never finishes when the API server stops answering
code: 'ECONNABORTED'
Axios has no timeout unless you give it one: the default is 0, which means wait forever.
3 causes, each with a fix