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 a year ago
- Comments: 15
I’m also experiencing this with a Remix application.
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.
@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:
I have a similar error and I am using nextjs
13.4.1
.Hey all!
The error you are seeing:
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 UIAnd 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
Closing fixed in
v13.4.1
.@RobinMalfait I think you might be missing the forest for all the trees here, the important line there is probably the last one:
The first part is probably just a warning coming from somewhere else in the code.
@ignaciolarranaga you may also need to remove
'use client'
from components imported in thepages
directory. I added the'use client'
before migration to prep for the changes.