简体   繁体   English

如何在React.js中使按钮交互?

[英]How to make the button interactive in React.js?

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. 每次下一次单击时,按钮上的数字必须增加1。 Initial number is 0, so, for example, after 5 clicks it becomes 5. Without an interactive element, the code is rendered normally. 初始编号为0,因此,例如,在5次单击后,它变为5.没有交互式元素,代码将正常呈现。

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 compiled
▶ 20 stack frames were collapsed.

Thank you in advance! 先感谢您!

In your example, this in your handleClick() function refers to the scope of the event that was fired from your click. 在你的榜样, this在你的handleClick()函数是指从您的点击触发事件的范围。 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. this.handleClick = this.handleClick.bind(this)添加到您的构造函数中,它应该可以正常工作。

https://reactjs.org/docs/handling-events.html https://reactjs.org/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) . 您可以更高级一点,并使用ECMAScript 6箭头函数语法,这将避免这种问题,也避免使用.bind(this)

If you declare handleClick like handleClick = () => {} , you wont need to use in your constructor this.handleClick.bind(this) . 如果你声明handleClickhandleClick = () => {} ,你将不需要在你的构造函数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 ,这将是类的this ,而不是函数的this

This is using newer versions of javascript and is highly recommended to don't use .bind(this) which can cause some issues. 这是使用较新版本的javascript,强烈建议不要使用.bind(this) ,这可能会导致一些问题。

Just declare handleClick like this and it will work without the need to use .bind(this) 只需声明handleClick就像这样,它将工作而无需使用.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. 将bind添加到构造函数中。 this.handleClick.bind(this) , which will ensure that your this actually refers to the button object. this.handleClick.bind(this) ,这将保证您的this实际上指的是按钮对象。
  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.org/docs/react-component.html 关于很多React主题的更多有用文档: https//reactjs.org/docs/react-component.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM