Skip to content

noReactPropAssignments

¥Summary

¥How to configure

biome.json
{
"linter": {
"rules": {
"correctness": {
"noReactPropAssignments": "error"
}
}
}
}

¥Description

禁止向 React 组件赋值 props。

¥Disallow assigning to React component props.

React 的 props 被认为是不可变的,给 props 对象的属性赋值被认为是不好的做法。使用 React 编译器时,这甚至会是一个硬性错误。

¥React’s props are assumed to be immutable, and it is considered bad practice to assign to properties of the props object. When using the React Compiler, this is even a hard error.

¥Examples

¥Invalid

function Foo(props) {
props.bar = "Hello " + props.bar;
return <div>{props.bar}</div>
}
code-block.jsx:2:2 lint/correctness/noReactPropAssignments ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

Mutating component props is not allowed.

1 │ function Foo(props) {
> 2 │ props.bar = “Hello ” + props.bar;
^^^^^
3 │
4 │ return <div>{props.bar}</div>

Consider using a local variable instead.

¥Valid

const Foo = function({bar}) {
bar = "Hello " + bar;
return <div>{bar}</div>
}

¥Related links