DEV Community

Discussion on: React Hooks: Implementation of useState

Collapse
 
punitkmryh_93 profile image
Punitkumar Y Harsoor
randomNumber = () => {              
    this.setState({
//random number to 10 
      n: (Math.floor(Math.random() * 10))
    })
  }
  • passing state object directly as an argument to setState is the proper way to use the function. You may WANT to change it with update function as an argument by returning state object. As it said to bad coding in react.
Collapse
 
punitkmryh_93 profile image
Punitkumar Y Harsoor
randomNumber = () => {              
    this.setState((prevState)=>{
      {
//random number to 10 
          n: (Math.floor(Math.random() * 10))
     }
  })
}

Since its an asynchronous function you may face error in future.