noReactPropAssign
Summary
Section titled “Summary”- Rule available since:
v2.0.0
- Diagnostic Category:
lint/nursery/noReactPropAssign
- This rule doesn’t have a fix.
- The default severity of this rule is information.
- Sources:
- Same as
react-hooks/react-compiler
- Same as
Description
Section titled “Description”Disallow assigning to React component props.
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
Section titled “Examples”Invalid
Section titled “Invalid”function Foo(props) { props.bar = "Hello " + props.bar;
return <div>{props.bar}</div>}
code-block.jsx:2:2 lint/nursery/noReactPropAssign ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
ℹ 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.
const Foo = function({bar}) { bar = "Hello " + bar; return <div>{bar}</div> }
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noReactPropAssign": "error" } } }}