可以导入所有容器组件中的所有动作创建器吗?

Jon*_*alb 7 javascript ecmascript-6 reactjs redux

我知道我有一个索引文件,我用不同的视图导入不同的文件和动作创建者:

import generalActions from './generalActions'
import vftActions from './vftActions'
import shareActions from './shareActions'
import codeFormActions from './codeFormActions'
import signupActions from './signupActions'

const actions = {
    ...generalActions,
    ...vftActions,
    ...shareActions,
    ...codeFormActions,
    ...signupActions
}

export default actions
Run Code Online (Sandbox Code Playgroud)

然后我每次都使用所有操作导入动作索引:

import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import actions from '../../redux/actions'

function mapDispatchToProps(dispatch) {
    return {
        actions: bindActionCreators(actions, dispatch)
    }
}

export default connect(null, mapDispatchToProps)(ContainerComponent)
Run Code Online (Sandbox Code Playgroud)

如果我在不同的导出中分离它并仅导入我的容器需要的动作创建器,那好吗?

此外,很有可能当我有很多动作创建者时,很难找到没有采取的名字.

您认为这是最好的方法吗?

sem*_*ser 10

我认为更好的解决方案是单独导出每个操作模块.我在我的项目中使用下一个架构:

所有操作的索引文件actions/index.js:

// just import actions from other files (modules)
import * as notification from './notification'
import * as friend from './friend'
import * as profile from './profile'

// export all actions as modules
export { notification }
export { friend }
export { profile }
Run Code Online (Sandbox Code Playgroud)

在那之后,在我的容器中我只导入我需要的东西actions/index.js:

import { notification,
         profile } from '../actions'
Run Code Online (Sandbox Code Playgroud)

通过这种方法,您可以完全控制容器所需的操作.