1

This is my component:

import React, {Component} from 'react';
import 'D:/School/Alta/interactiveweb/src/webapp/src/App.css'

class Chat extends Component {

  test() {
    alert();
  }

  render() {
    return <nav onClick={this.test()} className={"menu"} id={"Chat"}>
        <div className={"menu-itemContainer"}>
            <div className={"menu-item"}>
                <img alt={""} className={"chatPF"} src={require('./Images/defaultPF.png')}/>
                <a className={"chatname"}>Test</a>
                <a className={"chatlastuser"} id={"chatlastuser"}>
                    <font color={"orange"}>Bram: </font>wow wtf...
                </a>
                <a className={"chatlasttime"}>20:28</a>
            </div>
        </div>
    </nav>;
  }
}

export default Chat;

when starting it will run the function multiple times but when it's loaded and I click the nav it won't work.

3
  • 4
    Please don't upload images of code. They can't be copied to reproduce the issue, they aren't searchable for future readers and they are harder to read than text. Please post the actual code as text to create a minimal reproducible example. Commented Sep 30, 2020 at 17:20
  • 2
    Use onClick = {() => this.test()} Commented Sep 30, 2020 at 17:20
  • 1
    Even better: onClick={this.test} Commented Sep 30, 2020 at 17:21

3 Answers 3

1

Instead of onClick={this.test()}, you need to use onClick={this.test}. As you had it, it called the function immediately and left undefined in the onClick listener. By passing the function as-is, you ensure that it will be called properly when the element is clicked.


While onClick={()=>this.test()} would work, it adds another function to the call stack unnecessarily because in JavaScript functions are first-class objects.

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

Comments

1

You can learn more about events in React here. That being said, the following should work. You were calling the function every time the component render, but you want to call it only when the click occurs. To do that, you should just pass the handler / method that will then get fired.

import React, {Component} from 'react';
import 'D:/School/Alta/interactiveweb/src/webapp/src/App.css'

class Chat extends Component {
  test() {
    alert();
  }

  render() {
    return <nav onClick={this.test} className="menu" id="Chat">
        <div className="menu-itemContainer">
            <div className="menu-item">
                <img alt="" className="chatPF" src={require('./Images/defaultPF.png')}/>
                <a className="chatname">Test</a>
                <a className="chatlastuser" id="chatlastuser">
                    <font color="orange">Bram: </font>wow wtf...
                </a>
                <a className="chatlasttime">20:28</a>
            </div>
        </div>
    </nav>;
  }
}

export default Chat;

2 Comments

An answer without an explanation is not so useful.
I was editing it.
0

Change it to either onClick={this.test} or onClick={()=>this.test()}.

You don't want to call the functions in attributes, you just want to declare them. They will be called when the click event happens.

1 Comment

This answer is good, but I wouldn't suggest using ()=>this.test() because it adds another function to the call stack unnecessarily. In JS functions are first-class objects.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.