我有一个运行相同架构的应用程序,它运行没有问题。我正在尝试使用 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 项目成功地做到了这一点,没问题。
- 有什么我的 IDE 不喜欢的吗?
- 是贝尔吗?
让我知道你认为问题是什么。
我试图将其从 to 更改import
为require()
并解决了错误,但随后说:
TypeError: Cannot assign to read only property 'exports' of object '#<Object>'
这是什么意思?
谢谢!