react 自定义hook

const useCount=(defaultCount)=>{
  const [count,setCount]=useState(defaultCount)
  let it=useRef(null)
  
  useEffect(() => {
    console.log('mount:'+count)
    it.current = setInterval(() => {
      
      setCount(count => count + 1)
      
    }, 1000)
    
  } , [])
  
  useEffect(() => {
    console.log('update:'+count)
    if (count >= 5) {
      
      clearInterval(it.current)
      
    }
    
  },[count])
  
  return [count,setCount]
}

 

你可能感兴趣的:(react,react,hook)