web前端全系列 教程
1839个小节阅读:2244k
目录
鸿蒙应用开发
C语言快速入门
JAVA全系列 教程
面向对象的程序设计语言
Python全系列 教程
Python3.x版本,未来主流的版本
人工智能 教程
顺势而为,AI创新未来
大厂算法 教程
算法,程序员自我提升必经之路
C++ 教程
一门通用计算机编程语言
微服务 教程
目前业界流行的框架组合
web前端全系列 教程
通向WEB技术世界的钥匙
大数据全系列 教程
站在云端操控万千数据
AIGC全能工具班
A A
White Night
useState
useState
是一个Hook,它本质上是一个函数,这个函数可以用来定义组件的state变量。
useState()
方法里面唯一的参数就是初始 state。不同于 class 的是,我们可以按照需要使用数字或字符串对其进行赋值,而不一定是对象。
useState
返回值为:当前 state 以及更新 state 的函数。
xxxxxxxxxx
import React, { useState } from 'react'
export default function Counter() {
// 声明一个叫 “count” 的 state 变量。useState返回一个数组
const [count, setCount] = useState(0)
return (
<div>
我是使用hook的组件<br />
<button onClick={()=> setCount(count+1)}>变量</button>
<h3>数量:{count}</h3>
</div>
)
}
多个state变量
xxxxxxxxxx
import React, { useState } from 'react'
export default function Counter() {
// 声明一个叫 “count” 的 state 变量。useState返回一个数组
const [count, setCount] = useState(0)
//声明叫做color的state变量
const [color, setColor] = useState('red')
return (
<div>
我是使用hook的组件
<br />
<button onClick={()=> setCount(count+1)}>变量</button>
<button onClick={()=> setColor('blue')}>变色</button>
<h3 style={{color}}>数量:{count}</h3>
</div>
)
}
更改state
useState
返回值为:当前 state 以及更新 state 的函数。要更改state,则调用更新state的函数就可以。
实时效果反馈
1. 使用___来定义state变量:
A createState()
B usestate()
C useState()
D useStates()
答案
1=>C