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

Please point me to my error. I want to write an interactive button that displays the number of clicks o

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745665004a4639070.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信