0

I want updated playerNames, so that I can dispatch it,

const [playerNames, setPlayersName] = React.useState([])
const onCLick=(event)
{
   setPlayersName(prev=>[...prev,event.target.value])

    dispatch(selectPlayers(playerNames))
}
ASIF KAIF
  • 317
  • 1
  • 4
  • 17
  • Does this answer your question? [useState set method not reflecting change immediately](https://stackoverflow.com/questions/54069253/usestate-set-method-not-reflecting-change-immediately) – tarzen chugh Sep 13 '21 at 12:01
  • Does this answer your question? [How to use callback with useState hook in react](https://stackoverflow.com/questions/54954091/how-to-use-callback-with-usestate-hook-in-react) – Lin Du Sep 14 '21 at 02:00

1 Answers1

1

You can use an useEffect hook to get the updated values which works same as componentDidUpdate

useEffect(() => {
  console.log(playerNames, 'Updated playerNames value')
}, [playerNames])
Neel Dsouza
  • 1,342
  • 4
  • 15
  • 33