hooks的理解
hooks是特殊的函数,在类组件当中有state在函数式组件当中没有state,hooks提供了在函数式组件当中可以使用state
hoosk当中的两个方法(useState && useEffect)
useState
语法格式
const [count , setCount] = useState(0)
count是数据,setCount是操作数据的函数,useState后的括号当中的内容是count的默认数据,可以是一个值,也可以是一个对象,比如:
const [person, setPerson] = React.useState({name: '张三疯', age: 18})
亦或者可以是以下的形式
const [data, setData] = useState({ products: [{
gname: 'macbook'
}] });
操作数据
setPerson({
...data, //深拷贝出data当中的数据,
gname:"嘿嘿",//data当中有gname属性,修改它的属性值
})
useEffect
useEffect 相当于类组件当中的componentDidmount与componentD