Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

like image 965
boom Avatar asked Oct 11 '14 18:10

boom


1 Answers

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.

This is mostly only a problem because the console lazily evaluates the object you log. You could do a shallow clone of the event object to make console.log work.

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) 
like image 115
Brigand Avatar answered Sep 20 '22 19:09

Brigand