微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

调用 React 钩子 useRef 和 useEffect 在 forwardRef 回调中使用时会给出无效的钩子错误

如何解决调用 React 钩子 useRef 和 useEffect 在 forwardRef 回调中使用时会给出无效的钩子错误

我在 react-table rowSelection 中使用了以下代码,它给出了以下编译错误

const IndeterminateCheckBox = React.forwardRef(
    ({ indeterminate,...rest },ref) => {
      const defaultRef = React.useRef(null);
      const resolvedRef = ref || defaultRef;

      React.useEffect(() => {
        resolvedRef.current.indeterminate = indeterminate;
      },[resolvedRef,indeterminate]);

      return (
        <>
          <input type="checkBox" ref={resolvedRef} {...rest} />
        </>
      );
    }
  );

React Hook "React.useRef" 不能在回调中调用。 React Hooks 必须在 React 函数组件或自定义 React Hook 函数调用 react-hooks/rules-of-hooks

React Hook "React.useEffect" 不能在回调中调用。 React Hooks 必须在 React 函数组件或自定义 React Hook 函数调用 react-hooks/rules-of-hooks

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。