Dear campers,
I am relatively new to Redux. I am using Redux-Thunk
middleware to dispatch actions. One of my action creators makes a call to an external API and i am using the response data to dispatch another API call from a different action creator. Below, i am using fetchQuizTotal
to dispatch a call to an external API from fetchTriviaCategories
because it needs the data returned from fetchTriviaCategories
API call to make its own API call. It works but i am a bit hesitant because i am not sure if this is a good practice. Is there a better way of achieving the same result?
const fetchQuizTotal = (categoryId) => {
return (dispatch) => {
return fetch(baseUrl + categoryId)
.then((response) => response.json())
.then((quizCount) => {
dispatch(quizCount);
return quizCount;
});
};
};
const fetchTriviaCategories = () => {
return (dispatch) => {
return fetch(triviaCategoriesUrl)
.then((response) => response.json())
.then((triviaCategories) => {
const { trivia_categories } = triviaCategories;
dispatch(fetchQuizTotal(+trivia_categories[0].id));
return trivia_categories;
});
};
};