简体   繁体   English

React:键盘事件处理程序全部'空'

[英]React: Keyboard Event Handlers All 'Null'

I'm unable to get any of the React SyntheticKeyboardEvent handlers to register anything except null for the event properties. 我无法获取任何React SyntheticKeyboardEvent处理程序来为事件属性注册除null之外的任何内容。

I've isolated the component in a fiddle and am getting the same result as in my application. 我已经将这个组件隔离在一个小提琴中,并得到与我的应用程序相同的结果。 Can anyone see what I'm doing wrong? 谁能看到我做错了什么?

http://jsfiddle.net/kb3gN/1405/ http://jsfiddle.net/kb3gN/1405/

var Hello = React.createClass({
    render: function() {
      return (
      <div>
        <p contentEditable="true"
           onKeyDown={this.handleKeyDown}
           onKeyUp={this.handleKeyUp}
           onKeyPress={this.handleKeyPress}>Foobar</p>
        <textarea
           onKeyDown={this.handleKeyDown}
           onKeyUp={this.handleKeyUp}
           onKeyPress={this.handleKeyPress}>
        </textarea>
        <div>
          <input type="text" name="foo" 
           onKeyDown={this.handleKeyDown}
           onKeyUp={this.handleKeyUp}
           onKeyPress={this.handleKeyPress} />
        </div>
      </div>
      );
    },

    handleKeyDown: function(e) {
      console.log(e);
    },

    handleKeyUp: function(e) {
     console.log(e);
    },

    handleKeyPress: function(e) {
     console.log(e); 
    }
});

React.renderComponent(<Hello />, document.body);

BinaryMuse provided the answer on IRC. BinaryMuse在IRC上提供了答案。 Turns out it's just a quirk; 事实证明这只是一个怪癖; you can't read the properties directly from SyntheticKeyboardEvent -- you need to specify the properties from the handler: 您无法直接从SyntheticKeyboardEvent读取属性 - 您需要从处理程序指定属性:

handleKeyUp: function(e) {
 console.log(e.type, e.which, e.timeStamp);
},

http://jsfiddle.net/BinaryMuse/B98Ar/ http://jsfiddle.net/BinaryMuse/B98Ar/

console.log() is aynchronous and by the time it access the event React already garbage collected it (it reuses the event for performance reasons). console.log()是异步的,当它访问事件时,React已经垃圾收集它(它出于性能原因重用该事件)。

For debugging purposes, the simplest thing to do is to tell React to not discard that event 出于调试目的,最简单的方法是告诉React不要丢弃该事件

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 我找不到API文档,该方法至少记录在源代码https://github.com/facebook/react/blob/c78464f/src/renderers/shared/stack/event/SyntheticEvent.js#L155

As Riccardo Galli points out correctly, the log object is already garbage collected at the time you access it in the console. 正如Riccardo Galli指出的那样,日志对象在您在控制台中访问它时已经被垃圾收集了。

The solution I use is to just log a clone of the object, so it won't be garbage collected. 我使用的解决方案是只记录对象的克隆,因此不会进行垃圾回收。 Cloning can be done in a lot of ways, but since I use lodash, I log like this : 克隆可以通过很多方式完成,但由于我使用lodash,我会像这样记录:

  handleKeyDown: function(e) {
      console.log(_.cloneDeep(e)));
    }

You can also extract the underlying (original) browser event from the Synthetic*Event wrapper via the nativeEvent property. 您还可以通过nativeEvent属性从Synthetic*Event包装器中提取基础(原始)浏览器事件。 Eg, 例如,

handleKeyDown: function(e) {
  console.log('keyDown:event', e.nativeEvent);
},

(Just as with @Riccardo's note about e.persist() , it's unclear how you're supposed to know this without reading the React.js source code.) (就像@ Riccardo关于e.persist()的说明e.persist() ,如果不阅读React.js源代码,你不清楚如何知道这一点。)

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

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