Fetching data from API in JavaScript The 2019 Stack Overflow Developer Survey Results Are InFetching ALL data set from API iterating via response dataFetching of JSON data through an API with AngularJSGet data from external APIComponent that reacts on data changed from the storeFetching weather data from APIReturning data from JavaScript objectFetching and parsing JSON data from the Oodle APISimple React component to view cached data and test result from APIUsing a public API to display data

Falsification in Math vs Science

Output the Arecibo Message

If a sorcerer casts the Banishment spell on a PC while in Avernus, does the PC return to their home plane?

Did any laptop computers have a built-in 5 1/4 inch floppy drive?

Is it correct to say the Neural Networks are an alternative way of performing Maximum Likelihood Estimation? if not, why?

What do I do when my TA workload is more than expected?

Why does the nucleus not repel itself?

Will it cause any balance problems to have PCs level up and gain the benefits of a long rest mid-fight?

Accepted by European university, rejected by all American ones I applied to? Possible reasons?

Does HR tell a hiring manager about salary negotiations?

Pokemon Turn Based battle (Python)

Are turbopumps lubricated?

Did Scotland spend $250,000 for the slogan "Welcome to Scotland"?

Cooking pasta in a water boiler

Old scifi movie from the 50s or 60s with men in solid red uniforms who interrogate a spy from the past

Why isn't the circumferential light around the M87 black hole's event horizon symmetric?

writing variables above the numbers in tikz picture

Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?

What's the name of these plastic connectors

Worn-tile Scrabble

Can withdrawing asylum be illegal?

Loose spokes after only a few rides

How to type a long/em dash `—`

Does adding complexity mean a more secure cipher?



Fetching data from API in JavaScript



The 2019 Stack Overflow Developer Survey Results Are InFetching ALL data set from API iterating via response dataFetching of JSON data through an API with AngularJSGet data from external APIComponent that reacts on data changed from the storeFetching weather data from APIReturning data from JavaScript objectFetching and parsing JSON data from the Oodle APISimple React component to view cached data and test result from APIUsing a public API to display data



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4












$begingroup$


I'm using Axios to fetch data from an API, and don't know if this is the best way to deal with APIs in ReactJS or not. I want to know if anyone can suggest edits or something to make the code more efficient.



api.js



import axios from 'axios';

export const APIendpoint = axios.create(
baseURL: 'https://1eb19101-0790-406f-90e0-ee35a2ff2d1f.mock.pstmn.io',
timeout: 2000
);


matchesAction.js



import FETCH_MATCHES from './actionTypes';
import APIendpoint from '../api/api';

export const fetchMatches = () => dispatch =>
const matchInstance = APIendpoint.get('/matches');
matchInstance.then(response =>
dispatch(
type: FETCH_MATCHES,
payload: response.data
)
);
;









share|improve this question











$endgroup$




bumped to the homepage by Community 3 hours ago


