【问题标题】:Stateful Class Hook React [duplicate]有状态的类 Hook React [重复]
【发布时间】:2020-12-19 14:09:52
【问题描述】:

如何使它作为增量按钮工作:

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>
        );
 }

【问题讨论】:

标签: javascript reactjs


【解决方案1】:
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

【讨论】:

    猜你喜欢
    • 2021-08-24
    • 2020-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多