快来加入我们吧!《小河山菜鸟》为前端开发者提供技术资料和系列基础文章。为了更好的用户体验,请移步我们的官网小河山菜鸟(https://xhs-rookies.com/)学习,及时获取最新文章。“代码裁缝”,如果您对我们的文章感兴趣,或者想提点建议,请关注“小河山菜鸟”微信公众号,与我们取得联系,您也可以在微信上观看我们的文章。每一个建议或认可,都是对我们莫大的鼓励!为什么要学习钩子在本文前言中,我们的主要目的是了解钩子状态(useState)的使用。useState定义了useState()来为函数组件引入状态。纯函数不能有状态,所以使用钩子来引入状态。如何使用useState使用下面的语句。const[count,setCount]=useState(defaultValue)//假设defaultValue为0letcountVariable=useState(0)letcount=countVariable[0]letsetCount=countVariable[1]setCount(count+1)//count1setCount(10)//计数10const[name,setName]=useState('xhs')const[age,setAge]=useState(18)const[dowhat,setDowhat]=useState({thing:'LearnHooks'})setName('xxxxxxxhs')setAge(20)setDowhat({thing:'LearnNothing'})importReact,{Component}from'react'import'./App.css'exportclassAppextendsComponent{constructor(props){super(props)this.state={count:0,}}handleWithAddOne(){this.setState({count:this.state.count+1})}handleWithAddTwo(){this.setState({count:this.state.count+2})}handleWithDecreaseOne(){this.setState({count:this.state.count-1})}render(){const{count}=this.statereturn( 现在,数量是{count}。
