React: Event handler method for Backspace

后端 未结 2 1910
隐瞒了意图╮
隐瞒了意图╮ 2021-02-19 06:39

I am trying to handle an event when the user presses the Backspace button.

I saw this, and I guess I can find Backspace key code using

console.log(\"Did y

2条回答
  •  无人共我
    2021-02-19 07:06

    You have to listen to the onKeyDown event to capture the delete action. Example:

    var InputDemo = React.createClass({
        getInitialState: function() {
            return {
                message: ''
            };
        },
    
        onKeyDown: function(e) {
            if (e.keyCode === 8) {
                console.log('delete');
            }
        },
    
        handleChange: function(e) {
            this.setState({
                message: e.target.value
            });
        },
    
        render: function() {
            return (
                
    ); } });

    Running fiddle: https://jsfiddle.net/7eu41pzz/1/

提交回复
热议问题