javascript - React.js onClick 事件返回所有空值

标签 javascript reactjs

module.exports = React.createClass({
  click: function(e){
    console.log(e)
  },
  render: function() {
    return div({className: "thing1", children:[
      div({className: "thing2", onClick: this.click})
    ]})
  }
})

传递给的事件包含点击对象的所有制作,但值为空。

Object { dispatchConfig: null, dispatchMarker: null, nativeEvent: null, type: null, target: null, currentTarget: null, eventPhase: null, bubbles: null, cancelable: null, timeStamp: null, 22 more… }

有什么想法吗?

最佳答案

出于性能原因,React 池事件对象。因此,它从池中获取一个事件对象,为其设置属性,调用处理程序,然后将所有属性设置为 null,以便可以重用。

这主要只是一个问题,因为控制台会延迟评估您记录的对象。您可以对事件对象进行浅克隆,以使 console.log 正常工作。

出于调试目的,

console.shallowCloneLog = function(){
  var typeString = Function.prototype.call.bind(Object.prototype.toString)
  console.log.apply(console, Array.prototype.map.call(arguments, function(x){
    switch (typeString(x).slice(8, -1)) {
      case 'Number': case 'String': case 'Undefined': case 'Null': case 'Boolean': return x;
      case 'Array': return x.slice();
      default: 
        var out = Object.create(Object.getPrototypeOf(x));
        out.constructor = x.constructor;
        for (var key in x) {
          out[key] = x[key];
        }
        Object.defineProperty(out, 'constructor', {value: x.constructor});
        return out;
    }
  }));
}
console.shallowCloneLog(e)

关于javascript - React.js onClick 事件返回所有空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26317985/

相关文章:

reactjs - react 管理员数据网格 : expand all rows by default

reactjs - 如何为 reactjs 中的所有子页面创建通用页眉和页脚?

javascript - Reactjs 和 redux - 如何防止来自实时搜索组件的过多 api 调用?

javascript - 在 Ant Design 和 React 中选择一个选项后清除 Select 组件

javascript - 如何解决phonegap中的HTTPS问题

php - ckeditor - 无法禁用高级内容过滤器

javascript - canvas 的 toDataURL() 函数有哪些可能的数据类型?

javascript - 与使用 animateMotion 命令时一样,避免转动物体移动

javascript - React - 无法对已卸载的组件执行 React 状态更新

javascript - 如何访问对象变量的字段以作为 props 传递给其子组件?