Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

useResponsive giving error #2263

Closed
narasimhajupally opened this issue Jul 23, 2023 · 10 comments
Closed

useResponsive giving error #2263

narasimhajupally opened this issue Jul 23, 2023 · 10 comments
Assignees

Comments

@narasimhajupally
Copy link

not able to use useResponsive in next js
the error in the console

./hooks/use-device-type.ts:1:0
Module not found: Can't resolve 'ahooks/es/configResponsive'
> 1 | import { configResponsive, useResponsive } from "ahooks";
  2 | // import { useMediaQuery } from "react-responsive";
  3 | configResponsive({
  4 |     isMobile: 0,

https://nextjs.org/docs/messages/module-not-found

Import trace for requested module:
./components/devices.tsx
./components/nav.tsx
./layouts/authenticated-layout.tsx
./pages/_app.tsx

other hooks are running fine.

@liuyib
Copy link
Collaborator

liuyib commented Jul 23, 2023

similar: #2254


hi @narasimhajupally, you can solve this by:

// ❌ error
import { configResponsive, useResponsive } from "ahooks";

// ✅ work
import { configResponsive, useResponsive } from "ahooks/es";
// ✅ work
import { configResponsive, useResponsive } from "ahooks/es/useResponsive";
// ✅ work
import { configResponsive, useResponsive } from "ahooks/lib";
// or
// ✅ work
import { configResponsive, useResponsive } from "ahooks/lib/useResponsive";

so, why can't we import hook from ahooks in next.js, I will keep track of this problem. thx for your feedback.

@liuyib liuyib self-assigned this Jul 23, 2023
@narasimhajupally
Copy link
Author

@liuyib seems to be working on this. shouldn't this issue be open?

@liuyib liuyib reopened this Jul 24, 2023
@liuyib
Copy link
Collaborator

liuyib commented Jul 24, 2023

@meteorlxy
Copy link

meteorlxy commented Aug 25, 2023

Related: vercel/next.js#53051

Maybe need another special transform for useResponsive? Just like createUpdateEffect in that PR

@liuyib
Copy link
Collaborator

liuyib commented Aug 25, 2023

Maybe need another special transform for useResponsive? Just like createUpdateEffect in that PR

Nice catch! I'll try to find out what happened and let you known.

@liuyib
Copy link
Collaborator

liuyib commented Aug 28, 2023

The root cause is that Next.js included ahooks in the transform rules (modularizeImports), but some methods were not given special treatment.

Thanks @meteorlxy for the helpful suggestion.

@liuyib
Copy link
Collaborator

liuyib commented Oct 23, 2023

@PrinOrange
Copy link

Now is 2024 this problem still lingered and not solved yet.

@liuyib
Copy link
Collaborator

liuyib commented Jan 8, 2024

Now is 2024 this problem still lingered and not solved yet.

It's the problem for Next.js, we can do nothing.
Can you try this: #2395 (comment), and verify whether this problem has been solved? @PrinOrange Thank you.

@liuyib
Copy link
Collaborator

liuyib commented Jun 3, 2024

This issue has been solved in Next.js >=14.0.4: https://github.com/vercel/next.js/pull/59294/files

@liuyib liuyib closed this as completed Jun 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants