August 2017
Beginner
374 pages
10h 41m
English
You may have noticed that we are writing a lot of boilerplate code to dispatch the three different actions. We can write a function to be able to reuse this behavior. We will pass an action object, which looks as follows, to this function:
{ types: [ FETCH_POSTS_REQUEST, FETCH_POSTS_SUCCESS, FETCH_POSTS_FAILURE ], promise: fetch('http://localhost:8080/api/posts') .then(response => response.json())}
We are now going to write a generic function that contains all the boilerplate code:
export const thunkCreator = (action) => { const { types, promise, ...rest } = action const [ REQUESTED, RESOLVED, REJECTED ] = types
Read now
Unlock full access