我已经创建了一个简单的Checkbox组件,它使用useEffect
和useState
来处理组件的选中状态。
我已经将这个组件添加到了我的components集合中(我自己的项目包含所有组件)。但是,如果我想在其他项目中使用该组件,我会收到以下错误消息:https://reactjs.org/docs/error-decoder.html/?invariant=321
如果我取消对useState
和useEffect
方法的注释,则该组件可以工作,如果我将整个组件复制到我的项目中(使用useEffect和useState方法!!),它也可以工作。
我尝试过推荐的解决方案(https://reactjs.org/warnings/invalid-hook-call-warning.html),但它不能解决我的问题。
const Checkbox = (props) => {
const [isChecked, setIsChecked] = useState(props.checked); // initialize isChecked with the prop checked (true/false)
useEffect(() => {
if (props.getCheckedState) {
props.getCheckedState(isChecked); // to get the status of isChecked in my parent component
}
}, [isChecked]);
return (
//
// My component
//
);
};
export {Checkbox};
https://stackoverflow.com/questions/56549010
复制相似问题