Eslint-plugin-react: react/no-direct-mutation-state doesn't catch all cases

Created on 8 Sep 2017  路  8Comments  路  Source: yannickcr/eslint-plugin-react

  const { model } = this.state;
  model.rating = rate;

In this case eslint won't show any errors.

Most helpful comment

I think we figured this out yesterday. Somebody created a PR for not allowing mutation for this.props and added support for this case. Once that PR is finished we'll try to move all the logic over to no-direct-mutation-state rule as well.

https://github.com/yannickcr/eslint-plugin-react/pull/1416

All 8 comments

I think we figured this out yesterday. Somebody created a PR for not allowing mutation for this.props and added support for this case. Once that PR is finished we'll try to move all the logic over to no-direct-mutation-state rule as well.

https://github.com/yannickcr/eslint-plugin-react/pull/1416

is there any news on this? or alternatives that i could use?

@meldafert I just found out the other day about this rule not catching all cases. Ended up marking state immutable via Flow instead, which is perfect for this job:

// ...
type State = $ReadOnly<{| ... |}>
class Smth extends Component<Props, State> {
  // ...
}

@kangax thank you, this is exactly what i needed. (turns out typescript has this too).

@kangax thank you, this is exactly what i needed. (turns out typescript has this too).

@meldafert TypeScript already disallows changing props as they are marked as readonly through typescript typing already (the app will not build if you try to mutate props)

@meldafert TypeScript already disallows changing props as they are marked as readonly through typescript typing already (the app will not build if you try to mutate props)

@maniator True, but only for mutating this.props itself, but not for eg. this.props.list.push(), which is what i'm trying to catch.
(if i declare the prop as list: Readonly<Item[]>, it does prevent me from using push())

@meldafert ahh, so it does not do a _deap_ check.

You can probably also do (in ts)

```typescript
readonly list: Item[],

Was this page helpful?
0 / 5 - 0 ratings