1

I'm new to React and I'm trying to pass the HTML element from an onClick event, but I'm not getting the expected result.

Here is the code:

import React, { Component } from 'react';

export class Header extends Component{
  isScrolledIntoView (e){
     console.log('html element is ',e)
   }

    render(){
       return(
            <div>
                <button onClick={this.isScrolledIntoView.()}>Click me</button>
            </div>
        );
     }
}

The desired output would be to get the button's HTML element in the console.

4 Answers 4

3

You need to capture the target of the e (event) instead of the event itself, like this:

import React, { Component } from 'react';

export class Header extends Component {

  isScrolledIntoView (e) {
     console.log('html element is ', e.target)
  }

  render() {
       return (
            <div>
                <button onClick={this.isScrolledIntoView.bind(this)}>Click me</button>
            </div>
       );
  }

}

Here is a demo link: https://codesandbox.io/s/y8xXqopM7

Hope it helps!

Sign up to request clarification or add additional context in comments.

5 Comments

wow, it worked! Just curious what is 'e' here and e.target means?
I did a demo link, also, just in case: codesandbox.io/s/y8xXqopM7
It returns the element that triggered the event.
Whenever you have a HTML event, you have a event being capture on the page. The event object has a target property, which shows the targeted html element.
0

The method isScrolledIntoView() is bound to the class, not the component instance, so when you refer to this.isScrolledIntoView() in your render method it will return undefined. Regular React lifecycle methods are bound to the component instance, but for your own custom methods you need to do a little work, you can put it in the constructor:

constructor(props) {
    this.isScrolledIntoView = this.isScrolledIntoView.bind(this);
}

Or you can use class properties to auto-bind the method:

isScrolledIntoView = (e) => {
    // do stuff
}

Comments

0

2 things you need to change in your code.

1- You have to bind your isScrolledIntoView, and it could be inside your constructor, or doin' this => <button onClick={this.isScrolledIntoView.bind(this)}>Click me</button>

2- You should target your e event instead of only log e you should
=> console.log('html element is ', e.target)

Nice reading for novices in react

Comments

0
Passing current element using ref

class Square extends React.Component {
    constructor(props) {
    super(props);
    
    this.state = {
      value: null,
    };
  }
  
  render() {
    return (
      <button className="square"
 onClick={function(ref) { console.info(" click : " + ref.target.innerHTML); }}>
        {this.props.value}
      </button>
    );
  }
}

Credits : https://stackoverflow.com/ about using "ref"

1 Comment

Code-only answers are considered low quality: make sure to provide an explanation what your code does and how it solves the problem. It will help the asker and future readers both if you can add more information in your post. See also Explaining entirely code-based answers: meta.stackexchange.com/questions/114762/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.