React: Keyboard Event Handlers All 'Null'

前端 未结 4 1688
南方客
南方客 2021-01-30 16:27

I\'m unable to get any of the React SyntheticKeyboardEvent handlers to register anything except null for the event properties.

I\'ve isolated t

4条回答
  •  暖寄归人
    2021-01-30 16:55

    console.log() is aynchronous and by the time it access the event React already garbage collected it (it reuses the event for performance reasons).

    For debugging purposes, the simplest thing to do is to tell React to not discard that event

    e.persist() // NOTE: don't forget to remove it post debug
    console.log(e)
    

    I can't find an API documentation, the method is at least documented in the sources https://github.com/facebook/react/blob/c78464f/src/renderers/shared/stack/event/SyntheticEvent.js#L155

提交回复
热议问题