jsx-eslint / eslint-plugin-react

React-specific linting rules for ESLint
MIT License
8.95k stars 2.77k forks source link

Rule idea: no-mutation-props #1113

Open ianschmitz opened 7 years ago

ianschmitz commented 7 years ago

A component is never meant to modify its own props (https://facebook.github.io/react/docs/components-and-props.html#props-are-read-only).

I suggest we had a no-mutation-props rule to check for mutations of a components own props. It looks like we could likely copy the logic used in no-direct-mutation-state.js.

viankakrisna commented 7 years ago

maybe just go ahead and send a pr?

ljharb commented 7 years ago

Seems reasonable. However, this rule would have to warn any time the entire props object is passed to a function (which is a bad practice anyways) because that makes it impossible (in most cases) to determine if it's being mutated.

pgangwani commented 5 years ago

How can I achieve "no-mutation-props" in my eslint config ?

ljharb commented 5 years ago

@pgangwani #1416 isn’t merged yet.

pgangwani commented 5 years ago

I desperately need this. Got bunch of issues and instance where Devs have written this.props.someKey = "somevalue" in render functions.

Who can merge and by when ?

ljharb commented 5 years ago

You can read the PR to find out the challenges.

Really, stopping mutation isn’t something that can be done with static analysis - you can do it in your tests more reliably.