jsx-eslint/eslint-plugin-react

Add configuration for no-did-update-set-state to allow usage inside condition

Open

#2 021 ouverte le 18 oct. 2018

Voir sur GitHub
 (6 commentaires) (17 réactions) (0 assignés)JavaScript (2 797 forks)batch import
enhancementhelp wanted

Métriques du dépôt

Stars
 (8 630 stars)
Métriques de merge PR
 (Aucune PR mergée en 30 j)

Description

As @gaearon has mentioned here and other places, using setState inside componenDidUpdate is acceptable, so long as it's within a conditional check.

Does it seem reasonable to allow a configuration option for this rule to allow setState usage within componenDidUpdate, so long as it's within a conditional check of some kind?

For example, this would pass here:

componentDidUpdate() {
  if (!this.state.isEnabled) {
    this.setState({ isEnabled: true })
  }
}

but this would still fail:

componentDidUpdate() {
  this.setState({ isEnabled: true })
}

An issue here would be that it's still not entirely guaranteed that more complex use cases wouldn't still end up in an infinite loop.

Guide contributeur