Skip to content

noReactForwardRef

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"nursery": {
"noReactForwardRef": "error"
}
}
}
}

¥Description

forwardRef 的使用替换为将 ref 作为 prop 传递。

¥Replaces usages of forwardRef with passing ref as a prop.

在 React 19 中,不再需要 forwardRef 奖项。请将 ref 作为 prop 传递。此规则检测是否使用了 forwardRef API,并建议改用 ref 属性。详情请参阅 官方博客文章

¥In React 19, forwardRef is no longer necessary. Pass ref as a prop instead. This rule detects the usage of the forwardRef API, and it suggests using the prop ref instead. See the official blog post for details.

如果你使用的是 React 18 或更早版本,则应禁用此规则。

¥This rule should be disabled if you are working with React 18 or earlier.

¥Examples

¥Invalid

import { forwardRef } from "react";
const MyInput = forwardRef(function MyInput(props, ref) {
return <input ref={ref} {...props} />;
});
code-block.jsx:3:17 lint/nursery/noReactForwardRef  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Use of forwardRef is detected, which is deprecated.

1 │ import { forwardRef } from “react”;
2 │
> 3 │ const MyInput = forwardRef(function MyInput(props, ref) {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 4 │ return <input ref={ref} {…props} />;
> 5 │ });
^^
6 │

In React 19, ‘forwardRef’ is no longer necessary. Pass ‘ref’ as a prop instead.

Replace the use of forwardRef with passing ref as a prop.

Unsafe fix: Remove the forwardRef() call and receive the ref as a prop.

1 1 import { forwardRef } from “react”;
2 2
3 - const·MyInput·=·forwardRef(function·MyInput(props,·ref)·{
3+ const·MyInput·=·function·MyInput({·ref,·...props·})·{
4 4 return <input ref={ref} {…props} />;
5 - });
5+ };
6 6

import { forwardRef } from "react";
const MyInput = forwardRef((props, ref) => {
return <input ref={ref} {...props} />;
});
code-block.jsx:3:17 lint/nursery/noReactForwardRef  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Use of forwardRef is detected, which is deprecated.

1 │ import { forwardRef } from “react”;
2 │
> 3 │ const MyInput = forwardRef((props, ref) => {
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 4 │ return <input ref={ref} {…props} />;
> 5 │ });
^^
6 │

In React 19, ‘forwardRef’ is no longer necessary. Pass ‘ref’ as a prop instead.

Replace the use of forwardRef with passing ref as a prop.

Unsafe fix: Remove the forwardRef() call and receive the ref as a prop.

1 1 import { forwardRef } from “react”;
2 2
3 - const·MyInput·=·forwardRef((props,·ref)·=>·{
3+ const·MyInput·=·({·ref,·...props·})·=>·{
4 4 return <input ref={ref} {…props} />;
5 - });
5+ };
6 6

¥Valid

function MyInput({ ref, ...props }) {
return <input ref={ref} {...props} />;
}
const MyInput = ({ ref, ...props }) => {
return <input ref={ref} {...props} />;
}

¥Related links