Browse Source

practise

master
MYW 2 years ago
parent
commit
291edb53a3
  1. 23
      mobx-todo-mvc-P92-97/.gitignore
  2. 70
      mobx-todo-mvc-P92-97/README.md
  3. 28628
      mobx-todo-mvc-P92-97/package-lock.json
  4. 42
      mobx-todo-mvc-P92-97/package.json
  5. BIN
      mobx-todo-mvc-P92-97/public/favicon.ico
  6. 43
      mobx-todo-mvc-P92-97/public/index.html
  7. BIN
      mobx-todo-mvc-P92-97/public/logo192.png
  8. BIN
      mobx-todo-mvc-P92-97/public/logo512.png
  9. 25
      mobx-todo-mvc-P92-97/public/manifest.json
  10. 3
      mobx-todo-mvc-P92-97/public/robots.txt
  11. 8
      mobx-todo-mvc-P92-97/src/App.js
  12. 379
      mobx-todo-mvc-P92-97/src/Todo/index.css
  13. 101
      mobx-todo-mvc-P92-97/src/Todo/index.js
  14. 9
      mobx-todo-mvc-P92-97/src/index.js
  15. 1
      mobx-todo-mvc-P92-97/src/logo.svg
  16. 13
      mobx-todo-mvc-P92-97/src/store/index.js
  17. 60
      mobx-todo-mvc-P92-97/src/store/task.Store.js
  18. 54
      practise/31.hook/01.useState的基础使用.jsx
  19. 8
      practise/31.hook/03.useEffect副作用.jsx
  20. 6
      practise/31.hook/hook阶段小练习/02/useLocalStorage.js
  21. 30
      practise/33.Mobx/7.mobx模块化/App.js
  22. 33
      practise/33.Mobx/7.mobx模块化/counter.Store.js
  23. 28
      practise/33.Mobx/7.mobx模块化/index.js
  24. 15
      practise/33.Mobx/7.mobx模块化/list.Store.js

23
mobx-todo-mvc-P92-97/.gitignore

@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
# dependencies
/node_modules
/.pnp
.pnp.js
# testing
/coverage
# production
/build
# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local
npm-debug.log*
yarn-debug.log*
yarn-error.log*

70
mobx-todo-mvc-P92-97/README.md

