Home > Software design >  how to solve warning Use callback in setState when referencing the previous state
how to solve warning Use callback in setState when referencing the previous state

Time:01-26

For this piece of code, I am getting eslint warning: warning Use callback in setState when referencing the previous state react/no-access-state-in-setstate

how can it be solved?

const sketch = await ImageManipulator.manipulateAsync(this.state.sketch, [{ rotate: 90 }], {
  base64: true,
  format: ImageManipulator.SaveFormat.PNG,
})
this.setState({ sketch: sketch.uri })

It is showing a warning for first-line(const sketch =....).

CodePudding user response:

You are getting warning because of this.setState({ sketch: sketch.uri }) line.

You can do something like the below code to remove the lint warning.

const sketch = await ImageManipulator.manipulateAsync(this.state.sketch, [{ rotate: 90 }], {
  base64: true,
  format: ImageManipulator.SaveFormat.PNG,
})
this.setState(() => {
  return {
    sketch: sketch.uri,
  }
})
  •  Tags:  
  • Related