This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.





















    4












    $begingroup$


    I'm using Axios to fetch data from an API, and don't know if this is the best way to deal with APIs in ReactJS or not. I want to know if anyone can suggest edits or something to make the code more efficient.



    api.js



    import axios from 'axios';

    export const APIendpoint = axios.create(
    baseURL: 'https://1eb19101-0790-406f-90e0-ee35a2ff2d1f.mock.pstmn.io',
    timeout: 2000
    );


    matchesAction.js



    import FETCH_MATCHES from './actionTypes';
    import APIendpoint from '../api/api';

    export const fetchMatches = () => dispatch =>
    const matchInstance = APIendpoint.get('/matches');
    matchInstance.then(response =>
    dispatch(
    type: FETCH_MATCHES,
    payload: response.data
    )
    );
    ;









    share|improve this question











    $endgroup$




    bumped to the homepage by Community 3 hours ago


    This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.

















      4












      4








      4





      $begingroup$


      I'm using Axios to fetch data from an API, and don't know if this is the best way to deal with APIs in ReactJS or not. I want to know if anyone can suggest edits or something to make the code more efficient.



      api.js



      import axios from 'axios';

      export const APIendpoint = axios.create(
      baseURL: 'https://1eb19101-0790-406f-90e0-ee35a2ff2d1f.mock.pstmn.io',
      timeout: 2000
      );


      matchesAction.js



      import FETCH_MATCHES from './actionTypes';
      import APIendpoint from '../api/api';

      export const fetchMatches = () => dispatch =>
      const matchInstance = APIendpoint.get('/matches');
      matchInstance.then(response =>
      dispatch(
      type: FETCH_MATCHES,
      payload: response.data
      )
      );
      ;









      share|improve this question











      $endgroup$




      I'm using Axios to fetch data from an API, and don't know if this is the best way to deal with APIs in ReactJS or not. I want to know if anyone can suggest edits or something to make the code more efficient.



      api.js



      import axios from 'axios';

      export const APIendpoint = axios.create(
      baseURL: 'https://1eb19101-0790-406f-90e0-ee35a2ff2d1f.mock.pstmn.io',
      timeout: 2000
      );


      matchesAction.js



      import FETCH_MATCHES from './actionTypes';
      import APIendpoint from '../api/api';

      export const fetchMatches = () => dispatch =>
      const matchInstance = APIendpoint.get('/matches');
      matchInstance.then(response =>
      dispatch(
      type: FETCH_MATCHES,
      payload: response.data
      )
      );
      ;






      javascript react.js axios






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jul 11 '18 at 19:28







      gretty volk

















      asked Jul 11 '18 at 19:20









      gretty volkgretty volk

      212




      212





      bumped to the homepage by Community 3 hours ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.







      bumped to the homepage by Community 3 hours ago


      This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.






















          1 Answer
          1






          active

          oldest

          votes


















          0












          $begingroup$

          It can be done in many ways based on your use case & code structure, but i feel this can be optimal way of structuring.



          api/services/index.js



          import axios from 'axios';
          export default as MATCHES_API from "./services/matches.js";

          export const CONFIG_API = axios.create(
          baseURL: 'https://1eb19101-0790-406f-90e0-ee35a2ff2d1f.mock.pstmn.io',
          timeout: 2000
          );


          api/services/matches.js



          import CONFIG_API from "api/services";

          const ERROR_MSG = "<generic message>";

          export const matches = () =>
          return CONFIG_API.get("/matches").then(res =>
          // do some operations on data - if required
          return success: true, data: res.payload;
          ).catch(err =>
          return ;
          );
          ;


          actions/matchesAction.js



          import FETCH_MATCHES from './actionTypes';
          import MATCHES_API from "/api/services";

          export const fetchMatches = () => dispatch =>
          matches.then((response) =>
          const success = response;
          if (success)
          dispatch( type: FETCH_MATCHES, payload);
          else
          dispatch( type: FETCH_MATCHES_FAILED); /* or show notification */ .

          );
          ;


          Let me know if this works out.






          share|improve this answer









          $endgroup$













            Your Answer





            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            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: "196"
            ;
            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: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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%2fcodereview.stackexchange.com%2fquestions%2f198311%2ffetching-data-from-api-in-javascript%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









            0












            $begingroup$

            It can be done in many ways based on your use case & code structure, but i feel this can be optimal way of structuring.



            api/services/index.js



            import axios from 'axios';
            export default as MATCHES_API from "./services/matches.js";

            export const CONFIG_API = axios.create(
            baseURL: 'https://1eb19101-0790-406f-90e0-ee35a2ff2d1f.mock.pstmn.io',
            timeout: 2000
            );


            api/services/matches.js



            import CONFIG_API from "api/services";

            const ERROR_MSG = "<generic message>";

            export const matches = () =>
            return CONFIG_API.get("/matches").then(res =>
            // do some operations on data - if required
            return success: true, data: res.payload;
            ).catch(err =>
            return ;
            );
            ;


            actions/matchesAction.js



            import FETCH_MATCHES from './actionTypes';
            import MATCHES_API from "/api/services";

            export const fetchMatches = () => dispatch =>
            matches.then((response) =>
            const success = response;
            if (success)
            dispatch( type: FETCH_MATCHES, payload);
            else
            dispatch( type: FETCH_MATCHES_FAILED); /* or show notification */ .

            );
            ;


            Let me know if this works out.






            share|improve this answer









            $endgroup$

















              0












              $begingroup$

              It can be done in many ways based on your use case & code structure, but i feel this can be optimal way of structuring.



              api/services/index.js



              import axios from 'axios';
              export default as MATCHES_API from "./services/matches.js";

              export const CONFIG_API = axios.create(
              baseURL: 'https://1eb19101-0790-406f-90e0-ee35a2ff2d1f.mock.pstmn.io',
              timeout: 2000
              );


              api/services/matches.js



              import CONFIG_API from "api/services";

              const ERROR_MSG = "<generic message>";

              export const matches = () =>
              return CONFIG_API.get("/matches").then(res =>
              // do some operations on data - if required
              return success: true, data: res.payload;
              ).catch(err =>
              return ;
              );
              ;


              actions/matchesAction.js



              import FETCH_MATCHES from './actionTypes';
              import MATCHES_API from "/api/services";

              export const fetchMatches = () => dispatch =>
              matches.then((response) =>
              const success = response;
              if (success)
              dispatch( type: FETCH_MATCHES, payload);
              else
              dispatch( type: FETCH_MATCHES_FAILED); /* or show notification */ .

              );
              ;


              Let me know if this works out.






              share|improve this answer









              $endgroup$















                0












                0








                0





                $begingroup$

                It can be done in many ways based on your use case & code structure, but i feel this can be optimal way of structuring.



                api/services/index.js



                import axios from 'axios';
                export default as MATCHES_API from "./services/matches.js";

                export const CONFIG_API = axios.create(
                baseURL: 'https://1eb19101-0790-406f-90e0-ee35a2ff2d1f.mock.pstmn.io',
                timeout: 2000
                );


                api/services/matches.js



                import CONFIG_API from "api/services";

                const ERROR_MSG = "<generic message>";

                export const matches = () =>
                return CONFIG_API.get("/matches").then(res =>
                // do some operations on data - if required
                return success: true, data: res.payload;
                ).catch(err =>
                return ;
                );
                ;


                actions/matchesAction.js



                import FETCH_MATCHES from './actionTypes';
                import MATCHES_API from "/api/services";

                export const fetchMatches = () => dispatch =>
                matches.then((response) =>
                const success = response;
                if (success)
                dispatch( type: FETCH_MATCHES, payload);
                else
                dispatch( type: FETCH_MATCHES_FAILED); /* or show notification */ .

                );
                ;


                Let me know if this works out.






                share|improve this answer









                $endgroup$



                It can be done in many ways based on your use case & code structure, but i feel this can be optimal way of structuring.



                api/services/index.js



                import axios from 'axios';
                export default as MATCHES_API from "./services/matches.js";

                export const CONFIG_API = axios.create(
                baseURL: 'https://1eb19101-0790-406f-90e0-ee35a2ff2d1f.mock.pstmn.io',
                timeout: 2000
                );


                api/services/matches.js



                import CONFIG_API from "api/services";

                const ERROR_MSG = "<generic message>";

                export const matches = () =>
                return CONFIG_API.get("/matches").then(res =>
                // do some operations on data - if required
                return success: true, data: res.payload;
                ).catch(err =>
                return ;
                );
                ;


                actions/matchesAction.js



                import FETCH_MATCHES from './actionTypes';
                import MATCHES_API from "/api/services";

                export const fetchMatches = () => dispatch =>
                matches.then((response) =>
                const success = response;
                if (success)
                dispatch( type: FETCH_MATCHES, payload);
                else
                dispatch( type: FETCH_MATCHES_FAILED); /* or show notification */ .

                );
                ;


                Let me know if this works out.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Jul 15 '18 at 14:49









                Sandeep kumar H RSandeep kumar H R

                244




                244



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Code Review Stack Exchange!


                    • 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.

                    Use MathJax to format equations. MathJax reference.


                    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%2fcodereview.stackexchange.com%2fquestions%2f198311%2ffetching-data-from-api-in-javascript%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

                    名間水力發電廠 目录 沿革 設施 鄰近設施 註釋 外部連結 导航菜单23°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.7113923°50′10″N 120°42′41″E / 23.83611°N 120.71139°E / 23.83611; 120.71139計畫概要原始内容臺灣第一座BOT 模式開發的水力發電廠-名間水力電廠名間水力發電廠 水利署首件BOT案原始内容《小檔案》名間電廠 首座BOT水力發電廠原始内容名間電廠BOT - 經濟部水利署中區水資源局

                    格濟夫卡 參考資料 导航菜单51°3′40″N 34°2′21″E / 51.06111°N 34.03917°E / 51.06111; 34.03917ГезівкаПогода в селі 编辑或修订