zheng 1 天之前
父節點
當前提交
feea5c7925

+ 5 - 37
19.React/高阶/project1/src/App.css

@@ -1,38 +1,6 @@
-.App {
-  text-align: center;
-}
-
-.App-logo {
-  height: 40vmin;
-  pointer-events: none;
-}
-
-@media (prefers-reduced-motion: no-preference) {
-  .App-logo {
-    animation: App-logo-spin infinite 20s linear;
-  }
-}
-
-.App-header {
-  background-color: #282c34;
-  min-height: 100vh;
-  display: flex;
-  flex-direction: column;
-  align-items: center;
-  justify-content: center;
-  font-size: calc(10px + 2vmin);
-  color: white;
-}
-
-.App-link {
-  color: #61dafb;
-}
-
-@keyframes App-logo-spin {
-  from {
-    transform: rotate(0deg);
-  }
-  to {
-    transform: rotate(360deg);
-  }
+.main {
+    /* display: flex;
+    justify-content: center;
+    align-items: center; */
+    position: relative;
 }

+ 11 - 3
19.React/高阶/project1/src/App.js

@@ -2,15 +2,23 @@ import { useState } from 'react';
 import './App.css';
 import LearnUseState from './components/LearnUseState';
 import LearnUseEffect from './components/LearnUseEffect';
+import LearnUseRef from './components/LearnUseRef';
 function App() {
-  let [show,isShow] = useState(true);
+  let [show, isShow] = useState(true);
+  let [open, isOpen] = useState(true);
   return (
     <div className="App">
       <LearnUseState></LearnUseState>
       {
-        show ?  <LearnUseEffect></LearnUseEffect> : <div>已卸载</div>
+        show ? <LearnUseEffect></LearnUseEffect> : <div>已卸载</div>
       }
-     
+      <div className='main'>
+
+        {
+          open ? <LearnUseRef></LearnUseRef> : <div>已卸载</div>
+        }
+      </div>
+
     </div>
   );
 }

+ 23 - 0
19.React/高阶/project1/src/components/LearnUseRef.jsx

@@ -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;

+ 19 - 0
19.React/高阶/project1/src/components/useRef.css

@@ -0,0 +1,19 @@
+.box {
+    width: 300px;
+    height: 200px;
+    border: 2px solid #f00;
+    /* position: absolute;
+    top: 50%;
+    left: 50%;
+    margin-top: -100px;
+    margin-left: -150px; */
+    /* display: table-cell;
+    vertical-align: middle;
+    text-align: left; */
+    /* position: absolute;
+    top: 50%;
+    left: 50%;
+    transform: translate(-50%,-50%); */
+    position: absolute;
+    transform: translateX(150px) translateY(100px);
+}

+ 3 - 2
19.React/高阶/project1/src/index.js

@@ -6,10 +6,11 @@ import reportWebVitals from './reportWebVitals';
 
 const root = ReactDOM.createRoot(document.getElementById('root'));
 root.render(
-  <React.StrictMode>
     <App />
-  </React.StrictMode>
 );
+  // <React.StrictMode>
+  //   <App />
+  // </React.StrictMode>
 
 // If you want to start measuring performance in your app, pass a function
 // to log results (for example: reportWebVitals(console.log))