In this exercise, you will enable an app to talk to a server using redux-thunk
. We will call the Reddit API.
-
Clone the repo reddit-app. Now
npm install
, thennpm start
. -
Add in the Redux devtools store enhancer so you can use the devtools extension.
-
In
src/actions/index.js
, thefetchPosts()
function needs to be defined.-
First, dispatch a
requestPosts()
action, passing it the required 'reddit' (eg 'reactjs' or 'frontend'). -
Now return a promise from this function, as a result of calling
fetch
:return fetch(URL) .then( ) // convert from JSON .then( ) // dispatch an action for the received posts (see below)
- For the URL, https://www.reddit.com/r/reactjs would take you to the 'reactjs' reddit. If you append
.json
, it will retrieve this same page as JSON.
- For the URL, https://www.reddit.com/r/reactjs would take you to the 'reactjs' reddit. If you append
-
When the data arrives, dispatch a further action using the
receivePosts()
action creator.
-
-
Test your work using the browser devtools (
Network
tab,Redux
tab andReact
tab).Which actions are dispatched, and how do they change the state tree?
The Refresh
button doesn't yet work. To enable it, in containers/App.js
dispatch two further actions:
-
The first should invalidate the selected reddit by dispatching the
invalidateReddit()
action. What parameters does this require? How does this update the state tree? -
The second should fetch new posts if required. Dispatch
fetchPostsIfNeeded()
, again passing in the required parameter(s).
-
If the app goes offline (devtools
Network
➞Offline
), thefetch
doesn't succeed. Add a way to propagate the error to the UI. -
Enhance the app to enable you to click on a post to retrieve the corresponding comments.