javascript - 在 mapDispatchToProps 中传递事件对象和参数

标签 javascript reactjs ecmascript-6 redux react-redux

我希望能够在发送参数的同时访问 mapDispatchToProps 中的事件对象。你如何看待这件事?

这是我目前所拥有的:

const mapDispatchToProps = dispatch => ({
  onRowClick: (id, event) => {
     console.log(id, event)
  },
})

在我看来:

<TR key={`${index}`} onClick={() => onRowClick(id, event)}>

但是,这是不正确的。如何分派(dispatch)事件、传递参数并访问事件对象?

最佳答案

我来告诉你why you shouldn't do it: :

The SyntheticEvent is pooled. This means that the SyntheticEvent object will be reused and all properties will be nullified after the event callback has been invoked. This is for performance reasons. As such, you cannot access the event in an asynchronous way.

但是,如果您需要,您可以这样做:

const mapDispatchToProps = dispatch => ({
  onRowClick: (id, event) => {
     console.log(id, event)
  },
})

<TR key={`${index}`} onClick={(event) => {
  event.persist(); // remove the synthetic event from the pool
  onRowClick(id, event);
}}>

关于javascript - 在 mapDispatchToProps 中传递事件对象和参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49518232/

相关文章:

javascript - 你能给我看一些 Keymap.js(权威指南)的例子吗?

reactjs - 未找到页面已返回

javascript - 同时解构和传入完整对象

javascript - 比较两个对象以获得百分比

javascript - 如何为 'extend' 提供动态值? (或 'how does extend work in this case' ?)

javascript - 改变循环中 console.log 的速度

javascript - 为什么将元素追加到末尾

javascript - 如何使用 charAt 处理 JS 字符串中的空格

javascript - 如何在 react 中显示加载程序。使用钩子(Hook)

javascript - AsyncStorage不保存更改状态 - React Native