react runtime_error ai_generated true

警告:forwardRef 渲染函数只接受两个参数:props 和 ref。您是否打算传递一个 ref?

Warning: forwardRef render functions accept exactly two parameters: props and ref. Did you mean to pass a ref?

ID: react/forwardref-children-prop-warning

其他格式: JSON · Markdown 中文 · English
85%修复率
88%置信度
1证据数
2023-09-15首次发现

版本兼容性

版本状态引入弃用备注
[email protected] active
[email protected] active

根因分析

使用 React.forwardRef 包装的组件在其渲染函数中接收了超过两个参数(例如 props、ref 和额外的第三个参数如 children),这违反了 forwardRef 的签名。

English

A component wrapped in React.forwardRef is receiving more than two arguments (e.g., props, ref, and an extra third argument like children) in its render function, which violates the forwardRef signature.

generic

官方文档

https://react.dev/reference/react/forwardRef

解决方案

  1. Change the forwardRef render function to accept exactly two parameters: props and ref. Access children via props.children.
  2. If using TypeScript, ensure the forwardRef type signature matches: React.forwardRef<RefType, PropsType>((props, ref) => ...)

无效尝试

常见但无效的做法:

  1. Adding a third parameter to the forwardRef render function to accept children explicitly 95% 失败

    React.forwardRef only provides two arguments; any extra parameter will be undefined and the warning persists.

  2. Wrapping the component in another HOC that passes children as a separate prop 80% 失败

    The underlying forwardRef still receives the extra argument; the warning is about the render function signature, not prop structure.

  3. Ignoring the warning as it doesn't break the app immediately 70% 失败

    The warning indicates potential ref forwarding issues that can cause bugs with focus management or third-party libraries.