【发布时间】:2022-01-03 14:26:37
【问题描述】:
响应式.js
import { useMediaQuery } from "react-responsive";
export default {
sm: useMediaQuery({ maxWidth: 768 }),
md: useMediaQuery({ minWidth: 769, maxWidth: 1024 }),
lg: useMediaQuery({ minWidth: 1025 }),
};
当我尝试将其导入App.js时
import {sm,md,lg} from "../../responsive"
发生错误:
export 'lg' (imported as 'lg') was not found in '../../responsive' (possible exports: default)
怎么了?
更新
我发现还有一个错误:
Uncaught Error: 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.
at resolveDispatcher (react.development.js:1476)
at Object.useContext (react.development.js:1484)
at useDevice (react-responsive.js:57)
at useMediaQuery (react-responsive.js:95)
at Module../src/responsive.js (responsive.js:3)
我改到下面之后
import { useMediaQuery } from "react-responsive";
const sm = useMediaQuery({ maxWidth: 768 });
const md = useMediaQuery({ minWidth: 769, maxWidth: 1024 });
const lg = useMediaQuery({ minWidth: 1025 });
export { sm, md, lg };
【问题讨论】:
-
import sm,md,lg from "../../responsive"应该可以工作
标签: javascript reactjs react-responsive