August 2017
Beginner
298 pages
7h 4m
English
Usually, our React component will have to trigger only a single action--getAllPosts()--which will make the network request and return the post data. This action will be our dispatcher. This action will start the network request and dispatch all the other actions based on the result of the network request. In your postActions.js file, add the following code:
export const getAllPosts = () => { return dispatch => { // Create the dispatcher dispatch(postsApiCallStart()); // Dispatch - api call started apiCall('posts', {}, 'GET') .then(posts => { dispatch(postsApiCallSuccess()); // Dispatch - api call success dispatch(getPosts(posts)); // Dispatch - received posts array }) .catch(error => { dispatch(postsApiCallFailure()); // Dispatch ...Read now
Unlock full access