@ -0,0 +1,70 @@
# Getting Started with Create React App
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
## Available Scripts
In the project directory, you can run:
### `npm start`
Runs the app in the development mode.\
Open [http://localhost:3000](http://localhost:3000) to view it in your browser.
The page will reload when you make changes.\
You may also see any lint errors in the console.
### `npm test`
Launches the test runner in the interactive watch mode.\
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
### `npm run build`
Builds the app for production to the `build` folder.\
It correctly bundles React in production mode and optimizes the build for the best performance.
The build is minified and the filenames include the hashes.\
Your app is ready to be deployed!
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
### `npm run eject`
**Note: this is a one-way operation. Once you `eject`, you can't go back!**
If you aren't satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you're on your own.
You don't have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn't feel obligated to use this feature. However we understand that this tool wouldn't be useful if you couldn't customize it when you are ready for it.
## Learn More
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
To learn React, check out the [React documentation](https://reactjs.org/).
### Code Splitting
This section has moved here: [https://facebook.github.io/create-react-app/docs/code-splitting](https://facebook.github.io/create-react-app/docs/code-splitting)
### Analyzing the Bundle Size
This section has moved here: [https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size](https://facebook.github.io/create-react-app/docs/analyzing-the-bundle-size)
### Making a Progressive Web App
This section has moved here: [https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app](https://facebook.github.io/create-react-app/docs/making-a-progressive-web-app)
### Advanced Configuration
This section has moved here: [https://facebook.github.io/create-react-app/docs/advanced-configuration](https://facebook.github.io/create-react-app/docs/advanced-configuration)
### Deployment
This section has moved here: [https://facebook.github.io/create-react-app/docs/deployment](https://facebook.github.io/create-react-app/docs/deployment)
### `npm run build` fails to minify
This section has moved here: [https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify](https://facebook.github.io/create-react-app/docs/troubleshooting#npm-run-build-fails-to-minify)

28628
mobx-todo-mvc-P92-97/package-lock.json
File diff suppressed because it is too large
View File

42
mobx-todo-mvc-P92-97/package.json

@ -0,0 +1,42 @@
{
"name": "react-01",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0",
"@testing-library/user-event": "^13.5.0",
"mobx": "^6.6.1",
"mobx-react-lite": "^3.4.0",
"prop-type": "^0.0.1",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-scripts": "5.0.1",
"uuid": "^8.3.2",
"web-vitals": "^2.1.4"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
}
}

BIN
mobx-todo-mvc-P92-97/public/favicon.ico

43
mobx-todo-mvc-P92-97/public/index.html

@ -0,0 +1,43 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
-->
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Only files inside the `public` folder can be referenced from the HTML.
Unlike "/favicon.ico" or "favicon.ico", "%PUBLIC_URL%/favicon.ico" will
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
<!--
This HTML file is a template.
If you open it directly in the browser, you will see an empty page.
You can add webfonts, meta tags, or analytics to this file.
The build step will place the bundled scripts into the <body> tag.
To begin the development, run `npm start` or `yarn start`.
To create a production bundle, use `npm run build` or `yarn build`.
-->
</body>
</html>

BIN
mobx-todo-mvc-P92-97/public/logo192.png

After

Width: 192  |  Height: 192  |  Size: 5.2 KiB

BIN
mobx-todo-mvc-P92-97/public/logo512.png

After

Width: 512  |  Height: 512  |  Size: 9.4 KiB

25
mobx-todo-mvc-P92-97/public/manifest.json

@ -0,0 +1,25 @@
{
"short_name": "React App",
"name": "Create React App Sample",
"icons": [
{
"src": "favicon.ico",
"sizes": "64x64 32x32 24x24 16x16",
"type": "image/x-icon"
},
{
"src": "logo192.png",
"type": "image/png",
"sizes": "192x192"
},
{
"src": "logo512.png",
"type": "image/png",
"sizes": "512x512"
}
],
"start_url": ".",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff"
}

3
mobx-todo-mvc-P92-97/public/robots.txt

@ -0,0 +1,3 @@
# https://www.robotstxt.org/robotstxt.html
User-agent: *
Disallow:

8
mobx-todo-mvc-P92-97/src/App.js

@ -0,0 +1,8 @@
import Todo from './Todo/index'
function App() {
return (
<Todo />
)
}
export default App

379
mobx-todo-mvc-P92-97/src/Todo/index.css

@ -0,0 +1,379 @@
html,
body {
margin: 0;
padding: 0;
}
button {
margin: 0;
padding: 0;
border: 0;
background: none;
font-size: 100%;
vertical-align: baseline;
font-family: inherit;
font-weight: inherit;
color: inherit;
-webkit-appearance: none;
appearance: none;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
body {
font: 14px 'Helvetica Neue', Helvetica, Arial, sans-serif;
line-height: 1.4em;
background: #f5f5f5;
color: #4d4d4d;
min-width: 230px;
max-width: 550px;
margin: 0 auto;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
font-weight: 300;
}
:focus {
outline: 0;
}
.hidden {
display: none;
}
.todoapp {
background: #fff;
margin: 130px 0 40px 0;
position: relative;
box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.2),
0 25px 50px 0 rgba(0, 0, 0, 0.1);
}
.todoapp input::-webkit-input-placeholder {
font-style: italic;
font-weight: 300;
color: #e6e6e6;
}
.todoapp input::-moz-placeholder {
font-style: italic;
font-weight: 300;
color: #e6e6e6;
}
.todoapp input::input-placeholder {
font-style: italic;
font-weight: 300;
color: #e6e6e6;
}
.todoapp h1 {
position: absolute;
top: -155px;
width: 100%;
font-size: 100px;
font-weight: 100;
text-align: center;
color: rgba(175, 47, 47, 0.15);
-webkit-text-rendering: optimizeLegibility;
-moz-text-rendering: optimizeLegibility;
text-rendering: optimizeLegibility;
}
.new-todo,
.edit {
position: relative;
margin: 0;
width: 100%;
font-size: 24px;
font-family: inherit;
font-weight: inherit;
line-height: 1.4em;
border: 0;
color: inherit;
padding: 6px;
border: 1px solid #999;
box-shadow: inset 0 -1px 5px 0 rgba(0, 0, 0, 0.2);
box-sizing: border-box;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
.new-todo {
padding: 16px 16px 16px 60px;
border: none;
background: rgba(0, 0, 0, 0.003);
box-shadow: inset 0 -2px 1px rgba(0,0,0,0.03);
}
.main {
position: relative;
z-index: 2;
border-top: 1px solid #e6e6e6;
}
.toggle-all {
width: 1px;
height: 1px;
border: none; /* Mobile Safari */
opacity: 0;
position: absolute;
right: 100%;
bottom: 100%;
}
.toggle-all + label {
width: 60px;
height: 34px;
font-size: 0;
position: absolute;
top: -52px;
left: -13px;
-webkit-transform: rotate(90deg);
transform: rotate(90deg);
}
.toggle-all + label:before {
content: '❯';
font-size: 22px;
color: #e6e6e6;
padding: 10px 27px 10px 27px;
}
.toggle-all:checked + label:before {
color: #737373;
}
.todo-list {
margin: 0;
padding: 0;
list-style: none;
}
.todo-list li {
position: relative;
font-size: 24px;
border-bottom: 1px solid #ededed;
}
.todo-list li:last-child {
border-bottom: none;
}
.todo-list li.editing {
border-bottom: none;
padding: 0;
}
.todo-list li.editing .edit {
display: block;
width: calc(100% - 43px);
padding: 12px 16px;
margin: 0 0 0 43px;
}
.todo-list li.editing .view {
display: none;
}
.todo-list li .toggle {
text-align: center;
width: 40px;
/* auto, since non-WebKit browsers doesn't support input styling */
height: auto;
position: absolute;
top: 0;
bottom: 0;
margin: auto 0;
border: none; /* Mobile Safari */
-webkit-appearance: none;
appearance: none;
}
.todo-list li .toggle {
opacity: 0;
}
.todo-list li .toggle + label {
/*
Firefox requires `#` to be escaped - https://bugzilla.mozilla.org/show_bug.cgi?id=922433
IE and Edge requires *everything* to be escaped to render, so we do that instead of just the `#` - https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/7157459/
*/
background-image: url('data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%23ededed%22%20stroke-width%3D%223%22/%3E%3C/svg%3E');
background-repeat: no-repeat;
background-position: center left;
}
.todo-list li .toggle:checked + label {
background-image: url('data:image/svg+xml;utf8,%3Csvg%20xmlns%3D%22http%3A//www.w3.org/2000/svg%22%20width%3D%2240%22%20height%3D%2240%22%20viewBox%3D%22-10%20-18%20100%20135%22%3E%3Ccircle%20cx%3D%2250%22%20cy%3D%2250%22%20r%3D%2250%22%20fill%3D%22none%22%20stroke%3D%22%23bddad5%22%20stroke-width%3D%223%22/%3E%3Cpath%20fill%3D%22%235dc2af%22%20d%3D%22M72%2025L42%2071%2027%2056l-4%204%2020%2020%2034-52z%22/%3E%3C/svg%3E');
}
.todo-list li label {
word-break: break-all;
padding: 15px 15px 15px 60px;
display: block;
line-height: 1.2;
transition: color 0.4s;
}
.todo-list li.completed label {
color: #d9d9d9;
text-decoration: line-through;
}
.todo-list li .destroy {
display: none;
position: absolute;
top: 0;
right: 10px;
bottom: 0;
width: 40px;
height: 40px;
margin: auto 0;
font-size: 30px;
color: #cc9a9a;
margin-bottom: 11px;
transition: color 0.2s ease-out;
}
.todo-list li .destroy:hover {
color: #af5b5e;
}
.todo-list li .destroy:after {
content: '×';
}
.todo-list li:hover .destroy {
display: block;
}
.todo-list li .edit {
display: none;
}
.todo-list li.editing:last-child {
margin-bottom: -1px;
}
.footer {
color: #777;
padding: 10px 15px;
height: 20px;
text-align: center;
border-top: 1px solid #e6e6e6;
}
.footer:before {
content: '';
position: absolute;
right: 0;
bottom: 0;
left: 0;
height: 50px;
overflow: hidden;
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.2),
0 8px 0 -3px #f6f6f6,
0 9px 1px -3px rgba(0, 0, 0, 0.2),
0 16px 0 -6px #f6f6f6,
0 17px 2px -6px rgba(0, 0, 0, 0.2);
}
.todo-count {
float: left;
text-align: left;
}
.todo-count strong {
font-weight: 300;
}
.filters {
margin: 0;
padding: 0;
list-style: none;
position: absolute;
right: 0;
left: 0;
}
.filters li {
display: inline;
}
.filters li a {
color: inherit;
margin: 3px;
padding: 3px 7px;
text-decoration: none;
border: 1px solid transparent;
border-radius: 3px;
}
.filters li a:hover {
border-color: rgba(175, 47, 47, 0.1);
}
.filters li a.selected {
border-color: rgba(175, 47, 47, 0.2);
}
.clear-completed,
html .clear-completed:active {
float: right;
position: relative;
line-height: 20px;
text-decoration: none;
cursor: pointer;
}
.clear-completed:hover {
text-decoration: underline;
}
.info {
margin: 65px auto 0;
color: #bfbfbf;
font-size: 10px;
text-shadow: 0 1px 0 rgba(255, 255, 255, 0.5);
text-align: center;
}
.info p {
line-height: 1;
}
.info a {
color: inherit;
text-decoration: none;
font-weight: 400;
}
.info a:hover {
text-decoration: underline;
}
/*
Hack to remove background from Mobile Safari.
Can't use it globally since it destroys checkboxes in Firefox
*/
@media screen and (-webkit-min-device-pixel-ratio:0) {
.toggle-all,
.todo-list li .toggle {
background: none;
}
.todo-list li .toggle {
height: 40px;
}
}
@media (max-width: 430px) {
.footer {
height: 50px;
}
.filters {
bottom: 10px;
}
}

101
mobx-todo-mvc-P92-97/src/Todo/index.js

@ -0,0 +1,101 @@
import './index.css'
import { useStore } from '../store/index'
import { observer } from 'mobx-react-lite'
import { useState } from 'react'
import { v4 as uuid } from 'uuid'
function Task() {
// useStore
const { taskStore } = useStore()
// 单选受控
// 通过 mobx 里的 Store 去维护状态,input 只需把 e.target.value 交给 store,让 store 来进行修改
function onChange(id, e) {
console.log(id, e)
taskStore.singleCheck(id, e.target.checked)
}
// 全选
function allChange(e) {
console.log(e)
taskStore.allCheck(e.target.checked)
}
// 删除
function delTask(id) {
taskStore.delTask(id)
}
// 新增
const [taskValue, setTaskValue] = useState('')
function addTask (e) {
console.log(e)
if (e.keyCode === 13) {
taskStore.addTask (
{
id: uuid(),
name: taskValue,
isDone: false
}
)
setTaskValue('')
}
}
return (
<section className="todoapp">
<header className="header">
<h1>todos</h1>
{/* 新增输入框 */}
<input
className="new-todo"
autoFocus
autoComplete="off"
value={taskValue}
onChange={(e) => setTaskValue(e.target.value)}
onKeyUp={addTask}
placeholder="What needs to be done?"
></input>
</header>
<section className="main">
{/* 全选 */}
<input
id="toggle-all"
className="toggle-all"
type="checkbox"
checked={taskStore.isAll}
onChange={allChange}
/>
<label htmlFor="toggle-all"></label>
<ul className="todo-list">
{/* completed 类名标识 */}
{taskStore.list.map(item => (
<li
className={item.isDone ? 'todo completed' : 'todo'}
key={item.id}>
<div className="view">
{/* 单选框 单选受控*/}
<input
className="toggle"
type="checkbox"
onChange={(e) => onChange(item.id, e)}
checked={item.isDone} />
<label>{item.name}</label>
{/* 删除 */}
<button className="destroy" onClick={() => delTask(item.id)}></button>
</div>
</li>
))}
</ul>
</section>
<footer className="footer">
<span className="todo-count">
任务总数{taskStore.list.length} 已完成{taskStore.isFinishedLength}
</span>
</footer>
</section>
)
}
export default observer(Task)

9
mobx-todo-mvc-P92-97/src/index.js

@ -0,0 +1,9 @@
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App';
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<App />
);

1
mobx-todo-mvc-P92-97/src/logo.svg

@ -0,0 +1 @@
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 841.9 595.3"><g fill="#61DAFB"><path d="M666.3 296.5c0-32.5-40.7-63.3-103.1-82.4 14.4-63.6 8-114.2-20.2-130.4-6.5-3.8-14.1-5.6-22.4-5.6v22.3c4.6 0 8.3.9 11.4 2.6 13.6 7.8 19.5 37.5 14.9 75.7-1.1 9.4-2.9 19.3-5.1 29.4-19.6-4.8-41-8.5-63.5-10.9-13.5-18.5-27.5-35.3-41.6-50 32.6-30.3 63.2-46.9 84-46.9V78c-27.5 0-63.5 19.6-99.9 53.6-36.4-33.8-72.4-53.2-99.9-53.2v22.3c20.7 0 51.4 16.5 84 46.6-14 14.7-28 31.4-41.3 49.9-22.6 2.4-44 6.1-63.6 11-2.3-10-4-19.7-5.2-29-4.7-38.2 1.1-67.9 14.6-75.8 3-1.8 6.9-2.6 11.5-2.6V78.5c-8.4 0-16 1.8-22.6 5.6-28.1 16.2-34.4 66.7-19.9 130.1-62.2 19.2-102.7 49.9-102.7 82.3 0 32.5 40.7 63.3 103.1 82.4-14.4 63.6-8 114.2 20.2 130.4 6.5 3.8 14.1 5.6 22.5 5.6 27.5 0 63.5-19.6 99.9-53.6 36.4 33.8 72.4 53.2 99.9 53.2 8.4 0 16-1.8 22.6-5.6 28.1-16.2 34.4-66.7 19.9-130.1 62-19.1 102.5-49.9 102.5-82.3zm-130.2-66.7c-3.7 12.9-8.3 26.2-13.5 39.5-4.1-8-8.4-16-13.1-24-4.6-8-9.5-15.8-14.4-23.4 14.2 2.1 27.9 4.7 41 7.9zm-45.8 106.5c-7.8 13.5-15.8 26.3-24.1 38.2-14.9 1.3-30 2-45.2 2-15.1 0-30.2-.7-45-1.9-8.3-11.9-16.4-24.6-24.2-38-7.6-13.1-14.5-26.4-20.8-39.8 6.2-13.4 13.2-26.8 20.7-39.9 7.8-13.5 15.8-26.3 24.1-38.2 14.9-1.3 30-2 45.2-2 15.1 0 30.2.7 45 1.9 8.3 11.9 16.4 24.6 24.2 38 7.6 13.1 14.5 26.4 20.8 39.8-6.3 13.4-13.2 26.8-20.7 39.9zm32.3-13c5.4 13.4 10 26.8 13.8 39.8-13.1 3.2-26.9 5.9-41.2 8 4.9-7.7 9.8-15.6 14.4-23.7 4.6-8 8.9-16.1 13-24.1zM421.2 430c-9.3-9.6-18.6-20.3-27.8-32 9 .4 18.2.7 27.5.7 9.4 0 18.7-.2 27.8-.7-9 11.7-18.3 22.4-27.5 32zm-74.4-58.9c-14.2-2.1-27.9-4.7-41-7.9 3.7-12.9 8.3-26.2 13.5-39.5 4.1 8 8.4 16 13.1 24 4.7 8 9.5 15.8 14.4 23.4zM420.7 163c9.3 9.6 18.6 20.3 27.8 32-9-.4-18.2-.7-27.5-.7-9.4 0-18.7.2-27.8.7 9-11.7 18.3-22.4 27.5-32zm-74 58.9c-4.9 7.7-9.8 15.6-14.4 23.7-4.6 8-8.9 16-13 24-5.4-13.4-10-26.8-13.8-39.8 13.1-3.1 26.9-5.8 41.2-7.9zm-90.5 125.2c-35.4-15.1-58.3-34.9-58.3-50.6 0-15.7 22.9-35.6 58.3-50.6 8.6-3.7 18-7 27.7-10.1 5.7 19.6 13.2 40 22.5 60.9-9.2 20.8-16.6 41.1-22.2 60.6-9.9-3.1-19.3-6.5-28-10.2zM310 490c-13.6-7.8-19.5-37.5-14.9-75.7 1.1-9.4 2.9-19.3 5.1-29.4 19.6 4.8 41 8.5 63.5 10.9 13.5 18.5 27.5 35.3 41.6 50-32.6 30.3-63.2 46.9-84 46.9-4.5-.1-8.3-1-11.3-2.7zm237.2-76.2c4.7 38.2-1.1 67.9-14.6 75.8-3 1.8-6.9 2.6-11.5 2.6-20.7 0-51.4-16.5-84-46.6 14-14.7 28-31.4 41.3-49.9 22.6-2.4 44-6.1 63.6-11 2.3 10.1 4.1 19.8 5.2 29.1zm38.5-66.7c-8.6 3.7-18 7-27.7 10.1-5.7-19.6-13.2-40-22.5-60.9 9.2-20.8 16.6-41.1 22.2-60.6 9.9 3.1 19.3 6.5 28.1 10.2 35.4 15.1 58.3 34.9 58.3 50.6-.1 15.7-23 35.6-58.4 50.6zM320.8 78.4z"/><circle cx="420.9" cy="296.5" r="45.7"/><path d="M520.5 78.1z"/></g></svg>

13
mobx-todo-mvc-P92-97/src/store/index.js

@ -0,0 +1,13 @@
// 导入模块
import TaskStore from "./task.Store"
import React from "react"
class RootStore {
// 组合模块
constructor() {
this.taskStore = new TaskStore()
}
}
// 实例化根store注入context
const StoresContext = React.createContext(new RootStore())
// 导出方法 供组件调用方法使用store根实例
export const useStore = () => React.useContext(StoresContext)

60
mobx-todo-mvc-P92-97/src/store/task.Store.js

@ -0,0 +1,60 @@
import { makeAutoObservable } from 'mobx'
class TaskStore {
list = [
{
id: 1,
name: '学习 react',
isDone: true
},
{
id: 2,
name: '学习 Vue',
isDone: false
},
]
constructor() {
makeAutoObservable(this)
}
// 单选操作
singleCheck (id, isDone) {
// 查找 - find() 方法返回通过测试(函数内判断)的数组的第一个元素的值。
const item = this.list.find(item => item.id === id)
item.isDone = isDone
}
// 全选操作
allCheck(checked) {
// 把所有项的 isDone 属性,都按照传入的最新的状态修改
this.list.forEach(item => {
item.isDone = checked
})
}
// 计算属性
// 只有所有子选项都是选中的时,全选才是选中的状态
get isAll () {
// every() 方法用于检测数组所有元素是否都符合指定条件
return this.list.every(item => item.isDone)
}
// 已完成的数量
get isFinishedLength () {
return this.list.filter(item => item.isDone).length
}
// 删除
delTask = (id) => {
this.list = this.list.filter(item => item.id !== id)
}
// 新增
addTask = (task) => {
this.list.push(task)
console.log(this.list)
}
}
export default TaskStore

54
practise/31.hook/01.useState的基础使用.jsx

@ -1,29 +1,31 @@
/* useState
1.导入 useState 函数 react 导入
2.执行这个函数并且传入初始值必须在函数组件中
3.解构 [数据修改数据的方法]
4.使用数据 修改数据 */
/*
const [count, setCount] = useState(0)
1.useState 传过来的参数作为 count 的初始值
2. [count, setCount] 这里的写法是一个解构赋值useState 返回值是一个数组
3. [] 里面的值名字可以自定义保持语义化
4. [] 里面的值顺序不能更换第一个参数是数据状态第二参数是修改数据的方法
5. setCount 函数作用用来修改 count, 依旧保持不能直接修改原值要生成一个新值替换原值setCount基于原值计算得到的新值
6. count setCount 是一对的绑定在一起的setCount 只能用来修改对应的 count */
/*
当调用 setCount 的时候更新过程
首次渲染
首次被渲染的时候组件的内部代码会被执行一次其中 useState 也会跟着执行重点注意初始值只在首次渲染是生效
更新渲染 setCount 都会更新
1. app 组件会再次渲染这个函数会再次执行
2. useState 再次执行得到的新的 count 值不是 0 而是修改之后的 1模板会用新值渲染
重点useState 初始值只在首次渲染生效后续只要调用 setCount整个 app 中代码都会执行此时的 count 每次拿到的都是最新值
/** useState
* 1.导入 useState 函数 react 导入
* 2.执行这个函数并且传入初始值必须在函数组件中
* 3.解构 [数据修改数据的方法]
* 4.使用数据 修改数据 */
/**
* const [count, setCount] = useState(0)
* 1.useState 传过来的参数作为 count 的初始值
* 2. [count, setCount] 这里的写法是一个解构赋值useState 返回值是一个数组
* 3. [] 里面的值名字可以自定义保持语义化
* 4. [] 里面的值顺序不能更换第一个参数是数据状态第二参数是修改数据的方法
* 5. setCount 函数作用用来修改 count, 依旧保持不能直接修改原值要生成一个新值替换原值setCount基于原值计算得到的新值
* 6. count setCount 是一对的绑定在一起的setCount 只能用来修改对应的 count
*/
/**
* 当调用 setCount 的时候更新过程
*
* 首次渲染
* 首次被渲染的时候组件的内部代码会被执行一次其中 useState 也会跟着执行重点注意初始值只在首次渲染是生效
*
* 更新渲染 setCount 都会更新
* 1. app 组件会再次渲染这个函数会再次执行
* 2. useState 再次执行得到的新的 count 值不是 0 而是修改之后的 1模板会用新值渲染
*
* 重点useState 初始值只在首次渲染生效后续只要调用 setCount整个 app 中代码都会执行此时的 count 每次拿到的都是最新值
*/
import { useState } from 'react'

8
practise/31.hook/03.useEffect副作用.jsx

@ -2,10 +2,10 @@ import { useState, useEffect } from 'react'
// useEffect, react
/* count
1.导入 useEffect 函数
2.在函数组件中执行传入回调并且定义副作用
3.当通过修改状态更新组件时副作用也会不断执行 */
/** count
* 1.导入 useEffect 函数
* 2.在函数组件中执行传入回调并且定义副作用
* 3.当通过修改状态更新组件时副作用也会不断执行 */
function App () {
const [count, setCount] = useState(0) // 0 count

6
practise/31.hook/hook阶段小练习/02/useLocalStorage.js

@ -1,8 +1,8 @@
import { useEffect, useState} from 'react'
/* const {message, setMessage} = useState(defaultValue)
1.message 可以通过自定义传入默认初始值
2.每次修改 message 数据的时候都会自动往同步一份 */
/** const {message, setMessage} = useState(defaultValue)
* 1.message 可以通过自定义传入默认初始值
* 2.每次修改 message 数据的时候都会自动往同步一份 */
export function useLocalStorage (key, defaultValue) {
const [message, setMessage] = useState(defaultValue)

30
practise/33.Mobx/7.mobx模块化/App.js

@ -0,0 +1,30 @@
// 导入中间件,连接mobx、react,完成响应式变化
import { observer } from 'mobx-react-lite'
import { useStore } from './store/index'
function App() {
// 注意:解构赋值到 store 实例对象就可以了,防止破坏响应式,用哪个 store 就解构那个 store
const { counterStore } = useStore()
return (
<div className="App">
{/* list = [1, 2, 3, 4, 5, 6] 是引用型数据类型,值存放在堆内存中,在栈内存存放的是一个地址,这个地址将会指向堆内存中引用数据类型的具体数值 */}
{counterStore.list.map((item) => {
return <div key={item}>{item}</div>;
})}
<br />
<button onClick={() => {
counterStore.addList();
console.log(counterStore.list);
}
}>+</button>
<br />
{/* {counterStore.count}
<br/>
<button onClick={counterStore.addCount}>+</button> */}
</div>
)
}
export default observer(App)

33
practise/33.Mobx/7.mobx模块化/counter.Store.js

@ -0,0 +1,33 @@
// 初始化 mobx
import { makeAutoObservable } from 'mobx'
class CounterStore {
count = 0
// 定义一个原始数据 list
list = [1, 2, 3, 4, 5, 6]
// constructor() 是构造函数 作用:1.初始化this.state 2.函数方法绑定到实例。
constructor() {
// 将数据弄成响应式
makeAutoObservable(this)
}
// 定义计算属性
// get() 方法通过索引值获取动态数组中的元素。
get filterList () {
return this.list.filter(item => item > 2)
}
// 修改 list
addList = () => {
this.list.push(7, 8, 9)
//console.log(this.list)
}
addCount = () => {
this.count++
}
}
export {CounterStore}

28
practise/33.Mobx/7.mobx模块化/index.js

@ -0,0 +1,28 @@
// 组合子模块(根组件)
// 封装统一导出的供业务使用的方法
import React from 'react'
import { ListStore } from "./list.Store";
import { CounterStore } from "./counter.Store"
// 1.声明一个 RootStore
class RootStore {
constructor() {
// 对子模块进行实例化操作
// 将来实例化根 RootStore 的时候,根 RootStore 有两个属性,分别是 CounterStore 和 ListStore,各自对应的值就是导入的子模块实例对象
this.counterStore = new CounterStore()
this.listStore = new ListStore()
}
}
// 实例化操作
RootStore = new RootStore()
// 使用 react context 机制,完成统一的方法封装
// 通过 Provider value={传递的数据} 的查找机制:当调用 useContext,优先从 Provider value 找,如果找不到,就会 createContext 方法传递过来的默认参数
const context = React.createContext(RootStore)
// 这个方法作用:通过 useContext 拿到 RootStore 实例对象,然后返回,所以只要在业务组件中,调用 useStore(),将获得一个跟实例对象 RootStore
const useStore = () => React.useContext(context)
export { useStore }

15
practise/33.Mobx/7.mobx模块化/list.Store.js

@ -0,0 +1,15 @@
import { makeAutoObservable } from "mobx";
class ListStore {
list = ['vue', 'react']
constructor() {
makeAutoObservable(this)
}
addList = () => {
this.list.push('angular')
}
}
export { ListStore }
Loading…
Cancel
Save