mominger / blog

Tech blog
45 stars 3 forks source link

Design a web architecture based on React18+Mobx #40

Open mominger opened 2 years ago

mominger commented 2 years ago

Project architecture diagram

架构

Technology stack of project

teck_stack

The upper part is the English version, and the lower part is the Chinese version, with the same content. If there are any wrong, or you have anything hard to understand, pls feel free to let me know.many thx.

Based on React 18, React Route 6.x, Mobx 6.x Project address

1. Key design

1.1 Business decoupling

When R&D developers are working at the same time, and the business is complex, it is necessary to divide the business into modules and decouple it Such as there are A, B, C, etc modules, they will be isolated at key layers such as Store, Api, Page etc.

1.2 Views compatible for Mobile and PC

There are about 3 ways for views to be compatible with mobile and PC.

1.3 Authentication

example_authentication

Set isAuth for each route in every routes.ts. The default isAuth is true, which means authentication is required If authentication is required, It will render login component first unless it has logged in.

pls refer to require-auth component

1.4 I18n

There are 2 ways to process I18n for project as follows

1.5 Directory Structure

The following is an explanation of each layer of the architecture Postman/Swagger in bottom of architecture means after providing API documents in the back-end, use them for debugging

2. Network request and service processing

2.1 Newwork

2.2 code of View Layer

Store.onLoad(action) is automatically called through the page root component Page Or the page calls the api through store.action Refer to pages/home/index.tsx for details

2.3 code example of page store

If the page is wrapped by components/page component, onLoad in Store will be executed automatically when the page is initialized, execute automatically onUnload in Store when the page is unloaded. store_action

Define a methed for calling api page_api

call api in store call_api

All interface type are defined in @types directory Refer to store/home' for more detail

2.4 http.ts code

req_1 req2

Refer to http/base/http for more detail MobX builds store, which is equivalent to the page model layer, and the model layer is responsible for calling the Apis Compared with the Redux (dispatch(action) -> reducer -> new state) process, the store class of MobX looks natural and simple; in addition, Redux needs to rely on middleware, such as the plug-in Redux-thunk to call Api, but MobX does not. Finally, the Redux update component needs to be obtained asynchronously through store.subscribe, and MobX is a synchronous and responsive update. The above is the reason for replacing Redux with MobX.

3. Utils

There are many auxiliary JS classes under the helper package

3.1 Content

3.2 Internationalization

guojihua

3.3 Contants

constants

3.4 Config

Generate configuration items based on env in different environments

For others, please check the code under helper

4. Page Container

All pages are placed under pages, and public components are placed under components

4.1 components

pubic_components If it is not a public component, only used for a certain page, pls placed in the components folder of the corresponding page. such as home/components

4.1.1 Page root component

page_root

Extracting the root component is to reuse some common logic of all pages. For example, it will automatically process the initialization onLoad and unload onUnload of all page stores. For others, please check the code under components

4.2 Router

use React Router 6.x

4.2.1 Register Routes

register_route

4.2.3 Define Route for certain page module

chunk_name Chunk name will display when loading chunk_name

4.3 MobX state management

4.3.1 Define store

home_store

Init every store store_2

All store wrapped in Context store_3

Responsive variables, initialize action onLoad, unload action onUnload, other actions, computed variables

4.3.2 Store used in page

page_store

Call action/variables through store.xxx Example code is in pages/home

5. UI component library

For some common ui libraries such as Bootstrap, Material UI, Andt, etc, are easy to integrate into the project. The following is an example of integrating Antd.

5.1 integrate Antd

6. Compile and package

6.1 Configuration items

config

Can override webpack configuration information Refer to configuration item description

6.2 Debugger by mock environment

6.2.1 Add mock data

add mock data for certain api in mocks directory

6.2.2 Exec yarn mock

6.3.3 Principle analysis

Launch a mock server by koa

Set BASEURL to mock server host in .env.mock, so all request will connect to it

7. Log

7.1 Catching React Exceptions

c_1 c_2

7.2 Log upload

upload_l

At present, after the exception logs are collected in helper/log, they are only printed to the console, and normally should be uploaded to the log platform, such as Sentry. Note: Asynchronous exceptions such as Promises need to be caught by try...catch

8. Unit Test

8.1 add unit test

add unit test file in __tests__/unit directory use assert of jest for common function, such as js-dom-test-spec.js use enzyme or testing-library for react component, such as app-spec.js exec command yarn test Refer to Jest 27.x create-react-app unit test enzyme

8.2 Check out coverage

exec command yarn test:coverage

9. Pre Commit

pre_commit

9.1 Code format

9.2 Validate commit message

validate commit msg by commitlint, Refer to commitlint Message format such as 'feat: AAX-XXX...'

10 Compatible for Mobile and PC

10.1 Load the corresponding global style

css init

10.2 Load the corresponding hooks/components inside the page

home

10.3 Load the corresponding page component inside the router

router_envoriment

If in mobile envorinment, mount the mobile page component , otherwise, mount the pc page component

10.4 Example

mobile mobile

pc pc

11. Other

The following is the Chinese version, the same content as above

架构图

架构

技术栈

teck_stack

基于 React 18, React Route 6.x, Mobx 6.x 源码地址

1. Key design

1.1 Business decoupling

当研发人员同时工作,业务复杂时,需要将业务划分为模块,解耦 比如有A、B、C等模块,会在Store、Api、Page等关键层进行隔离。

1.2 Views compatible for Mobile and PC

有如下 3 种方式可以让View层与Mobile和 PC端兼容。

1.3 Authentication

example_authentication

每个routes.ts里给每个路由设置isAuth.默认isAuth为true,代表需要鉴权 如果需要鉴权,则会判定是否已登录。没登录渲染login组件。已登录则正常渲染。 具体参考require-auth组件

1.4 I18n

有如下2种处理项目I18n的方式

1.5 Directory Structure

下面是架构图各层的解释 架构图底端的 Postman/Swagger 是指后端提供 API 文档后,用它们来调试

2.Network request and service processing

2.1 Flow

Newwork

2.2 code of View Layer

Store.onLoad(action) 通过页面根组件Page自动调用 或页面通过store.action调用api 详见 pages/home/index.tsx

2.3 code example of page store

如果页面被components/page组件包裹,则页面初始化时会自动执行Store中的onLoad,卸载页面时会自动执行Store中的onUnload。 store_action

定义调用api的action page_api

在 store 中调用 api call_api

所有接口类型都定义在@types 里 详情请参阅store/home

2.4 http.ts code

req_1 req2

更多详情请参考http/base/http MobX构建store,相当于model层,model层负责调用Api 相比于 Redux(dispatch(action) -> reducer -> new state)流程,MobX 的 store 类看起来自然简洁; 另外,Redux 需要依赖中间件,比如插件 Redux-thunk 调用 Api,而 MobX 不需要。 最后,需通过 store.subscribe 异步获取 Redux 更新组件,而 MobX 是同步响应式更新。 以上就是用 MobX 代替 Redux 的原因。

3. Utils

helper包下有很多辅助JS类

3.1 Content

3.2 Internationalization

guojihua

3.3 Contants

constants

3.4 Config

基于不同环境下的env生成配置项

更多详情请查看 helper 下的代码

4. Page Container

所有页面组件都放在pages下,公共组件放在components下

4.1 components

pubic_components 如果不是公共组件,只用于某个页面,请放在对应页面的 components 文件夹中。 例如 home/components

4.1.1 Page root component

page_root

提取根组件是为了重用页面的一些通用逻辑。 例如,它会自动处理所有页面store的初始化action: onLoad, onUnload。 其他请查看components的代码

4.2 Router

使用 React Router 6.x

4.2.1 Register Routes

register_route

4.2.3 Define Route for certain page module

chunk_name 加载时显示Chunk 名称 chunk_name

4.3 MobX state management

4.3.1 Define store

home_store

初始化每个store store_2

所有store包裹在Context里 store_3

响应式变量,初始化action: onLoad,卸载action:onUnload,其他action,computed变量

4.3.2 Store used in page

page_store

通过store.xxx调用action/变量 示例代码在 pages/home

5. UI component library

对于一些常用的ui库如BootstrapMaterial UIAndt 等,很容易集成到项目中。 下面是一个集成Antd的例子。

5.1 integrate Antd

6. Compile and package

6.1 Configuration items

config

可以覆盖webpack配置信息 Refer to configuration item description

6.2 Debugger by mock environment

6.2.1 Add mock data

在 mocks 目录中为一些 api 添加mock数据

6.2.2 Exec yarn mock

6.3.3 Principle analysis

通过koa启一个mock服务器

将 BASEURL 设置为 .env.mock 中的模拟服务器host,于是所有请求都会访问它

7. Log

7.1 Catching React Exceptions

c_1 c_2

7.2 Log upload

upload_l

目前在helper/log中收集异常日志后,只打印到控制台,一般应该上传到日志平台,如Sentry。 注意:Promise 等异步异常需要通过 try...catch 来捕获

8. Unit Test

8.1 add unit test

__tests__/unit 目录中添加单元测试文件 jest 的 assert 可用于普通函数,在 js-dom-test-spec.js有示例代码 用 enzyme 或 testing-library 来测试react 组件,在 app-spec.js 有示例代码 执行命令yarn test 参考 Jest 27.x create-react-app unit test enzyme

8.2 Check out coverage

执行命令 yarn test:coverage

9. Pre Commit

pre_commit

9.1 Code format

9.2 Validate commit message

通过 commitlint 验证 commit msg,参考 commitlint msg格式如'feat: XXX...'

10 Compatible for Mobile and PC

10.1 Load the corresponding global style

css init

10.2 Load the corresponding hooks/components inside the page

home

10.3 Load the corresponding page component inside the router

router_envoriment

如果是mobile环境,挂载mobile端页面组件,否则,挂载pc端页面组件

10.4 Example

mobile mobile

pc pc

11. Other

mominger commented 1 year ago

update key design