简体   繁体   English

在Redux-React Native中使用reducer时导入/导出

[英]Import/export when using reducers in Redux-React Native

I'm studying Redux with React Native and I have an example like this: 我正在使用React Native研究Redux,我有一个这样的例子:


reducer.js reducer.js

import {combineReducers} from "redux"; 
import {HomeReducer as home} from "../routes/Home/modules/home";

const makeRootReducer = () => {
    return combineReducers({
        home
    });
};

export default makeRootReducer;

createStore.js createStore.js

import {createStore, applyMiddleware, compose} from 'redux';
import thunk from "redux-thunk";
import {createLogger} from 'redux-logger';

import makeRouteReducer from './reducer';

const log = createLogger({diff: true, collapsed: true});

export default (initialState = {}) => {
    const middleware = [thunk, log];
    const enhancers = [];

    return store = createStore(
        makeRouteReducer(),
        initialState,
        compose(
            applyMiddleware(...middleware),
            ...enhancers
        )
    );
}

It works fine but I don't understand why I have to use export default in reducer.js . 它工作正常,但我不明白为什么我必须在reducer.js中使用export default

When I try to use 当我尝试使用

// reducer.js
export makeRootReducer

and

// createStore.js
import {makeRouteReducer} from './reducer';

It didn't work. 没用

Please help me by explaining it in detail. 请通过详细解释为我提供帮助。

Thank you very much. 非常感谢你。

You're talking about named exports and default exports ... 您正在谈论命名出口默认出口 ...

Example of a named export: 命名导出示例:

export const someFunction = () => {
   // some code here...
}

Now you can import this in another file like this: 现在,您可以将其导入另一个文件中,如下所示:

import {someFumnction} from './nameOfFile'

But if you do: 但是,如果您这样做:

export default function someFunction () {
   // some code here...
}

That's a default export and you have to import it like this: 这是默认导出,您必须像这样导入它:

import someFunction from './nameOfFile'

In your example if you change this: 在您的示例中,如果您更改此设置:

const makeRootReducer = () => {
    return combineReducers({
        home
    });
};

export default makeRootReducer;

To this: 对此:

export const makeRootReducer = () => {
    return combineReducers({
        home
    });
};

It will become a named export and not a default export and now you can do: 它将成为命名的导出,而不是默认的导出,现在您可以执行以下操作:

import {makeRootReducer} from....

Hope that clarifies... And here is more info 希望能澄清...这是更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM