在react中使用redux项目架构及编码风格推荐

在react中使用redux推荐的部分项目架构组织

|-- src
    |-- action
    |   |-- index.js
    |   |-- types.js
    |   |-- theme
    |       |-- index.js
    |-- reducer
    |   |-- index.js
    |   |-- theme
    |       |-- index.js
    |-- store
        |-- index.js

action 文件夹下放action相关文件,其中types.js 定义了actiontype 常量。

types.js

export default {
    THEME_CHANGE: 'THEME_CHANGE',
    THEME_INIT: 'THEME_INIT',
};

theme 文件夹下定义了index.jstheme 相关的action创建函数
其中 函数名推荐使用on + 对象 + 动作类型 来命名。

index.js

import Types from '../types';

export function onThemeChange(theme) {
    return {type: Types.THEME_CHANGE, theme: theme};

}

action目录下的index.js中导出不同的action创建函数。

action/index.js

import {onThemeChange} from './theme';

export default {
    onThemeChange,
};

reducer 的目录构建参考action.

值得注意的是我们在引入index.js文件时直接importindex.js所在的文件夹,根据nodejs文件查找策略如果导入的是一个文件夹,会寻找该文件夹下的index.js文件。

总结来说,在实际的项目中,我们需要做到以下

  • actiontype定义常量
  • action通过action创建函数来生成(自定义或者借助第三方库)
  • reducer根据功能或者类别进行拆分,最终使用combineReducer 组成根reducer.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值