Import/export when using reducers in Redux-React Native












1















I'm studying Redux with React Native and I have an example like this:





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



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.



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.










share|improve this question





























    1















    I'm studying Redux with React Native and I have an example like this:





    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



    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.



    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.










    share|improve this question



























      1












      1








      1








      I'm studying Redux with React Native and I have an example like this:





      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



      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.



      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.










      share|improve this question
















      I'm studying Redux with React Native and I have an example like this:





      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



      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.



      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.







      javascript reactjs react-native redux






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 13 '18 at 3:20









      Nguyễn Thanh Tú

      4,6393827




      4,6393827










      asked Nov 13 '18 at 3:09









      Nguyễn Văn ĐạiNguyễn Văn Đại

      236




      236
























          1 Answer
          1






          active

          oldest

          votes


















          2














          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






          share|improve this answer


























          • But when i try to use it, i get an error like: TypeError: (0, _reducer.makeRouteReducer) is not a function. (In '(0, _reducer.makeRouteReducer)()', '(0, _reducer.makeRouteReducer)' is undefi ned)

            – Nguyễn Văn Đại
            Nov 13 '18 at 4:05













          • What is the error?

            – SakoBu
            Nov 13 '18 at 4:05











          • (0, _reducer.makeRouteReducer) is not a function

            – Nguyễn Văn Đại
            Nov 13 '18 at 4:06






          • 1





            Oh there is a typo in your code... makeRootReducer and makeRouteReducer... @NguyễnVănĐại

            – SakoBu
            Nov 13 '18 at 4:15













          • This is my app repo after i changed it by following your recommendation, but it still don't work: github.com/daihalu/Booking-Car.git

            – Nguyễn Văn Đại
            Nov 13 '18 at 4:32











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273213%2fimport-export-when-using-reducers-in-redux-react-native%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          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






          share|improve this answer


























          • But when i try to use it, i get an error like: TypeError: (0, _reducer.makeRouteReducer) is not a function. (In '(0, _reducer.makeRouteReducer)()', '(0, _reducer.makeRouteReducer)' is undefi ned)

            – Nguyễn Văn Đại
            Nov 13 '18 at 4:05













          • What is the error?

            – SakoBu
            Nov 13 '18 at 4:05











          • (0, _reducer.makeRouteReducer) is not a function

            – Nguyễn Văn Đại
            Nov 13 '18 at 4:06






          • 1





            Oh there is a typo in your code... makeRootReducer and makeRouteReducer... @NguyễnVănĐại

            – SakoBu
            Nov 13 '18 at 4:15













          • This is my app repo after i changed it by following your recommendation, but it still don't work: github.com/daihalu/Booking-Car.git

            – Nguyễn Văn Đại
            Nov 13 '18 at 4:32
















          2














          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






          share|improve this answer


























          • But when i try to use it, i get an error like: TypeError: (0, _reducer.makeRouteReducer) is not a function. (In '(0, _reducer.makeRouteReducer)()', '(0, _reducer.makeRouteReducer)' is undefi ned)

            – Nguyễn Văn Đại
            Nov 13 '18 at 4:05













          • What is the error?

            – SakoBu
            Nov 13 '18 at 4:05











          • (0, _reducer.makeRouteReducer) is not a function

            – Nguyễn Văn Đại
            Nov 13 '18 at 4:06






          • 1





            Oh there is a typo in your code... makeRootReducer and makeRouteReducer... @NguyễnVănĐại

            – SakoBu
            Nov 13 '18 at 4:15













          • This is my app repo after i changed it by following your recommendation, but it still don't work: github.com/daihalu/Booking-Car.git

            – Nguyễn Văn Đại
            Nov 13 '18 at 4:32














          2












          2








          2







          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






          share|improve this answer















          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







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 13 '18 at 4:22

























          answered Nov 13 '18 at 3:16









          SakoBuSakoBu

          1,109317




          1,109317













          • But when i try to use it, i get an error like: TypeError: (0, _reducer.makeRouteReducer) is not a function. (In '(0, _reducer.makeRouteReducer)()', '(0, _reducer.makeRouteReducer)' is undefi ned)

            – Nguyễn Văn Đại
            Nov 13 '18 at 4:05













          • What is the error?

            – SakoBu
            Nov 13 '18 at 4:05











          • (0, _reducer.makeRouteReducer) is not a function

            – Nguyễn Văn Đại
            Nov 13 '18 at 4:06






          • 1





            Oh there is a typo in your code... makeRootReducer and makeRouteReducer... @NguyễnVănĐại

            – SakoBu
            Nov 13 '18 at 4:15













          • This is my app repo after i changed it by following your recommendation, but it still don't work: github.com/daihalu/Booking-Car.git

            – Nguyễn Văn Đại
            Nov 13 '18 at 4:32



















          • But when i try to use it, i get an error like: TypeError: (0, _reducer.makeRouteReducer) is not a function. (In '(0, _reducer.makeRouteReducer)()', '(0, _reducer.makeRouteReducer)' is undefi ned)

            – Nguyễn Văn Đại
            Nov 13 '18 at 4:05













          • What is the error?

            – SakoBu
            Nov 13 '18 at 4:05











          • (0, _reducer.makeRouteReducer) is not a function

            – Nguyễn Văn Đại
            Nov 13 '18 at 4:06






          • 1





            Oh there is a typo in your code... makeRootReducer and makeRouteReducer... @NguyễnVănĐại

            – SakoBu
            Nov 13 '18 at 4:15













          • This is my app repo after i changed it by following your recommendation, but it still don't work: github.com/daihalu/Booking-Car.git

            – Nguyễn Văn Đại
            Nov 13 '18 at 4:32

















          But when i try to use it, i get an error like: TypeError: (0, _reducer.makeRouteReducer) is not a function. (In '(0, _reducer.makeRouteReducer)()', '(0, _reducer.makeRouteReducer)' is undefi ned)

          – Nguyễn Văn Đại
          Nov 13 '18 at 4:05







          But when i try to use it, i get an error like: TypeError: (0, _reducer.makeRouteReducer) is not a function. (In '(0, _reducer.makeRouteReducer)()', '(0, _reducer.makeRouteReducer)' is undefi ned)

          – Nguyễn Văn Đại
          Nov 13 '18 at 4:05















          What is the error?

          – SakoBu
          Nov 13 '18 at 4:05





          What is the error?

          – SakoBu
          Nov 13 '18 at 4:05













          (0, _reducer.makeRouteReducer) is not a function

          – Nguyễn Văn Đại
          Nov 13 '18 at 4:06





          (0, _reducer.makeRouteReducer) is not a function

          – Nguyễn Văn Đại
          Nov 13 '18 at 4:06




          1




          1





          Oh there is a typo in your code... makeRootReducer and makeRouteReducer... @NguyễnVănĐại

          – SakoBu
          Nov 13 '18 at 4:15







          Oh there is a typo in your code... makeRootReducer and makeRouteReducer... @NguyễnVănĐại

          – SakoBu
          Nov 13 '18 at 4:15















          This is my app repo after i changed it by following your recommendation, but it still don't work: github.com/daihalu/Booking-Car.git

          – Nguyễn Văn Đại
          Nov 13 '18 at 4:32





          This is my app repo after i changed it by following your recommendation, but it still don't work: github.com/daihalu/Booking-Car.git

          – Nguyễn Văn Đại
          Nov 13 '18 at 4:32


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53273213%2fimport-export-when-using-reducers-in-redux-react-native%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Florida Star v. B. J. F.

          Error while running script in elastic search , gateway timeout

          Adding quotations to stringified JSON object values