Is it possible to import all action creators in all container components?

I know correctly that I have an index file in which I import different files with action creators for different views:

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

And then every time I import an action index with all the 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)

Well, if I separate it in different exports and import only the creators of the actions that my container needs?

In addition, it is very likely that when I have many action creators, it will be difficult to find names that have not been accepted.

What do you think is the best aproach?

+4
source share
2 answers

, . :

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'

, .

+6

- . , .

:

import * as generalActions from './generalActions'

export default action, generalActions, export .

, , .

+2

Source: https://habr.com/ru/post/1652381/


All Articles