首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >当我使用umd库中的元件时,为什么会出现invalid-hook错误?

当我使用umd库中的元件时,为什么会出现invalid-hook错误?
EN

Stack Overflow用户
提问于 2019-06-12 01:34:45
回答 2查看 251关注 0票数 0

我已经创建了一个简单的Checkbox组件,它使用useEffectuseState来处理组件的选中状态。

我已经将这个组件添加到了我的components集合中(我自己的项目包含所有组件)。但是,如果我想在其他项目中使用该组件,我会收到以下错误消息:https://reactjs.org/docs/error-decoder.html/?invariant=321

如果我取消对useStateuseEffect方法的注释,则该组件可以工作,如果我将整个组件复制到我的项目中(使用useEffect和useState方法!!),它也可以工作。

我尝试过推荐的解决方案(https://reactjs.org/warnings/invalid-hook-call-warning.html),但它不能解决我的问题。

代码语言:javascript
复制
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};
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56549010

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档