1

responsive.js

import { useMediaQuery } from "react-responsive";

export default {
  sm: useMediaQuery({ maxWidth: 768 }),
  md: useMediaQuery({ minWidth: 769, maxWidth: 1024 }),
  lg: useMediaQuery({ minWidth: 1025 }),
};

When I tried to import it in App.js

import {sm,md,lg} from "../../responsive"

error occurs:

export 'lg' (imported as 'lg') was not found in '../../responsive' (possible exports: default)

What's wrong?

update

I find that there is another error:

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)

After I change to below

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 };
1
  • import sm,md,lg from "../../responsive" should work Commented Nov 25, 2021 at 9:50

4 Answers 4

3

You can either use a default export or a named export.

Using the default export, which is what you have in your question.

import responsive from '../../responsive';

console.log(responsive.sm);

Alternatively you could use a named export.

export const sm = "foo";
export const md = "bar";
export const lg = "baz";

And import them via:

import { sm, md, lg } from '../../responsive';

The issue you're encountering about using a hook outside of a React component is because you are using useMediaQuery (a hook) outside of a component, it can be easily remedied by creating a custom, reusable hook.

const useMeasurements = () => {
  return {
    sm: useMediaQuery({ maxWidth: 768 }),
    md: useMediaQuery({ minWidth: 769, maxWidth: 1024 }),
    lg: useMediaQuery({ minWidth: 1025 })
  }
};

Inside of your component you can use it like so:

const MyComponent = () => {
  const { sm, md, lg } = useMeasurements();
};
Sign up to request clarification or add additional context in comments.

1 Comment

please check my update above
0

You must import default export as below:

import sm,md,lg from "../../responsive";

2 Comments

I don't think that's correct. The default export is the object encapsulating these three values. So, OP's syntax is correct.
please check my update above
0

Both of these alternatives should work: import responsive from '../../responsive';

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 };

Regarding the hook errors, here you have the same issue, with answers: Invalid hook call. Hooks can only be called inside of the body of a function component

2 Comments

please check my update above
0

You are calling useMediaQuery hook outside of the function, which is agains the React Hook rule.

So you can achieve this by creating another custom hook and wrap with it.

// useGrid.js
import { useMediaQuery } from "react-responsive";

const useGrid = () => {
    const sm = useMediaQuery({ maxWidth: 768 });
    const md = useMediaQuery({ minWidth: 769, maxWidth: 1024 });
    const lg = useMediaQuery({ minWidth: 1025 });
    return { sm, md, lg }
}
export { useGrid }

Now in your file you can simply use it:

// component.js
import { useGrid } from "./useGrid"

const CustomComponent = () => {
    const { sm, md, lg } = useGrid();
  };

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.