赞
踩
useRef的作用主要有两种
操作步骤
import { useRef } from 'react'
const xxxx = useRef(null)
<input ref={xxxx} />
xxxx.current
- import React, { useEffect, useState } from 'react'
- import ReactDom from 'react-dom'
- export default function App () {
- const [count, setCount] = useState(0)
- let timeId = null
- console.log(timeId)
- useEffect(() => {
- timeId = setInterval(() => {
- setCount((count) => count + 1)
- }, 1000)
- console.log(timeId)
- }, [])
-
- const hClick = () => {
- console.log(timeId)
- clearInterval(timeId)
- }
-
- return (
- <div>
- count:{count}
- <button onClick={hClick}>点击停止定时器</button>
- </div>
- )
- }
- ReactDom.render(<App />, document.getElementById('root'))
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。