Sunday, May 12, 2024
 Popular · Latest · Hot · Upcoming
150
rated 0 times [  151] [ 1]  / answers: 1 / hits: 21000  / 10 Years ago, sun, march 2, 2014, 12:00:00

I'm unable to get any of the React SyntheticKeyboardEvent handlers to register anything except null for the event properties.



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/



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);

More From » reactjs

 Answers
8

BinaryMuse provided the answer on 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:



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


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


[#72214] Friday, February 28, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kaileya

Total Points: 168
Total Questions: 95
Total Answers: 72

Location: Antigua and Barbuda
Member since Sat, Apr 24, 2021
3 Years ago
kaileya questions
Sun, Aug 16, 20, 00:00, 4 Years ago
Thu, Jun 25, 20, 00:00, 4 Years ago
Wed, Jul 3, 19, 00:00, 5 Years ago
Tue, Apr 23, 19, 00:00, 5 Years ago
Wed, Feb 20, 19, 00:00, 5 Years ago
;