https://github.com/rt2zz/redux-persist shows the following code snippet
const persistedReducer = persistReducer(persistConfig, rootReducer)
export default () => {
let store = createStore(persistedReducer)
let persistor = persistStore(store)
return { store, persistor }
}
You could instead do this, and what is the difference between the two?
const persistedReducer = persistReducer(persistConfig, rootReducer)
let store = createStore(persistedReducer)
let persistor = persistStore(store)
export { store, persistor }
more importantly, why would one use the first form instead of the 2nd?
The first snippet is exporting a function
which returns a object
. The second snippet returns a object
directly.
When import first snippet, You need to call the function to get the object. Meanwhile You can directly import object from second snippet.
vi a.mjs
then write:
export default () => {
let store = 'createStore';
let persistor = 'persistStore';
return { store, persistor }
}
let store = 'createStore';
let persistor = 'persistStore';
export { store, persistor }
vi b.mjs
then write:
import a from './a.mjs';
import * as $a from './a.mjs';
import { store, persistor } from './a.mjs';
console.log('export default function:', a);
console.log('export object:', $a);
console.log('export object.default:', $a.default);
console.log('export { store, persistor }:', store, persistor);
node --experimental-modules b.mjs
And the you will be get the result like this:
export default function: [Function: default]
export object: [Module] {
default: [Function: default],
persistor: 'persistStore',
store: 'createStore'
}
export object.default: [Function: default]
export { store, persistor }: createStore persistStore
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.