React hooks: accessing up-to-date state from within a callback

后端 未结 8 1768
梦谈多话
梦谈多话 2020-12-25 09:35

EDIT (22 June 2020): as this question has some renewed interest, I realise there may be a few points of confusion. So I would like to highlight: the example in the question

8条回答
  •  一个人的身影
    2020-12-25 10:22

    onClick={() => { clickHandler(); }}
    

    This way you run the function as defined when you click it not when you declared the onClick handler.

    React re-runs the hook function every time there is a change, and when it does so it re-defines your clickHandler() function.

    For the record, you could clean that up. Since you don't care what your arrow function returns you could write it as such.

    onClick={e => clickHandler()}
    

提交回复
热议问题