2016-08-24 38 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

「通常,僅導入實際使用的內容是一種很好的做法。」 這是爲什麼?這正是被問到的問題,所以詳細闡述這些良好做法將會有所幫助:-) –

相關問題