快来加入我们吧!
"小和山的菜鸟们",为前端开发者提供技术相关资讯以及系列基础文章。为更好的用户体验,请您移至我们官网小和山的菜鸟们 ( https://xhs-rookies.com/ ) 进行学习,及时获取最新文章。
"Code tailor" ,如果您对我们文章感兴趣、或是想提一些建议,微信关注 “小和山的菜鸟们” 公众号,与我们取的联系,您也可以在微信上观看我们的文章。每一个建议或是赞同都是对我们极大的鼓励!
前言
这篇文章,我们主要目的是了解一下 状态钩子(useState) 的使用.
useState
定义
useState()
用于为函数组件引入状态。纯函数不能有状态,所以使用钩子来引入状态。
如何使用
useState
的使用如下面的语句。
const [count, setCount] = useState(defaultValue) // 假设 defaultValue 为 0
let countVariable = useState(0)
let count = countVariable[0]
let setCount = countVariable[1]
setCount(count + 1) // count 1
setCount(10) // count 10
const [name, setName] = useState('xhs')
const [age, setAge] = useState(18)
const [dowhat, setDowhat] = useState({ thing: 'Learn Hooks' })
setName('xxxxxxxhs')
setAge(20)
setDowhat({ thing: 'Learn Nothing' })