简体   繁体   English

无法解析 React.js 中找不到的模块

[英]Can't resolve module not found in React.js

I can resolve this issue.我可以解决这个问题。 I have tried relative path but i am still getting the same error.我已经尝试过相对路径,但我仍然遇到同样的错误。

Compiled with problems:X

ERROR in ./src/containers/Home/HomePage.jsx 6:0-48

Module not found: Error: Can't resolve './hooks/useMedia' in 'C:\Users\{user}\Desktop\Hackathon-website-template\src\containers\Home'

the error:错误:

import {UseMedia} from 'hooks/useMedia';

the file being called :被调用的文件:

import { useEffect } from 'react';

export const UseMedia = (query: string, val: number, setMediaQuery: any) => {
  const queryStr = `(${query}: ${val}px)`;

  useEffect(() => {
    const mediaWatcher = window.matchMedia(queryStr);
    mediaWatcher.matches ? setMediaQuery(true) : setMediaQuery(false);

    function updateMediaWatcher(e: any) {
      e.matches ? setMediaQuery(true) : setMediaQuery(false);
    }
    mediaWatcher.addEventListener('change', updateMediaWatcher);

    return function cleanup() {
      mediaWatcher.removeEventListener('change', updateMediaWatcher);
    };
  });
};

in resolving the issue, I have tried import {UseMedia} from './hooks/useMedia';在解决这个问题时,我尝试了import {UseMedia} from './hooks/useMedia'; as well as import {UseMedia} from './src/hooks/useMedia';以及import {UseMedia} from './src/hooks/useMedia'; and I still have the same issue我仍然有同样的问题

If the structure of your repo is standard, You should be in a component or screen folder.如果你的 repo 的结构是标准的,你应该在一个组件或屏幕文件夹中。 But your target hook is in the "hooks" folder.但是您的目标挂钩位于“挂钩”文件夹中。 So you have to go back in the directory for one step and import your hook.所以你必须回到目录中一步并导入你的钩子。 it should work for you:它应该适合你:

import {UseMedia} from '../hooks/useMedia'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM