简体   繁体   English

警告:Function 组件不能在 React 中使用 forwardRef 给出 refs 警告

[英]Warning: Function components cannot be given refs warning in React using forwardRef

I'm new to React and the Hooks.我是 React 和 Hooks 的新手。 I was trying to implement a datepicker from react-datepicker into my component, and it comes with a predetermined input element but I wanted to change it with an icon from FontAwesome so I used his custom-input for it.我试图在我的组件中实现一个来自react-datepicker的日期选择器,它带有一个预定的输入元素,但我想用 FontAwesome 的图标来改变它,所以我使用了他的自定义输入

This is the code这是代码

  const Task = () => {
      const [startDate, setStartDate] = useState(new Date());
      const IconInput = forwardRef(
        ({ value, onClick }, ref) => (
          <FontAwesome icon={faCalendarAlt} className="example-custom-input" onClick={onClick} ref={ref}>
            {value}
          </FontAwesome>
        ),
      );
      return (
        <DatePicker
          selected={startDate}
          onChange={date => setStartDate(date)}
          customInput={<IconInput/>}
        />
      );
    };

The code is working and the input is changed for an icon from FontAwesome but the problem is that I receive a warning in console.代码正在运行,并且从 FontAwesome 更改了图标的输入,但问题是我在控制台中收到警告

How is this happening?这是怎么回事?

also I found this solution but I don't know how is working.我也找到了这个解决方案,但我不知道它是如何工作的。

  const Task = () => {
      const [startDate, setStartDate] = useState(new Date());

      const ref = createRef();

      const IconInput = forwardRef(
        ({ value, onClick }, ref) => (
          <FontAwesome icon={faCalendarAlt} className="example-custom-input" onClick={onClick}>
            {value}
          </FontAwesome>
        ),
      );
      return (
        <DatePicker
          selected={startDate}
          onChange={date => setStartDate(date)}
          customInput={<IconInput ref={ref}/>}
        />
      );
    };

I added the ref variable which returns createRef() and putting in the <IconInput/> this ref={ref} then deleting the ref from inside forwardRef() .我添加了返回createRef()ref变量并放入<IconInput/> this ref={ref}然后从forwardRef()内部删除ref This will give me no warnings... But how this works?这不会给我任何警告......但是这是如何工作的?

You should pass the ref you got from the second parameter of React.forwardRef to the FontAwesome component.您应该将从React.forwardRef的第二个参数中获得的 ref 传递给FontAwesome组件。 The problem is that the font awesome library doesn't have a ref prop, it has forwardedRef.问题是字体真棒库没有 ref 道具,它有 forwardedRef。

const IconInput = forwardRef(
    ({ value, onClick }, ref) => (
        <FontAwesome icon={faCalendarAlt} className="example-custom-input" onClick={onClick} forwardedRef={ref}>
            {value}
        </FontAwesome>
    ),
);

The reason for the error you got is that when you pass ref to a class component, it returned the class instance, but in functional component, it has no meaning unless you use forwardRef.您收到错误的原因是,当您将 ref 传递给 class 组件时,它返回了 class 实例,但在功能组件中,除非您使用 forwardRef,否则它没有任何意义。 To help developers, React shows a warning in the console in those cases (passing ref to functional components that doesn't use forwardRef).为了帮助开发人员,React 在这些情况下会在控制台中显示警告(将 ref 传递给不使用 forwardRef 的功能组件)。 When you passed ref to FontAwesome, which is a functional component that doesn't use forwardRef, it showed you this error.当您将 ref 传递给 FontAwesome(一个不使用 forwardRef 的功能组件)时,它会向您显示此错误。 When you removed the ref, it removed the error, but if you'll look, the ref you've got from IconInput is empty.当您删除 ref 时,它删除了错误,但如果您看一下,您从 IconInput 获得的 ref 是空的。

Also, in functional components you should use useRef() instead of createRef , like so:此外,在功能组件中,您应该使用useRef()而不是createRef ,如下所示:

const ref = useRef();

return (
    <DatePicker
        selected={startDate}
        onChange={date => setStartDate(date)}
        customInput={<IconInput ref={ref}/>}
    />
);

And just one last thing - You should move const IconInput =... to be outside of the component, because now you re-create the component itself on every render, which will cause problems.最后一件事 - 您应该将const IconInput =...移到组件之外,因为现在您在每次渲染时重新创建组件本身,这会导致问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 警告 function 组件不能给出参考 - 即使使用带有样式组件的 forwardRef() - Warning with function components cannot be given refs - even though using forwardRef() with Styled Components 反应警告:Function 组件不能给出参考 - React Warning: Function components cannot be given refs 反应:警告:无法为函数组件提供参考。 尝试访问此引用将失败。 你的意思是使用 React.forwardRef() 吗? - React: Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()? 警告:无法为 Function 组件提供参考。 尝试访问此 ref 将失败。 你是想使用 React.forwardRef() 吗? - Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()? 警告:函数组件不能被赋予 refs - Warning: Function components cannot be given refs 理解:警告:Function 组件不能给出参考 - Understanding: Warning: Function components cannot be given refs 使用 react-test-renderer<Switch> 导致“无状态功能组件无法提供参考”警告 - Using react-test-renderer with <Switch> causes "Stateless function components cannot be given refs" warning 使用 forwardRef 并在 function 组件中连接“功能组件不能被赋予 refs” - “Function components cannot be given refs” with forwardRef and connect in function component 在 React 中,我收到警告:无法为函数组件提供 refs。 尝试访问此 ref 将失败。 你的意思是使用 React.forwardRef() 吗? - In React I get the warning: Function component cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()? 不能为 TypeScript &gt; Function 组件提供 refs。 尝试访问此引用将失败。 你的意思是使用 React.forwardRef() 吗? - TypeScript > Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM