headlessui: cannot read properties of null (reading 'useRef') next v13.4.0: Vercel

When upgrading to next 13.4.0 compilation on vercel fails with the following message.



Error occurred prerendering page "/pt-PT/open-source". Read more: https://nextjs.org/docs/messages/prerender-error
--
07:54:26.914 | TypeError: Cannot read properties of null (reading 'useRef')
07:54:26.915 | at exports.useRef (/vercel/path1/node_modules/react/cjs/react.production.min.js:25:337)
07:54:26.915 | at De (file:///vercel/path1/node_modules/@headlessui/react/dist/components/popover/popover.js:1:2827)


About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 17 (1 by maintainers)

Most upvoted comments

I’m also experiencing this with a Remix application.

I have a similar error and I am using nextjs 13.4.1.

TypeError: Cannot read properties of null (reading ‘useRef’) at useRef (/Users/michael/project/node_modules/react/cjs/react.development.js:1630:21) at Ne (file:///Users/michael/project/node_modules/@headlessui/react/dist/components/tr

Update: after a bit more searching, I discovered there are actually a ton of reports of related whack-a-mole issues that seemingly affect different libraries however are all rooted in NextJS v 13.4.x.

Despite issue reports getting closed the underlying problem does not seem to be resolved, including in the latest release 13.4.4.

For now I’ve set the following version in package.json and my build works fine:

"next": "~13.3.4",

For more info check out https://github.com/vercel/next.js/issues/49355 and the web of linked issues and comments I picked 13.3.4 because of this comment: https://github.com/vercel/next.js/issues/49355#issuecomment-1538406708

As lovely as NextJS is and for all the lifting and freebies that Vercel is willing to provide to us devs, I think the endless parade of paradigm shifts and lock-in strategies create a fertile environment for these sorts of issues.

Hey, there. I’m getting the same error after upgrading to Next.js 14. It is not clear to me how this issue was fixed. I would appreciate any help.

"@headlessui/react": "1.7.19",
"next": "14.2.4",
"react": "18.3.1",
"react-dom": "18.3.1",
"eslint-config-next": "14.2.4",

This worked for me.

Typically it means that multiple React versions are installed. You can try a few things:

Run npm ls react and see if you see different versions or not Remove node_modules, then reinstall dependencies using npm install (then restart your dev server) Remove the .next folder which could include stale caches (then restart your dev server)

If non of these work, can you open an issue with a minimal reproduction repo attached that shows the issue?

Robin from Tailwind CSS Discord server.

Typically happens when you break the rules of hooks, but the more common reason I’ve seen in practice is that you have multiple React instances on the page. […]

@RobinMalfait I think you might be missing the forest for all the trees here, the important line there is probably the last one:

- error TypeError: Cannot read properties of null (reading 'useRef')

The first part is probably just a warning coming from somewhere else in the code.

Hey all!

The error you are seeing:

Warning: Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons:
1. You might have mismatching versions of React and the renderer (such as React DOM)
2. You might be breaking the Rules of Hooks
3. You might have more than one copy of React in the same app
See https://reactjs.org/link/invalid-hook-call for tips about how to debug and fix this problem.

- error TypeError: Cannot read properties of null (reading 'useRef')

Typically happens when you break the rules of hooks, but the more common reason I’ve seen in practice is that you have multiple React instances on the page.

Headless UI itself doesn’t break the rules of hooks and doesn’t ship with multiple React instances so I’m 99.99% certain this isn’t a Headless UI bug itself.

But, I wanted to see if I could reproduce it using the latest version of Next.js and Headless UI. Trying to reproduce this myself but I cannot reproduce the bug using the latest Next.js version. (For context, Next.js recently solved an issue where multiple React instances were on the page: https://github.com/vercel/next.js/issues/53110)

I created a repository with Next.js, the app/ router and Headless UI

And deployed it to Vercel:

The local development (next dev) and a production build (deployed to Vercel) work as expected without errors.

Going to close this issue for now, since I don’t think there is a bug in Headless UI itself (because we don’t break the rules of hooks nor ship multiple React versions).

If you are still running into this issue, please open a new issue with a minimal reproduction repo attached that showcases the issue. Also double check that you are using the latest Next.js version, if not try to bump that first.

Also feel free to use this base setup to get you a head start: https://github.com/RobinMalfait/next-headlessui-popover

@j-mendez some additional info on this one?, I think I have the same problem just in a different place with version 13.4.7:

2023-06-26T16:40:01.218Z	546e7a1f-5ed3-4b39-9fb3-ec02e225af14	ERROR	TypeError: Cannot read properties of null (reading 'useCallback')
    at Object.useCallback (/var/task/node_modules/react/cjs/react.development.js:1646:21)
    at InsertedHTML (/var/task/node_modules/next/dist/server/app-render/app-render.js:861:52)
    at Je (/var/task/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.edge.production.min.js:121:272)
    at Z (/var/task/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.edge.production.min.js:128:91)
    at Ee (/var/task/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.edge.production.min.js:137:422)
    at AsyncLocalStorage.run (node:async_hooks:338:14)
    at Timeout._onTimeout (/var/task/node_modules/next/dist/compiled/react-dom/cjs/react-dom-server.edge.production.min.js:150:403)
    at listOnTimeout (node:internal/timers:569:17)
    at process.processTimers (node:internal/timers:512:7)

Closing fixed in v13.4.1.

@ignaciolarranaga you may also need to remove 'use client' from components imported in the pages directory. I added the 'use client' before migration to prep for the changes.

I’m encountering this using "@headlessui/react": "^1.7.15", and "next": "13.4.3". Oddly, I am able to npm run build locally. I only encounter this error when the vercel CI pipeline runs npm run build.

Rolling back to "next": "~12.3.4" resolved the issue.