Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect which React component triggers onKeyUp event?

Let's say we have a single onKeyUp handler:

handleKeyUp: function(e) {
  /* handle stuff */
},

And we have a couple of input components, both of which could trigger the handler:

<input type="text" ref="login" onKeyUp={this.handleKeyUp} />
...
<input type="text" ref="pwd" onKeyUp={this.handleKeyUp} />

How do I make it so the handler can detect if the onKeyUp was triggered from login or pwd?

A scenario is where I detect a tab press on pwd and then I proceed to try to save the text fields (but not where I tab off from login).

I've tried looking into detail of e.target but couldn't figure out how to reference the originating component.

Update

Sorry, must not be thinking clearly. Yes, e.target is a reference to the originating component. I was looking to get a handle on ref to get the value. But I don't need the ref, I can just get the value from e.target.value.

like image 731
Daniel May Avatar asked Mar 08 '16 08:03

Daniel May


People also ask

How do I use onKeyUp in Reactjs?

Use the React. KeyboardEvent<HTMLElement> type to type the onKeyUp event in React. The KeyboardEvent interface is used for onKeyUp events. You can access the value of the key pressed by the user as event.

How do you know Enter key is pressed in React?

To detect when the user pressed the Enter key when typing in an input field: Add the onKeyDown prop to the input element. Every time the user presses a key in the input field, check if the pressed key is Enter.

How do you get onClick event in React?

React events are written in camelCase syntax: onClick instead of onclick . React event handlers are written inside curly braces: onClick={shoot} instead of onClick="shoot()" .


1 Answers

As stated in React's Event System documentation :

Your event handlers will be passed instances of SyntheticEvent, a cross-browser wrapper around the browser's native event. It has the same interface as the browser's native event, including stopPropagation() and preventDefault(), except the events work identically across all browsers.

Thus, an instance of a SyntheticEvent is passed to your callback

handleKeyUp: function(event) {
    /* event is an instance of SyntheticEvent 
       from wich you can extract the currentTarget 
    */
},

Edit : In case you really want to access component's ref name before doing anything, here is how you could do it in ES6 :

class MyComponent extends React.Component {

    constructor() {

        super();

        this.handleLoginKeyUp = this.keyUpHandler.bind(this, 'LoginInput');
        this.handlePwdKeyUp = this.keyUpHandler.bind(this, 'PwdInput');
    }

    keyUpHandler(refName, e) {
        console.log(refName);
        // prints either LoginInput or PwdInput
    }

    render() {

        return (
            <div>
                <input type="text" onKeyUp={this.handleLoginKeyUp} ref="LoginInput" />
                <input type="text" onKeyUp={this.handlePwdKeyUp} ref="PwdInput" />
            </div>
        );
    }
}
like image 122
mr-wildcard Avatar answered Oct 11 '22 19:10

mr-wildcard