react
runtime_error
ai_generated
true
Warning: forwardRef render functions accept exactly two parameters: props and ref. Did you mean to use React.forwardRef?
ID: react/forwardref-params-error
92%Fix Rate
86%Confidence
1Evidence
2023-07-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| [email protected] | active | — | — | — |
| [email protected] | active | — | — | — |
Root Cause
A function component is defined with more than two parameters (e.g., props, ref, extraParam) while using forwardRef, but forwardRef expects exactly two parameters.
generic中文
函数组件在使用 forwardRef 时定义了超过两个参数(例如 props、ref、extraParam),但 forwardRef 期望正好两个参数。
Official Documentation
https://reactjs.org/docs/forwarding-refs.htmlWorkarounds
-
95% success Remove the extra parameter and use props object directly: `const MyComponent = React.forwardRef((props, ref) => { const { extraParam, ...rest } = props; return <div ref={ref}>...</div>; });`
Remove the extra parameter and use props object directly: `const MyComponent = React.forwardRef((props, ref) => { const { extraParam, ...rest } = props; return <div ref={ref}>...</div>; });` -
90% success If you need additional parameters, pass them via props: `<MyComponent extraParam={value} />` and access `props.extraParam` inside the forwardRef function.
If you need additional parameters, pass them via props: `<MyComponent extraParam={value} />` and access `props.extraParam` inside the forwardRef function.
中文步骤
Remove the extra parameter and use props object directly: `const MyComponent = React.forwardRef((props, ref) => { const { extraParam, ...rest } = props; return <div ref={ref}>...</div>; });`If you need additional parameters, pass them via props: `<MyComponent extraParam={value} />` and access `props.extraParam` inside the forwardRef function.
Dead Ends
Common approaches that don't work:
-
Ignoring the warning and passing extra parameters anyway
80% fail
React will ignore the extra parameters, and the component may not behave as expected.
-
Using a class component instead of a function component with forwardRef
50% fail
Class components handle refs differently; you may lose the benefits of forwardRef or cause other errors.
-
Wrapping the component in another HOC that passes extra props
60% fail
This adds complexity without fixing the root cause; the extra parameter is still passed to the forwardRef function.