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;
$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
)
);
;
javascript react.js axios
$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.
add a comment |
$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
)
);
;
javascript react.js axios
$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.
add a comment |
$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
)
);
;
javascript react.js axios
$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
javascript react.js axios
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.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
$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.
$endgroup$
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
$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.
$endgroup$
add a comment |
$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.
$endgroup$
add a comment |
$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.
$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.
answered Jul 15 '18 at 14:49
Sandeep kumar H RSandeep kumar H R
244
244
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
var $window = $(window),
onScroll = function(e)
var $elem = $('.new-login-left'),
docViewTop = $window.scrollTop(),
docViewBottom = docViewTop + $window.height(),
elemTop = $elem.offset().top,
elemBottom = elemTop + $elem.height();
if ((docViewTop elemBottom))
StackExchange.using('gps', function() StackExchange.gps.track('embedded_signup_form.view', location: 'question_page' ); );
$window.unbind('scroll', onScroll);
;
$window.on('scroll', onScroll);
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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