customHooks module.exports 不使用 es6 样式导入

customHooks module.exports not importing with es6 style

我有一个应用程序 运行 使用相同的架构,它没有问题。我正在尝试使用 React 17.0.2 在 CRA 中进行复制。这只是一个文件,使用 module.exports 以有组织的方式保存我的自定义挂钩。这是我的文件:customHooks.js

import { useCallback, useEffect, useRef, useState } from "react";

module.exports = {
    usePrevious: (stateValue) => {
        const ref = useRef();
        useEffect(() => {
            ref.current = stateValue;
        })
        return ref.current
    },
    getUrlVars: (url) => {
        let vars = {};
        if (url) {
            let parts = url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
                vars[key] = value;
            });
        } else {
            console.error('No url specified')
        }

        return vars;
    },
    setCookieManually: async (cname, cvalue, exdays) => {
        const d = new Date();
        d.setTime(d.getTime() + (exdays*24*60*60*1000));
        let expires = "expires="+ d.toUTCString();
        document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
    },
    removeCookieManually: async (cname) => {
        document.cookie = `${cname}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`
    },
    useSetState: (initialValue) => {
        let [val, setVal] = useState(initialValue);
        let cbRef = useRef(null);
        let first = useRef(true);

        useEffect(() => {
            if (first.current) {
                first.current = false;
                return;
            }

            if (typeof cbRef.current === "function") {
                console.log("calling cb");
                cbRef.current();
            }
        }, [val]);

        let setValCB = useCallback((newVal, cb) => {
            console.log("setValCB", newVal);
            cbRef.current = cb;
            setVal(newVal);
        }, []);

        /*
        * USAGE:
        * let [selected, setSelected] = useSetState("");
        *
        * setSelected(title, () => {
        *   console.log("onRowSelected", title);
        *   props.onRowSelected(title);
        * });
        *
        * */

        return [val, setValCB];
    }
}

我只是想像这样导入:

import { usePrevious } from '..customHooks'

但我一直收到错误消息:Attempted import error: 'usePrevious' is not exported from '../customHooks.js'

注意:我已经用 NextJs 项目成功地完成了这个没问题。

让我知道您认为问题出在哪里。

我尝试将其从 import 更改为 require() 并解决了错误,但随后显示: TypeError: Cannot assign to read only property 'exports' of object '#<Object>'

这是什么意思?

谢谢!

您是否尝试过使用默认导入而不是命名?因为您要导出的文件不是命名导出文件,而是默认文件。

所以你可以试试这个,

import usePrevious from '..customHooks'

更新:节点版本 14 及之后可能 运行 进入这个问题


要解决此问题,必须删除 module.exports。似乎在节点 14 之后事情变得很奇怪。

现在我必须先写出每个函数,然后 export 该函数在结构中。


import { useCallback, useEffect, useRef, useState } from "react";

   const usePrevious = (stateValue) => {
        const ref = useRef();
        useEffect(() => {
            ref.current = stateValue;
        })
        return ref.current
    }

   const getUrlVars = (url) => {
        let vars = {};
        if (url) {
            let parts = url.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
                vars[key] = value;
            });
        } else {
            console.error('No url specified')
        }

        return vars;
    }

   const setCookieManually = async (cname, cvalue, exdays) => {
        const d = new Date();
        d.setTime(d.getTime() + (exdays*24*60*60*1000));
        let expires = "expires="+ d.toUTCString();
        document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
    }

   const removeCookieManually = async (cname) => {
        document.cookie = `${cname}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`
    }

   const useSetState = (initialValue) => {
        let [val, setVal] = useState(initialValue);
        let cbRef = useRef(null);
        let first = useRef(true);

        useEffect(() => {
            if (first.current) {
                first.current = false;
                return;
            }

            if (typeof cbRef.current === "function") {
                console.log("calling cb");
                cbRef.current();
            }
        }, [val]);

        let setValCB = useCallback((newVal, cb) => {
            console.log("setValCB", newVal);
            cbRef.current = cb;
            setVal(newVal);
        }, []);

        /*
        * USAGE:
        * let [selected, setSelected] = useSetState("");
        *
        * setSelected(title, () => {
        *   console.log("onRowSelected", title);
        *   props.onRowSelected(title);
        * });
        *
        * */

        return [val, setValCB];
    }

export {
    usePrevious,
    getUrlVars,
    setCookieManually,
    removeCookieManually,
    useSetState,
}

然后这允许我所有的导入与解构一起工作:

import { usePrevious } from '../utils/customHooks'