2016-08-24 67 views
3

权利知道我在哪里导入不同的文件,用行动创造者的不同视图索引文件:可以导入所有容器组件中的所有动作创建器吗?

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 

然后我每次导入动作指数与所有的操作:

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) 

其好的,如果我分开这在不同的出口,并只导入我的容器需要的行动创造者?

此外,很有可能当我有很多动作创作者时,很难找到没有拍摄的名字。

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

+0

看起来我的回答对很多人都有帮助。你能赞成并批准吗? – semanser

回答

5

我认为更好的解决方案是分别导出每个动作模块。 我使用的下一个架构在我的项目:

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

// just import actions from another 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 } 

及后,在我的容器我只导入我从actions/index.js需要:

import { modals, 
     signIn } from '../actions' 

通过这种方法,您将完全控制您需要为容器执行的操作。

2

只导入您需要的操作是一种常见的操作方法。我不明白为什么你需要把所有的动作捆绑在一个对象中。

您也可以使用命名空间的导入是这样的:在这种情况下

import * as generalActions from './generalActions' 

,你不需要export default操作对象包含所有generalActions你可以export每个动作。

通常,仅导入实际使用的内容是一种很好的做法。

+2

“通常,仅导入实际使用的内容是一种很好的做法。” 这是为什么?这正是被问到的问题,所以详细阐述这些良好做法将会有所帮助:-) –

相关问题