vendredi 17 avril 2020

Why am I getting same value from random function in ReactJS after some time?

I want to generate a random value integer in the range [0, 5] with each click of the button. Initially I get some random values on the console. But after some clicks, same number appears repeatedly on the console. Why?

'''

import React, { useState } from 'react'
import ReactDOM from 'react-dom'

const App = () => {
  const [selected, setSelected] = useState(0)
  const myRandom = Math.floor(Math.random() * 6)  

  console.log("selected", selected)
  console.log("myRandom", myRandom)

  return (
      <div>
      <button onClick={() => {setSelected(myRandom) } } >next</button>
      </div>  
  )
}

ReactDOM.render(
  <App />,
  document.getElementById('root')
)

'''




Aucun commentaire:

Enregistrer un commentaire