Commit bbec5115 authored by 郑云峰's avatar 郑云峰

refactor: 重构mobx

parent 2140e2c5
import RootStore from './rootStore'; import { makeAutoObservable } from 'mobx';
import { UserStore } from './userStore';
const rootStore = new RootStore(); export class Store {
/** userStore: UserStore = new UserStore();
* 这里只做根Store的导出
* 预留以后可能会出现 额外的Store
*/
const Store = {
...rootStore,
};
export default Store; constructor() {
makeAutoObservable(this);
}
}
import { makeAutoObservable } from 'mobx';
import UserStore from '../userStore';
import { UserStoreModel } from '../userStore/model';
class RootStore {
userStore: UserStoreModel;
constructor() {
makeAutoObservable(this);
this.userStore = new UserStore(this);
}
}
export default RootStore;
import { UserStoreModel } from '../userStore/model';
export interface RootStoreModel {
userStore: UserStoreModel;
}
import React, { import React, { createContext, useContext } from 'react';
FC, import { Store } from '.';
createContext,
ReactNode,
ReactElement,
useContext,
} from 'react';
import { RootStoreModel } from './rootStore/model';
export const StoreContext = createContext<RootStoreModel>({} as RootStoreModel); const StoreContext = createContext<Store>(new Store());
export type StoreComponent = FC<{ const store = new Store();
store: RootStoreModel;
children: ReactNode;
}>;
// eslint-disable-next-line react/prop-types const StoreProvider = () => {
export const StoreProvider: StoreComponent = ({ return <StoreContext.Provider value={store}></StoreContext.Provider>;
children, };
store,
}): ReactElement => (
<StoreContext.Provider value={store}>{children}</StoreContext.Provider>
);
/** export const useStores = () => useContext(StoreContext);
* 组件具体使用mobx的hooks
*/
export const useStores = (): RootStoreModel => useContext(StoreContext);
export default useStores;
import { makeObservable, observable, runInAction } from 'mobx'; export * from './userStore';
import { export * from './types';
getAsyncStorage,
setAsyncStorage,
removeAsyncStorage,
} from '../../utils/storage';
import { USER_INFO, TOKEN } from '../../constants';
import { RootStoreModel } from '../rootStore/model';
import { UserStoreModel, userInfoType } from './model';
export default class UserStore implements UserStoreModel {
private rootStore: RootStoreModel;
username = 'Bob';
userInfo: userInfoType | null = null;
constructor(rootStore: RootStoreModel) {
makeObservable(this, {
username: observable,
userInfo: observable,
});
this.rootStore = rootStore;
this.fetchUserInfo();
}
setUserName(name: string) {
this.username = name;
}
async fetchUserInfo() {
this.userInfo = await getAsyncStorage(USER_INFO);
}
// 用户登录时,或者修改用户信息的时候更新UserInfo
async setUserInfo(data: userInfoType) {
await setAsyncStorage(USER_INFO, data);
runInAction(() => {
this.userInfo = data;
});
}
async removeUserInfo() {
await removeAsyncStorage(TOKEN);
await removeAsyncStorage(USER_INFO);
// 商品分享口令生成数字
await removeAsyncStorage('SHARE_CODE_NUM');
runInAction(() => {
this.userInfo = null;
});
}
}
export type userInfoType = { export interface UserInfo {
account?: string; account?: string;
company?: string; company?: string;
countryCode?: string; countryCode?: string;
...@@ -20,12 +20,4 @@ export type userInfoType = { ...@@ -20,12 +20,4 @@ export type userInfoType = {
roleId: number; roleId: number;
roleName: string; roleName: string;
}[]; }[];
};
export interface UserStoreModel {
username: string;
setUserName: (name: string) => void;
userInfo: null | userInfoType;
removeUserInfo: () => void;
setUserInfo: (data: any) => void;
} }
import { makeObservable, observable, action, flow } from 'mobx';
import { TOKEN, USER_INFO } from '../../constants';
import { removeAsyncStorage, setAsyncStorage } from '../../utils/storage';
import { UserInfo } from './types';
export class UserStore {
username: string = '';
userInfo: UserInfo | null = null;
constructor() {
makeObservable(this, {
username: observable,
setUsername: action,
setUserInfo: flow,
removeUserInfo: flow,
});
}
setUsername(username: string) {
this.username = username;
}
removeUsername() {
this.username = '';
}
// 使用 flow 代替 async / await,具体可看mobx官方文档https://zh.mobx.js.org/actions.html#%E4%BD%BF%E7%94%A8-flow-%E4%BB%A3%E6%9B%BF-async--await-
*setUserInfo(userInfo: UserInfo) {
try {
yield setAsyncStorage(USER_INFO, userInfo);
this.userInfo = userInfo;
} catch (error) {
console.log('store存储用户信息失败----------', error);
this.userInfo = null;
}
}
*removeUserInfo() {
try {
yield removeAsyncStorage(TOKEN);
yield removeAsyncStorage(USER_INFO);
// 商品分享口令生成数字
yield removeAsyncStorage('SHARE_CODE_NUM');
// console.log('store移除用户信息-----Success');
this.userInfo = null;
} catch (error) {
console.log('store移除用户信息-----Error', error);
}
}
}
import React from 'react'; import React from 'react';
import { View, Button } from 'react-native'; import { View, Button } from 'react-native';
import { RootStackScreenProps } from '../../routers/types'; import { RootStackScreenProps } from '../../routers/types';
import useStores from '../../store/useStores'; import { useStores } from '../../store/useStores';
import { ContentItem } from './ContentItem'; import { ContentItem } from './ContentItem';
import { styles } from './styles'; import { styles } from './styles';
...@@ -21,8 +21,7 @@ const AccountInfo: React.FC<TestDetailsScreenNavigationProp> = ({ ...@@ -21,8 +21,7 @@ const AccountInfo: React.FC<TestDetailsScreenNavigationProp> = ({
const { userStore } = useStores(); const { userStore } = useStores();
const loginOut = () => { const loginOut = () => {
// removeUserInfo() userStore.removeUserInfo();
console.log(userStore);
navigation.navigate('BottomTabs', { navigation.navigate('BottomTabs', {
screen: 'PersonalCenter', screen: 'PersonalCenter',
......
...@@ -3,6 +3,7 @@ import { StackScreenProps } from '@react-navigation/stack'; ...@@ -3,6 +3,7 @@ import { StackScreenProps } from '@react-navigation/stack';
import React from 'react'; import React from 'react';
import { View, Text } from 'react-native'; import { View, Text } from 'react-native';
import { RootTabScreenProps, RootStackParamList } from '../../routers/types'; import { RootTabScreenProps, RootStackParamList } from '../../routers/types';
import { useStores } from '../../store/useStores';
import { styles } from './styles'; import { styles } from './styles';
type PersonalCenterNavigationProp = CompositeScreenProps< type PersonalCenterNavigationProp = CompositeScreenProps<
...@@ -13,8 +14,12 @@ type PersonalCenterNavigationProp = CompositeScreenProps< ...@@ -13,8 +14,12 @@ type PersonalCenterNavigationProp = CompositeScreenProps<
const PersonalCenter: React.FC<PersonalCenterNavigationProp> = ({ const PersonalCenter: React.FC<PersonalCenterNavigationProp> = ({
navigation, navigation,
}) => { }) => {
const { userStore } = useStores();
const goAccountInfo = () => { const goAccountInfo = () => {
navigation.navigate('AccountInfo'); navigation.navigate('AccountInfo');
// 测试mobx
userStore.setUsername('张三');
}; };
return ( return (
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment