Skip to content

Commit

Permalink
feature: ✨ 新增状态管理Mobx v6
Browse files Browse the repository at this point in the history
  • Loading branch information
guokaigdg committed Feb 13, 2023
1 parent b38e285 commit 992e188
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 5 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
- TypeScript v4
- webpack v5
- axios
- mobx-react-lite
- mobx v6
- mobx-react-lite v3
- react-router-dom v6
- postcss-px-to-viewport

Expand Down
10 changes: 10 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
"scripts": {
"dev": "npm run start",
"start": "env-cmd -r .env.json -e development node webpack/server",
"build": "npm run build:qa npm run build:prod",
"build:qa": "env-cmd -r .env.json -e qa webpack --config ./webpack/config/webpack.prod.js",
"build:prod": "env-cmd -r .env.json -e production webpack --config ./webpack/config/webpack.prod.js"
},
Expand Down Expand Up @@ -106,6 +105,8 @@
"webpackbar": "^5.0.2"
},
"dependencies": {
"mobx": "^6.8.0",
"mobx-react-lite": "^3.4.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.8.1"
Expand Down
16 changes: 16 additions & 0 deletions src/store/about/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {makeAutoObservable} from 'mobx';
import {globalStore} from '../global';

class About {
constructor() {
makeAutoObservable(this);
}
count = 0;
aboutAddCount = () => {
const {count: globalCount} = globalStore;

This comment has been minimized.

Copy link
@Oreo-Undefined

Oreo-Undefined Mar 30, 2023

接某处解构的评论:这里的解构其实就没问题,因为没有 track 的场景

This comment has been minimized.

Copy link
@sohucw

sohucw Apr 14, 2023

是没意义, 多种场景的使用都搞一下

this.count += globalCount;
};
}

const aboutStore = new About();
export {aboutStore};
18 changes: 18 additions & 0 deletions src/store/global/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import {makeAutoObservable} from 'mobx';

class Global {
constructor() {
makeAutoObservable(this);

This comment has been minimized.

Copy link
@Oreo-Undefined

Oreo-Undefined Mar 30, 2023

makeAutoObservable 与 makeObservable 的场景也可以都列出来,二者是有区别的。对于某些精细控制的优化场景,makeObservable 要更出色

This comment has been minimized.

Copy link
@guokaigdg

guokaigdg Apr 7, 2023

Author Owner

makeObservable 确实可以更加精细化定义,后续将2者区别与使用场景都补充上。

This comment has been minimized.

Copy link
@guokaigdg

guokaigdg Apr 13, 2023

Author Owner

🚀 补充说明了 commit看 这里

}
count = 0;
name = 'react';
addCount = () => {
this.count++;

This comment has been minimized.

Copy link
@Oreo-Undefined

Oreo-Undefined Mar 30, 2023

框架级别的源码不同于教程类的源码,这里严谨地,应该是全局开启 strict 模式后,状态的修改控制在 action 内,或者 runInAction。除了同步更新逻辑,也建议增加异步更新的流程作为示范。

This comment has been minimized.

Copy link
@guokaigdg

guokaigdg Apr 7, 2023

Author Owner

异步请求例子与runInAction已添加,在另外的commit中,参考这里

};
setName = (data: string) => {
this.name = data;
};
}

const globalStore = new Global();
export {globalStore};
12 changes: 12 additions & 0 deletions src/store/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import * as React from 'react';
import {configure} from 'mobx';
import {globalStore} from './global';
import {aboutStore} from './about';

configure({enforceActions: 'always'}); // 任何状态都能只能通过actions来修改,在实际开发中也包括新建状态。

This comment has been minimized.

Copy link
@Oreo-Undefined

Oreo-Undefined Mar 30, 2023

good

export const stores = {globalStore, aboutStore};

export const storesContext = React.createContext(stores);

This comment has been minimized.

Copy link
@Oreo-Undefined

Oreo-Undefined Mar 30, 2023

这里使用的 react 的 context API,但我没找到 Provider 在哪里。我们知道,context 提供状态跨层级传递,前提是根节点外要注入共享的数据,下游通过 Consumer/useContext 来访问共享数据。如果没有使用 Provider,那么下级无异于直接引入共享的数据,也就是 context 没起到应有的作用。
read:
https://react.dev/reference/react/createContext#provider

This comment has been minimized.

Copy link
@guokaigdg

guokaigdg Apr 13, 2023

Author Owner

这里


export const useStores = () => React.useContext(storesContext);
1 change: 1 addition & 0 deletions src/view/About/index.less
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.about-root {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
width: 100vw;
Expand Down
8 changes: 7 additions & 1 deletion src/view/About/index.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import React from 'react';
import {observer} from 'mobx-react-lite';
import {useStores} from '@/store';
import './index.less';

const About = () => {
const {globalStore} = useStores();
const {count, name} = globalStore;

This comment has been minimized.

Copy link
@Oreo-Undefined

Oreo-Undefined Mar 30, 2023

mobx 包装过的响应式数据是不能解构使用的,除非是不需要触发 reaction/rerender 的场景,这里的 count、name 将会失去响应性。

return (
<div className='about-root'>
<p>Hello About</p>
<p>{name}</p>
<p>{count}</p>
</div>
);
};

export default About;
export default observer(About);

This comment has been minimized.

Copy link
@Oreo-Undefined

Oreo-Undefined Mar 30, 2023

其实 mobx-react-lite 还有个 Observer 组件,比 track 整个 FC 的性能更佳

1 change: 1 addition & 0 deletions src/view/Home/HomeOne/index.less
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.home-one-root {
display: flex;
flex: 1;
flex-direction: column;
align-items: center;
justify-content: center;
box-sizing: border-box;
Expand Down
17 changes: 15 additions & 2 deletions src/view/Home/HomeOne/index.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import React from 'react';
import {observer} from 'mobx-react-lite';
import {useStores} from '@/store';
import './index.less';

const HomeOne = () => {
return <div className='home-one-root'>HomeOne</div>;
const {globalStore, aboutStore} = useStores();

This comment has been minimized.

Copy link
@Oreo-Undefined

Oreo-Undefined Mar 30, 2023

这个解构没问题,实例本身不具备响应性

const {count, name} = globalStore;
const {count: aboutCount} = aboutStore;

This comment has been minimized.

Copy link
@Oreo-Undefined

Oreo-Undefined Mar 30, 2023

这两个解构都不推荐


return (
<div className='home-one-root'>
HomeOne
<p>{name}</p>
<p> globalStore: {count}</p>
<p> aboutStore: {aboutCount}</p>
</div>
);
};

export default HomeOne;
export default observer(HomeOne);
17 changes: 17 additions & 0 deletions src/view/Home/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,24 @@
import React from 'react';
import {useNavigate, Link, Outlet} from 'react-router-dom';
import {useStores} from '@/store';
import './index.less';

function Home() {
const {globalStore, aboutStore} = useStores();
const {addCount, setName} = globalStore;
const {aboutAddCount} = aboutStore;
const navigate = useNavigate();

const handleClickLink = () => {
navigate('/home/one'); // 跳转路由
};
const handleClickAddCount = () => {
addCount();
aboutAddCount();
};
const handleClickSetName = () => {
setName(`Hello${Math.random() * 10}`);
};

return (
<div className='home-root'>
Expand All @@ -17,6 +28,12 @@ function Home() {
<div className='btn' onClick={handleClickLink}>
使用navigate方式跳转HomeOne navigate('/home/one')
</div>
<div className='btn' onClick={handleClickAddCount}>
Mobx数据更新-addCount
</div>
<div className='btn' onClick={handleClickSetName}>
Mobx数据更新-setName
</div>
</div>
<Outlet />
</div>
Expand Down
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6135,6 +6135,16 @@ mkdirp@^1.0.3, mkdirp@^1.0.4:
resolved "https://registry.npmmirror.com/mkdirp/-/mkdirp-1.0.4.tgz#3eb5ed62622756d79a5f0e2a221dfebad75c2f7e"
integrity sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==

mobx-react-lite@^3.4.0:
version "3.4.0"
resolved "https://registry.npmmirror.com/mobx-react-lite/-/mobx-react-lite-3.4.0.tgz#d59156a96889cdadad751e5e4dab95f28926dfff"
integrity sha512-bRuZp3C0itgLKHu/VNxi66DN/XVkQG7xtoBVWxpvC5FhAqbOCP21+nPhULjnzEqd7xBMybp6KwytdUpZKEgpIQ==

mobx@^6.8.0:
version "6.8.0"
resolved "https://registry.npmmirror.com/mobx/-/mobx-6.8.0.tgz#59051755fdb5c8a9f3f2e0a9b6abaf86bab7f843"
integrity sha512-+o/DrHa4zykFMSKfS8Z+CPSEg5LW9tSNGTuN8o6MF1GKxlfkSHSeJn5UtgxvPkGgaouplnrLXCF+duAsmm6FHQ==

mrmime@^1.0.0:
version "1.0.1"
resolved "https://registry.npmmirror.com/mrmime/-/mrmime-1.0.1.tgz#5f90c825fad4bdd41dc914eff5d1a8cfdaf24f27"
Expand Down

0 comments on commit 992e188

Please sign in to comment.