0

Currently I'm reading the documentation of React and I don't understand what do the following parameters mean syntactically in setState() method:

setState(updater[, callback])

I tried to use Esprima parser to perform syntatical analysis in order to determine what are those but it shows syntactical error for the line of code.

Artem Malchenko
  • 2,320
  • 1
  • 18
  • 39
  • Remove the `[]`... – Nope May 22 '18 at 08:29
  • 1
    `setState({ someItem: true })` or `setState(state => ({counter: state.counter + 1})`. Use the latter when referencing state to derive the next state (it's good practice). You rarely need to use the callback it's for edge cases. – Dominic May 22 '18 at 08:29
  • @Nope: It is stated in the documentation – Isaac May 22 '18 at 08:29
  • 3
    @Isaac In documentation brackets denote optional parameters if the parser can't deal with that, it's not the documentations fault I'd reckon [**https://stackoverflow.com/questions/10925478/how-to-read-api-documentation-for-newbs**](https://stackoverflow.com/questions/10925478/how-to-read-api-documentation-for-newbs) – Nope May 22 '18 at 08:31
  • setState() function is async that's why we pass callback function in setState() function, If you want to update your state you should do - > this.setState({stateName:data}) If you want to update your state and there is another dependancy on that updated state so while doing this this.setState({stateName:data}) this.newData(this.state.stateName); Due to asynch nature it send will old state data in newData(), to make sure updated data pass in newData() , you should do this - > this.setState({stateName:data},()=>{this.newData(this.state.stateName)}) – Amit Chauhan May 22 '18 at 08:58

0 Answers0