- Update state based on API calls with Redux
Start by cloning and setting up the API for this lesson:
Then clone and setup the client:
Use your own checklist or follow the steps bellow to setup redux in the Music Box Client.
- Begin by installing
redux
,react-redux
- Create a
store.js
file that exports a newly created store (you can temporarily use an anonymous function for the reducers) - Add the store to
index.js
using theProvider
componenet fromreact-redux
- Create an artists reducer file with a blank array as inisital state
- Add the artist reducer to
store.js
usingcombineReducers
- Connect
App.js
to your redux store usingconnect
-
Install
redux-thunk
andredux-logger
packages with npm. How do these packaged relate to redux? What isredux-thunk
and why are we using it now? How doesYour answer...
-
Next we're going to create an action that will request all the artists from our server. Create a new actions folder and file. What changes to our actions do we need to make now that we're using
redux-thunk
?Your answer...
-
Since we'll be working with asynchronous code, do we need to do anything different with our reducer? Why or why not?
Your answer...
-
Update your reducer to listen for the action type you created. Then, add your action creator to the
App.js
file. What component lifecycle method do you use inApp.js
?Your answer...
-
How might we implement the delete functionality for each artist? Consider that to delete an artist you will need to update both the API and the local state. After spending a minute or two planning, complete this functionality. There are at least two different ways to update the local state.
What approach did you take?
-
Consider how creating a new resource might work with Redux. In this case, there are a number of considerations to think about:
- What happens if there's an error in the form input?
- What happens if there's an error that comes back from the database?
- What happens if a new record is created?
Take a few minutes with your group to discuss how you might implement the create feature.
What you discussed...
Discuss with other groups how they're planning to solve the problem. Then, do it! Make sure that you show any errors that arise from submitting the form.