Skip to content

Commit 23fac04

Browse files
author
chj_damon
committed
fix: 针对context性能优化提供示例代码
1 parent 886a64c commit 23fac04

File tree

14 files changed

+2890
-111
lines changed

14 files changed

+2890
-111
lines changed

mock/route.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,6 @@ export default {
1111
name: 'hotel',
1212
apiUrl: '/hotel',
1313
description: '酒店',
14-
children: [
15-
{
16-
name: 'hotelInfo',
17-
apiUrl: '/hotel/hotelInfo',
18-
description: '酒店基本信息',
19-
},
20-
],
2114
},
2215
],
2316
code: 20000,

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"test": "jest --passWithNoTests --detectOpenHandles --watch",
2222
"tsc": "tsc -p ./tsconfig.json",
2323
"eslint:fix": "eslint --fix --ext .ts,.tsx src/",
24-
"prettier:fix": "prettier --check src/**/*.tsx --write",
24+
"prettier:fix": "prettier --check src/**/*.{ts,tsx} --write",
2525
"style:fix": "stylelint src/**/*.{css,less,scss} --fix"
2626
},
2727
"config": {
@@ -64,6 +64,7 @@
6464
"@types/jest": "^26.0.20",
6565
"@types/react": "^17.0.3",
6666
"@types/react-dom": "^17.0.2",
67+
"@umijs/fabric": "^2.5.7",
6768
"@umijs/plugin-esbuild": "^1.1.0",
6869
"antd-dayjs-webpack-plugin": "^1.0.6",
6970
"commitizen": "^4.2.3",

src/pages/auth/useAuthService.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
import getContextService from '@/utils/getContextService';
2-
import { useCallback } from 'react';
3-
4-
// 这个服务将被注册至全局
5-
export const AuthContext = getContextService(useAuthService);
2+
import { useCallback, useMemo } from 'react';
63

74
/**
85
* 权限验证服务
96
*
107
*/
118
export default function useAuthService() {
12-
const token = window.localStorage.getItem('accessToken');
9+
const getToken = useCallback(() => {
10+
return window.localStorage.getItem('accessToken');
11+
}, []);
1312

1413
const saveToken = useCallback((token: string) => {
1514
window.localStorage.setItem('accessToken', token);
@@ -20,9 +19,12 @@ export default function useAuthService() {
2019
window.localStorage.setItem('accessToken', '');
2120
}, []);
2221

23-
return {
24-
token,
22+
return useMemo(() => ({
23+
getToken,
2524
saveToken,
2625
logout,
27-
};
26+
}), []);
2827
}
28+
29+
// 这个服务将被注册至全局
30+
export const AuthContext = getContextService(useAuthService);

src/pages/auth/wrappers/auth.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { AuthContext } from '../useAuthService';
55

66
export default (props: IRouteComponentProps) => {
77
const authService = useContext(AuthContext);
8-
if (authService.token) {
8+
if (authService.getToken()) {
99
return props.children;
1010
}
1111
return <Redirect to="/auth/login" />;

src/pages/hotel/HotelInfo/index.tsx

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
11
import { Button } from 'antd';
22
import { Link } from 'umi';
3-
import React, { memo, useContext } from 'react';
3+
import React, { useContext } from 'react';
4+
import { HotelDispatchContext } from '../useHotelService';
45
import HotelOtherInfo from '../HotelOtherInfo';
5-
import { HotelContext } from '../useHotelService';
6+
import HotelMetaInfo from '../HotelMetaInfo';
67

7-
const HotelInfo = () => {
8-
const hotelService = useContext(HotelContext);
8+
export default function HotelInfo() {
9+
const {setValue, setName} = useContext(HotelDispatchContext);
910

10-
const handleClick = () => {
11-
hotelService.setValue((val) => val + 1);
11+
const changeValue = () => {
12+
setValue(Math.random() * 100);
1213
};
1314

14-
console.log('222');
15+
const changeName = () => {
16+
setName((Math.random() * 100).toFixed(2));
17+
};
18+
19+
console.log('222')
1520
return (
1621
<div>
1722
<div>酒店基本信息</div>
18-
<Button onClick={handleClick}>修改value</Button>
19-
<HotelOtherInfo />
23+
<Button onClick={changeValue}>修改value</Button>
24+
<Button onClick={changeName}>修改name</Button>
2025
<Link to="/homepage">homepage</Link>
26+
<HotelOtherInfo />
27+
<HotelMetaInfo />
2128
</div>
2229
);
2330
};
24-
export default memo(HotelInfo);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import React, { useContext, useMemo } from 'react';
2+
import { HotelStateContext } from '../useHotelService';
3+
4+
export default function HotelMetaInfo() {
5+
const {name} = useContext(HotelStateContext);
6+
7+
console.log('444')
8+
return useMemo(() => (
9+
<div>
10+
name: {name}
11+
</div>
12+
), [name]);
13+
};

src/pages/hotel/HotelOtherInfo/index.tsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import React, { memo, useContext } from 'react';
2-
import { HotelContext } from '../useHotelService';
1+
import React, { useContext, useMemo } from 'react';
2+
import { HotelStateContext } from '../useHotelService';
33

4-
const HotelOtherInfo = () => {
5-
const hotelService = useContext(HotelContext);
6-
return <div>其他信息:{hotelService.value}</div>;
4+
export default function HotelOtherInfo() {
5+
const {value} = useContext(HotelStateContext);
6+
7+
console.log('333')
8+
return useMemo(() => (<div>value:{value}</div>), [value]);
79
};
810

9-
export default memo(HotelOtherInfo);

src/pages/hotel/index.tsx

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
import React from 'react';
1+
import React, { useMemo } from 'react';
22
import type { IRouteComponentProps } from 'umi';
33

4-
import useHotelService, { HotelContext } from './useHotelService';
4+
import useHotelService, { HotelDispatchContext, HotelStateContext } from './useHotelService';
55

66
export default function Hotel(props: IRouteComponentProps) {
7-
const hotelService = useHotelService();
7+
const {setValue, setName, value, name} = useHotelService();
88

9-
return <HotelContext.Provider value={hotelService}>{props.children}</HotelContext.Provider>;
9+
const dispatchValues = useMemo(() => ({setValue, setName}), [setValue, setName]);
10+
11+
const stateValues = useMemo(() => ({ value, name }), [value, name]);
12+
13+
return <HotelDispatchContext.Provider value={dispatchValues}>
14+
<HotelStateContext.Provider value={stateValues}>
15+
{props.children}
16+
</HotelStateContext.Provider>
17+
</HotelDispatchContext.Provider>;
1018
}

src/pages/hotel/useHotelService.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,27 @@
1+
import { useCallback, useState } from 'react';
12
import getContextService from '@/utils/getContextService';
2-
import { useState } from 'react';
33

44
export default function useHotelService() {
55
const [value, setValue] = useState(0);
6+
const [name, setName] = useState('');
7+
8+
const changeValue = useCallback((val: number) => {
9+
setValue(val);
10+
}, []);
11+
12+
const changeName = useCallback((name: string) => {
13+
setName(name);
14+
}, []);
615

716
return {
817
value,
9-
setValue,
18+
name,
19+
setValue: changeValue,
20+
setName: changeName,
1021
};
1122
}
1223

13-
export const HotelContext = getContextService(useHotelService);
24+
type ServiceReturnType = ReturnType<typeof useHotelService>;
25+
26+
export const HotelDispatchContext = getContextService<DispatchContext<ServiceReturnType>>();
27+
export const HotelStateContext = getContextService<StateContext<ServiceReturnType>>();

src/utils/getContextService.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ import { createContext } from 'react';
99
* @returns
1010
*/
1111
export default function getContextService<T>(
12-
_: (...args: any) => T,
1312
initialData: T | undefined = undefined,
1413
) {
15-
return createContext(initialData as T);
14+
return createContext<T>(initialData as T);
1615
}

0 commit comments

Comments
 (0)