最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - How to make the button interactive in React.js? - Stack Overflow

matteradmin4PV0评论

Please point me to my error.

I want to write an interactive button that displays the number of clicks on it. With each next click, the number on the button have to increase by one. Initial number is 0, so, for example, after 5 clicks it bees 5. Without an interactive element, the code is rendered normally.

Here is my code:

class Button extends React.Component{
  constructor(props){
    super(props);
    this.state = {counter: 0};
  };

  handleClick(){
    this.setState(
      prevState => ({counter: prevState.counter + 1})
    );
  }

  render(){
    return (
      <button onClick={this.handleClick}>{this.state.counter}</button>
    );
  }
}

Here is the error I get:

TypeError: Cannot read property 'setState' of undefined
handleClick
src/index.js:30:6
  27 | 
  28 |   render(){
  29 |     return (
> 30 |       <button onClick={this.handleClick}>{this.state.counter}</button>
     |      ^  31 |     );
  32 |   }
  33 | }
View piled
▶ 20 stack frames were collapsed.

Thank you in advance!

Please point me to my error.

I want to write an interactive button that displays the number of clicks on it. With each next click, the number on the button have to increase by one. Initial number is 0, so, for example, after 5 clicks it bees 5. Without an interactive element, the code is rendered normally.

Here is my code:

class Button extends React.Component{
  constructor(props){
    super(props);
    this.state = {counter: 0};
  };

  handleClick(){
    this.setState(
      prevState => ({counter: prevState.counter + 1})
    );
  }

  render(){
    return (
      <button onClick={this.handleClick}>{this.state.counter}</button>
    );
  }
}

Here is the error I get:

TypeError: Cannot read property 'setState' of undefined
handleClick
src/index.js:30:6
  27 | 
  28 |   render(){
  29 |     return (
> 30 |       <button onClick={this.handleClick}>{this.state.counter}</button>
     |      ^  31 |     );
  32 |   }
  33 | }
View piled
▶ 20 stack frames were collapsed.

Thank you in advance!

Share Improve this question asked Mar 13, 2019 at 13:07 kokserekkokserek 57911 silver badges26 bronze badges 0
Add a ment  | 

4 Answers 4

Reset to default 4

In your example, this in your handleClick() function refers to the scope of the event that was fired from your click. In order to solve this you must bind the functions scope to the element.

Add this.handleClick = this.handleClick.bind(this) to your constructor and it should work fine.

https://reactjs/docs/handling-events.html

You could go a little bit more advanced and use the ECMAScript 6 arrow function syntax, which will avoid that kind of problem and also avoid the need to use .bind(this).

If you declare handleClick like handleClick = () => {}, you wont need to use in your constructor this.handleClick.bind(this). Everything will work normally when you use this, it will be the class's this, not the function's this.

This is using newer versions of javascript and is highly remended to don't use .bind(this) which can cause some issues.

Just declare handleClick like this and it will work without the need to use .bind(this)

  handleClick = () => {
    this.setState(
      prevState => ({counter: prevState.counter + 1})
    );
  }

You have to bind the click handler:

...
  constructor(props){
    super(props);
    this.state = {counter: 0};
    this.handleClick.bind(this) // <----- here
  }
...

I think you have two options:

  1. Add bind to your constructor. this.handleClick.bind(this), which will ensure that your this actually refers to the button object.
  2. Use newer format, which I like better.

    handleClick = () => {
        this.setState(
            prevState => ({counter: prevState.counter + 1})
        );
    }
    

A little more helpful documentation on a lot of React topics here: https://reactjs/docs/react-ponent.html

Post a comment

comment list (0)

  1. No comments so far