|
|
@@ -0,0 +1,23 @@
|
|
|
+import { useRef, useState, useEffect } from 'react';
|
|
|
+import './useRef.css'
|
|
|
+function LearnUseRef() {
|
|
|
+ let [time, setTime] = useState(new Date());
|
|
|
+ // let [timer,setTimer] = useState(null)
|
|
|
+ let timer = useRef(null);
|
|
|
+ // {current:xxx}
|
|
|
+ console.log(timer.current,'初始化')
|
|
|
+ useEffect(() => {
|
|
|
+ timer.current = setInterval(() => {
|
|
|
+ setTime(new Date())
|
|
|
+ console.log(timer.current,'更新')
|
|
|
+ }, 1000)
|
|
|
+ }, [])
|
|
|
+ function handleStop() {
|
|
|
+ clearInterval(timer.current);
|
|
|
+ }
|
|
|
+ return <div className='box'>
|
|
|
+ <h2>当前时间:{time.toLocaleString()}</h2>
|
|
|
+ <button onClick={handleStop}>停止计时</button>
|
|
|
+ </div>
|
|
|
+}
|
|
|
+export default LearnUseRef;
|