0

I am using ESlint in my react project.

I have to update states and local variables when props are updated.

ESlint and React ask me not to use setState in:

  • componentDidMount
  • componentWillUpdate
  • componentDidUpdate
  • getSnapshotBeforeUpdate

I can use only getDerivedStateFromProps to update state, but I can't use it to use and update local variables.

And I can use componentWillReceiveProps(nextProps) to update state and local variables but it doesn't update them in the first loaded.

Is there any good solution to update them?

Diamond
  • 3,470
  • 2
  • 19
  • 39
  • 1
    Syncing props and local state is generally considered an anti-pattern. You would rather use a state management system like redux, to sync your "states". Could you maybe elaborate more on the actual problem you're trying to fix, since syncing the state like you're planning to, will quickly become a pain in the ass. – Tobias Tengler Jun 20 '19 at 18:26
  • Well, `nextProps` are from `mobx` store. I have to init and update `highcharts` and its controllers (`zoom in/out `: they are `state` values) when `props` are changed. – Diamond Jun 20 '19 at 18:36
  • 1
    I agree with @TobiasTengler , please explain why you need to use props to update local state because there is a fair chance there is a better way – Chris Sandvik Jun 20 '19 at 18:58
  • I agree with TobiasTengler and ChrisSandvik. Usually I find SO questions too detailed, but in this one the context is required – GalAbra Jun 30 '19 at 09:12

0 Answers0