简体   繁体   English

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

[英]React.js onClick event returning all null values

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

The event that is passed to contains all the making of a click object but the values are null. 传递给的事件包含所有单击对象的制作但值为null。

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

Any ideas? 有任何想法吗?

React pools event objects for performance reasons. 出于性能原因,对事件池进行反应。 So it takes an event object from the pool, sets properties on it, calls your handler, and then sets all of the properties to null so it can be reused. 因此,它从池中获取一个事件对象,在其上设置属性,调用您的处理程序,然后将所有属性设置为null,以便可以重用它。

This is mostly only a problem because the console lazily evaluates the object you log. 这主要是一个问题,因为console懒惰地评估您记录的对象。 You could do a shallow clone of the event object to make console.log work. 您可以对事件对象执行浅层克隆以使console.log工作。

For debugging purposes, 出于调试目的,

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)

Event handlers will be passed instances of SyntheticEvent . 事件处理程序将传递SyntheticEvent实例。 The SyntheticEvent is pooled. SyntheticEvent汇集在一起​​。 This means that the SyntheticEvent object will be reused and all properties will be nullified after the event callback has been invoked. 这意味着将重用SyntheticEvent对象,并且在调用事件回调后,所有属性都将无效

If you want to access the event properties in an asynchronous way, you should call event.persist() 如果要以异步方式访问事件属性,则应调用event.persist()

func(e){
    e.persist();
    console.log(e);// all the properties are retained
}

render () {
    return(
      <div onMouseOver={this.func}>
      //rest of the logic
      </div>
    );
}

如果你有方便的jQuery,只需调用:

console.log('e: ', $.extend({}, e));

如果您正在使用阶段2或更高版本的ES6功能,则console.log({...e})工作方式与上面的浅克隆实现相同。

Using console.log(Object.assign({}, e)); 使用console.log(Object.assign({}, e)); is an elegant way to handle this--a good alternative jQuery's extend method. 是一种处理这个问题的优雅方法 - 一个很好的替代jQuery的扩展方法。

In the interest of teaching a man to fish, it'd be good to learn how this works. 为了教导一个人捕鱼,了解它的工作原理会很好。 Here's the direct link to that section from ES2015 specs: http://www.ecma-international.org/ecma-262/6.0/#sec-object.assign 以下是ES2015规范中该部分的直接链接: http//www.ecma-international.org/ecma-262/6.0/#sec-object.assign

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM