I have a lot of action creators that I am trying to organize into a few different files. Each separate file has exported functions like:
export function addData(data) {
return {
type: 'ADD_DATA',
data
}
}
Before they were separated into distinct files, they were imported like:
import { addData } from './actions/actionCreators'
However with the structure now like
├─┬ actions
│ ├── actionsCreators1.js
│ ├── actionsCreators2.js
│ └── index.js
I would like to consolidate them in the index file and export them as they were initially named. What I have tried is:
actions/index.js
import * as actions1 from './actionCreators1'
import * as actions2 from './actionCreators2'
export default {
...actions1,
...actions2
}
However the named functions are undefined when imported. I can bring them in individually import { action1, action2 } from './actionCreatos1'
and it will work, however it's not ideal to have to write everything in two places.
Rather than relying on spread, you can use the module system itself to pass the names through, e.g.
so as long as the names in the files don't collide, they will just be passed through nicely.
In searching for a consistent export / import pattern, this is one possible solution.
using this for all imports:
import alpha from './alpha'
(no star)For the exports:
This groups everything with a
export
before it, as well as everything exported fromalpha
.I believe the problem is in your actions/index.js
Since you're exporting as
default
, you would have to import like this:This will not work:
The destructuring syntax can only grab named exports, but you're using a default export.
I'd like to be able to create named exports with object spread, but alas, it is not valid syntax: