Question 80
Question
Answer
// Action Creator: Dispatches a thunk that fetches data
export const fetchData = () => {
return (dispatch) => {
fetch('https://api.example.com/data')
.then(response => response.json())
.then(data => {
dispatch({ type: 'FETCH_DATA_SUCCESS', payload: data });
})
.catch(error => {
dispatch({ type: 'FETCH_DATA_FAILURE', payload: error });
});
};
};
Last updated