Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It's ok to import all action creators in all container components?

Right know I have a index file where 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 I import the actions index every time 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)

Its ok if I separate this in different exports and import only the action creators that my container needs?

Also with this it's very probable that when I have a lot of action creators it will be difficult to find names that are not taken.

What do you think it's the best aproach?

like image 724
Jonathan Calb Avatar asked Aug 24 '16 19:08

Jonathan Calb


2 Answers

I think the better solution is to export every action module separately. I use next architecture in my projects:

The index file for all actions 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 }

After that, in my Container I import only what I need from actions/index.js:

import { notification,
         profile } from '../actions'

With this approach, you will get full control of what actions do you need for your Containers.

like image 163
semanser Avatar answered Oct 24 '22 06:10

semanser


Importing only actions that you need is a common way to do that. I don't understand why do you need to bundle all actions in one object.

You can also use namespaced import like this:

import * as generalActions from './generalActions'

in this case, you don't need to export default action object containing all generalActions you can just export each action.

Generally, it's a good practice to import only what you actually use.

like image 21
Dmitriy Nevzorov Avatar answered Oct 24 '22 06:10

Dmitriy Nevzorov