HOOKS
useState
import React, { useState } from 'react'
const [object,updateFunction] = useState(initValue)
initValue为初始值
object为接收值
updateFunction为更新函数
更新方式:
updateFunction(object)
updateFunction(()=>{
return object
})
useEffect
import React, { useEffect } from 'react'
useEffect(()=>{}) 检测所有的useState定义的值得变化,值发生改变时,会自动调用
useEffect(()=>{},[]) 所有值的变化都不检测
useEffect(()=>{},[count]) 检测useState定义的count属性的变化
useEffect(()=>{
window.addEventListener('scroll',scroll)
return{
window.removeEventListener('scroll',scroll)
}
})
useRef
import React, { useRef } from 'react'
const testRef = useRef()
<input ref={testRef} />