状态管理hook
useState
useState常见的两种赋值方式
setCount(1)
setCount(c=>c+1)
如果setCount(c+1) //这样有问题,一直是同一个值
useReducer
function countReducer(state,action){switch(action.type) {case 'add':return state+1case 'minus':return state-1default:return state}}function MyCountFunc(){const [count,dispatchCount]=useReducer(countReducer,0);useEffect(()=>{const interval=setInterval(()=>{dispatchCount({type:'add'})},1000)return ()=>{clearInterval(interval)}},[])return <span>{count}</span>}
useEffect
1、只有一个参数
useEffect(()=>{console.log('1、初始化的时候打印 2、每次更新的时候打印');return ()=>{console.log('1、初始化的时候不执行 2、')}})
useMemo
- useCallback
