有状态的类挂钩反应

时间:2020-08-31 12:45:45

标签: javascript reactjs

如何使其用作增量按钮:

import React, { Component } from 'react';
export default class Logon extends Component{
    constructor(props){
        super(props);
        this.state = {
            counter: 0
        }
    }
render(){   
        return(
               <button onClick={() => {this.state.counter = this.state.counter + 1}}> 
                       {this.state.counter}
               </button>
        );
 }

1 个答案:

答案 0 :(得分:2)

You need to call the `setState` to set the value of state in React.

<button onClick={() => {this.setState({counter: counter+1})}> 
   {this.state.counter}
</button>

在React js中引用State