useState 使用方法


import React,{useState} from 'react'  export default function ComplexHookState() {      const [friends, setFriends] = useState(['李雷','科比'])      const [students, setStudents] = useState([         { id:110, name:'Eric',  age:28 },         { id:111, name:'kebo',  age:22 },         { id:112, name:'lily',  age:25 }     ])      function incrementAgeWithIndex(index){          const newStudents = [...students]         newStudents[index].age += 1          setStudents(newStudents)     }      return (         
              

好友列表

            
                    { friends.map((item,index)=>{ return {item} }) }                    setFriends([...friends,'tom'])}>添加朋友             
              

学生列表

            
                    { students.map((item,index)=>{                         return (  名字:{item.name} 年龄:{item.age}                                  incrementAgeWithIndex(index)}>age+1  )                 }) }                

        
    ) }   import React ,{useState} from 'react'
export default function CounterHock() {          const arr = useState(0);     console.log(arr);     const state = arr[0];     const setState = arr[1];     return (         
             

当前计数: {state}

             setState(state + 1 )} > +1               setState(state - 1 )} > -1          
    ) }