zheng 1 тиждень тому
батько
коміт
1f27f9bd01

+ 5 - 116
21.react/高阶/project5/src/App.jsx

@@ -1,122 +1,11 @@
-import { useState } from 'react'
-import reactLogo from './assets/react.svg'
-import viteLogo from './assets/vite.svg'
-import heroImg from './assets/hero.png'
-import './App.css'
+import CountPage from "./components/CountPage";
 
 function App() {
-  const [count, setCount] = useState(0)
-
   return (
-    <>
-      <section id="center">
-        <div className="hero">
-          <img src={heroImg} className="base" width="170" height="179" alt="" />
-          <img src={reactLogo} className="framework" alt="React logo" />
-          <img src={viteLogo} className="vite" alt="Vite logo" />
-        </div>
-        <div>
-          <h1>Get started</h1>
-          <p>
-            Edit <code>src/App.jsx</code> and save to test <code>HMR</code>
-          </p>
-        </div>
-        <button
-          type="button"
-          className="counter"
-          onClick={() => setCount((count) => count + 1)}
-        >
-          Count is {count}
-        </button>
-      </section>
-
-      <div className="ticks"></div>
-
-      <section id="next-steps">
-        <div id="docs">
-          <svg className="icon" role="presentation" aria-hidden="true">
-            <use href="/icons.svg#documentation-icon"></use>
-          </svg>
-          <h2>Documentation</h2>
-          <p>Your questions, answered</p>
-          <ul>
-            <li>
-              <a href="https://vite.dev/" target="_blank">
-                <img className="logo" src={viteLogo} alt="" />
-                Explore Vite
-              </a>
-            </li>
-            <li>
-              <a href="https://react.dev/" target="_blank">
-                <img className="button-icon" src={reactLogo} alt="" />
-                Learn more
-              </a>
-            </li>
-          </ul>
-        </div>
-        <div id="social">
-          <svg className="icon" role="presentation" aria-hidden="true">
-            <use href="/icons.svg#social-icon"></use>
-          </svg>
-          <h2>Connect with us</h2>
-          <p>Join the Vite community</p>
-          <ul>
-            <li>
-              <a href="https://github.com/vitejs/vite" target="_blank">
-                <svg
-                  className="button-icon"
-                  role="presentation"
-                  aria-hidden="true"
-                >
-                  <use href="/icons.svg#github-icon"></use>
-                </svg>
-                GitHub
-              </a>
-            </li>
-            <li>
-              <a href="https://chat.vite.dev/" target="_blank">
-                <svg
-                  className="button-icon"
-                  role="presentation"
-                  aria-hidden="true"
-                >
-                  <use href="/icons.svg#discord-icon"></use>
-                </svg>
-                Discord
-              </a>
-            </li>
-            <li>
-              <a href="https://x.com/vite_js" target="_blank">
-                <svg
-                  className="button-icon"
-                  role="presentation"
-                  aria-hidden="true"
-                >
-                  <use href="/icons.svg#x-icon"></use>
-                </svg>
-                X.com
-              </a>
-            </li>
-            <li>
-              <a href="https://bsky.app/profile/vite.dev" target="_blank">
-                <svg
-                  className="button-icon"
-                  role="presentation"
-                  aria-hidden="true"
-                >
-                  <use href="/icons.svg#bluesky-icon"></use>
-                </svg>
-                Bluesky
-              </a>
-            </li>
-          </ul>
-        </div>
-      </section>
-
-      <div className="ticks"></div>
-      <section id="spacer"></section>
-    </>
+    <div>
+      <CountPage></CountPage>
+    </div>
   )
 }
 
-export default App
+export default App;

+ 18 - 0
21.react/高阶/project5/src/components/CountPage.jsx

@@ -0,0 +1,18 @@
+import { useDispatch, useSelector } from "react-redux";
+// useSelector 读数据  你选择的state片段 useSelector()
+import { addOne } from "../store/slices/counterSlice";
+function CountPage() {
+    const { value } = useSelector(x => x.counterSlice);
+    const dispatch = useDispatch();
+    // useDispatch 写数据 触发函数 
+    console.log(dispatch, 'dd')
+    return (
+        <div>
+            <h1>计数器</h1>
+            <h2>初始值:{value}</h2>
+            <button onClick={() => dispatch(addOne())}>加一</button>
+        </div>
+    )
+}
+
+export default CountPage;

+ 1 - 1
21.react/高阶/project5/src/main.jsx

@@ -2,7 +2,7 @@ import { StrictMode } from 'react'
 import { createRoot } from 'react-dom/client'
 import { Provider } from 'react-redux';
 import store from './store/index.js';
-import './index.css'
+// import './index.css'
 import App from './App.jsx'
 
 createRoot(document.getElementById('root')).render(

+ 2 - 1
21.react/高阶/project5/src/store/slices/counterSlice.js

@@ -14,6 +14,7 @@ const counterSlice = createSlice({
     }
 })
 
-
+// 抛出方法
+export const { addOne } = counterSlice.actions;
 // 抛出切片
 export default counterSlice.reducer;