0 votes
in ReactJS by
What would be the common mistake of function being called every time the component renders?

1 Answer

0 votes
by

You need to make sure that function is not being called while passing the function as a parameter.

render() {
  // Wrong: handleClick is called instead of passed as a reference!
  return <button onClick={this.handleClick()}>{'Click Me'}</button>
}

Instead, pass the function itself without parenthesis:

render() {
  // Correct: handleClick is passed as a reference!
  return <button onClick={this.handleClick}>{'Click Me'}</button>
}

Related questions

0 votes
asked Nov 2, 2023 in ReactJS by AdilsonLima
0 votes
asked Nov 4, 2023 in ReactJS by AdilsonLima
...