React 组件的三大核心属性之 state 在函数式组件中的基本使用

使用Hook可以在函数式组件中使用state

import React, {useState} from 'react'
import {Button} from "antd";

export default function FuncCompDemo(props) {

    const initCount = 0;
    /**
     * @param 初值
     * @return 数据和修改数据的function
     */ 
    const [count, setCount] = useState(initCount)

    function incr() {
        /**
         * 修改count, 使用方式二
         * @param 修改后的值
         */
        setCount(count + 1)
    }

    function decr() {
        /**
         * 修改count, 使用方式二
         * @param 一个函数,修改后的值
         */
        setCount(value => value - 1)
    }

    return (
        <div>
            <h1>数值为: {count}</h1>
            <Button onClick={incr} type="primary"> +1 </Button><br/>
            <Button onClick={decr} type="primary"> -1 </Button>
        </div>
    )

}
posted @   叕叕666  阅读(58)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示