How to make multiple API calls within React Native and Reflux

Web Technologies Web Development 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ I am following Indrik Lasn's React/Reflux/Thunk tutorial in React Native Training and having a hard time editing the code to allow for chained API calls. Tutorial Link: https://medium.com/react-native-training/learn-how-to-build-a-rn-redux-cryptocurrency-app-chapter-iii-a454dda156b How do I chain multiple API calls from coinmarketcap.com's API? This is the code I thought would work. Developer tools shows the data is coming in for the 2 API calls but once the 2nd API loads into the browser, it replaces the data from the 1st API data within the view? import axios from 'axios'; import { apiBaseURL } from './../Utils/Constants'; import { FETCHING_COIN_DATA, FETCHING_COIN_DATA_SUCCESS, FETCHING_COIN_DATA_FAIL } from './../Utils/ActionTypes'; export default function FetchCoinData() { return dispatch => { dispatch({ type: FETCHING_COIN_DATA }) axios.get(`${apiBaseURL}/v1/ticker/bitcoin`) .then(res => dispatch({ type: FETCHING_COIN_DATA_SUCCESS, payload: res.data })) .then(() => axios.get(`${apiBaseURL}/v1/ticker/litecoin`)) .then(res => dispatch({ type: FETCHING_COIN_DATA_SUCCESS, payload: res.data })) .catch(err => { dispatch({ type: FETCHING_COIN_DATA_FAIL, payload: err.data }) }); } }

Posted on 16 Aug 2022, this text provides information on Web Development related to Web Technologies. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ async componentDidMount() { const firstRequest = await axios.get('https://maps.googleapis.com/maps/api/geocode/json?&address=' + this.props.p1); const secondRequest = await axios.get('https://maps.googleapis.com/maps/api/geocode/json?&address=' + this.props.p2); const thirdRequest = await axios.get('https://maps.googleapis.com/maps/api/directions/json?origin=place_id:' + firstRequest.data.results.place_id + '&destination=place_id:' + secondRequest.data.results.place_id + '&key=' + 'API-KEY-HIDDEN'); this.setState({ p1Location: firstRequest.data, p2Location: SecondRequest.data, route: thirdRequest.data, }); }

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Web Technologies Links

tuteehub community

Join Our Community Today

Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.

tuteehub community