对象类型
import React, { useState } from 'react';
const Index = ()=> {
const [obj, setObj] = useState({name:"zhangsan"});
return (
<>
<h2>{obj.name}---{obj.age}</h2>
<button onClick={()=> (
setObj({
...obj,
age:18
})
)}>change-obj</button>
</>
);
}
数组类型
import React, { useState } from 'react';
const Index = ()=> {
const [arr, setArr] = useState([1,2,3]);
return (
<>
<h2>{arr}</h2>
<button onClick={()=>(setArr(
setarr(()=> {
arr.push(4);
return [...arr]
})>change-arr</button>
</>
);
}