Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the react component instance corresponding to event.target element in a click handler?

This is my react component class

class TestInstance extends React.Component {
    onClick(e) {
        //When the user clicks in the button, 
        //I need to read the custom-id property here
    }

    render() {
        return (
            <Wrapper onClickCapture={this.onClick}>
                <div>
                    <button custom-data={{test: 'test data'}}>Click Me</button>
                </div>
            </Wrapper>
        );
    }
}

Here I'm listening to all click events that happen under the wrapper node. Whenever a click happens, I need to find out the react component instance associated with e.target and that instance's value for its custom-data prop. There will be multiple such children with different values for custom-data prop. Whenever such an element is clicked, I want to extract that element's value for its custom-data prop an do some stuff. What is the best way to do this in reactjs? One way would be to navigate the entire children tree and comapring the e.target instance for identity with the DOM elements for each of the children. I also found that e._targetInst._currentElement.props gives me the value of the props. But I don't know how reliable these undocumented variables are. Is there any documented solution for this? Basically I'm looking for something that gives me the opposite effect of ReactDOM.findDOMNode. I already have a DOM node and I need the React Element associated with that.

like image 722
Jophin Joseph Avatar asked Jan 11 '17 20:01

Jophin Joseph


People also ask

How to implement onClick event in react?

A React class component can be created by extending the Component class, and an onClick event can be implemented with a function to handle the click: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 import React, { Component } from 'react'; export default class App extends Component { handleClick() { console.log("click");

What is the event handler in react?

In React, the onClick handler allows you to call a function and perform an action when an element is clicked. onClick is the cornerstone of any React app. Click on any of the examples below to see code snippets and common uses: Call a Function After Clicking a Button; Call an Inline Function in an onClick Event Handler

How do I add an event listener to a React component?

When using React you should generally not need to call addEventListener to add listeners to a DOM element after it is created. Instead, just provide a listener when the element is initially rendered. When you define a component using an ES6 class, a common pattern is for an event handler to be a method on the class.

How do I refer to a React component instance?

Referring to a Component Instance When a component is render 'ed', a React component instance is created from the passed configuration options. One can gain access to this instance and it's properties (e.g., this.props) and methods (e.g., this.setState ()) in two ways.


1 Answers

As I mentioned in the comments above, this is much easier if you call the prop data-custom-id instead of custom-id. That way it will be rendered as a DOM attribute and you can get its value by calling e.target.getAttribute('data-custom-id'). You can see it working in the below snippet. (Since you didn't show us the code for the Wrapper component I took a guess at an implementation.)

class Wrapper extends React.Component {
  componentDidMount() {
    this.refs.wrap.addEventListener('click', this.props.onClickCapture, true);
  }
  componentDidUnmount() {
    this.refs.wrap.removeEventListener('click', this.props.onClickCapture);
  }
  render() {
    return <div ref="wrap">{this.props.children}</div>;
  }
}

class TestInstance extends React.Component {
  onClick(e) {
    console.log('Clicked %s', e.target.getAttribute('data-custom-id'));
  }

  render() {
    return (
      <Wrapper onClickCapture={this.onClick}>
        <div>
          <button data-custom-id="btn-1">Button 1</button>
          <button data-custom-id="btn-2">Button 2</button>
        </div>
      </Wrapper>
    );
  }
}

ReactDOM.render(<TestInstance/>, document.getElementById('root'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>

<div id="root"/>
like image 153
Jordan Running Avatar answered Oct 21 '22 01:10

Jordan Running