hooks icon indicating copy to clipboard operation
hooks copied to clipboard

useResponsive giving error

Open narasimhajupally opened this issue 2 years ago • 9 comments

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.

narasimhajupally avatar Jul 23 '23 08:07 narasimhajupally

similar: https://github.com/alibaba/hooks/issues/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 avatar Jul 23 '23 11:07 liuyib

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

narasimhajupally avatar Jul 24 '23 05:07 narasimhajupally

related:

  • https://github.com/vercel/next.js/issues/52703
  • https://github.com/vercel/next.js/issues/52307#issuecomment-1627372291

refer: https://github.com/vercel/next.js/issues/52307#issuecomment-1627372291, this issue seems to be SWC' problem.

liuyib avatar Jul 24 '23 06:07 liuyib

Related: https://github.com/vercel/next.js/pull/53051

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

meteorlxy avatar Aug 25 '23 13:08 meteorlxy

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 avatar Aug 25 '23 16:08 liuyib

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 avatar Aug 28 '23 07:08 liuyib

mark: https://github.com/mui/material-ui/pull/35457/files

liuyib avatar Oct 23 '23 07:10 liuyib

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

PrinOrange avatar Jan 04 '24 05:01 PrinOrange

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: https://github.com/alibaba/hooks/issues/2395#issuecomment-1869472895, and verify whether this problem has been solved? @PrinOrange Thank you.

liuyib avatar Jan 08 '24 02:01 liuyib

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

liuyib avatar Jun 03 '24 07:06 liuyib