ReactJS - prevState in the new useState React hook?

前端 未结 4 851
南方客
南方客 2021-01-31 17:45

I really like the new React hooks and I\'m using them frequently for a project I\'m working on. I\'m coming across a situation where I want to use the prevState in the

4条回答
  •  迷失自我
    2021-01-31 18:28

    state updater from useState provides a callback pattern which returns you the previous state which you can use to update the current state

    const [ someState, setSomeState ] = useState( new Map() )
    setSomeState(prevState => prevState.set( key, value ) )
    

提交回复
热议问题