I am struggling with finding the way of how to best dispatch multiple actions with redux thunk. Firstly, i want to keep all my thunks isolated and not mix them, because it of course will let me use them anywhere (in one view i could dispatch fetchData and then show a message saying "the data was successfully fetched" and in another view after fetching the Data just fetching something else or In some cases i want to execute code just if my thunk was successfully dispatched). For example lets say i have the next thunk:
export const fetchData = () => async (dispatch, getState) => {
dispatch(fetchDataStart())
try {
const response = await fetch('https://jsonplaceholder.typicode.com/photos')
const data = await response.json()
dispatch(fetchDataSuccess(data))
// should i dispatch actions here and create more thunks like this one for differents
// situations
} catch (error) {
dispatch(fetchDataFailed(error))
throw error // is this throw error correct to stop the flow of the code
}
}
and in my component I dispatch that action to fetch the data I need:
useEffect(() => {
// should i make this function async and await for the first dispatch and then dispatch
// another function
const loadUsers = () => {
dispatch(fetchData())
// await dispatch(fetchData())
// dispatch(nextAction())
}
loadUsers()
},[ ])
What is the correct approach to dispatch another action depending on the first fetch?
Should i throw an error in the thunks to avoid other actions to be dispatched if the fetch is not successful?
Should I dispatch the next action in the component or just create more and more thunks for every different situation depending on what i want to do after one action? (this could make my actions files bigger )
Thank you in advance for your help