Clone this repo and run npm i
and then npm start
-
You've already seen this app before (twice now) so you should be pretty familiar with it. In this assignment we will incorporate Redux into the application so all components have access to the cars. We will also add additional properties to the state and see how that effects things.
-
We've removed the login/logout functionality so you don't have to login each time to see your changes.
-
Note that the Redux setup is done for you, the purpose of this excercise is to get familiar with
mapStateToProps
to pass the data to your components. You will also be creating the containers. -
Even though the setup has been done, please go look at the files under the "redux" folder. Remember, there are 3 files needed for this setup. They are:
state.js
: The inital state of your applicationreducers.js
: A file with functions that determine how to serve your state (more on that next lesson)store.js
: The combination of "state" and "reducers" that is injected into your application via theProvider
-
Once you've navigated to (and gotten familiar) with the files above.. go to the
App.js
and see theProvider
wrapping our application. -
Finally, click the Dashboard tab and notice that a new component was created with a table of all the cars. Look at the code for that component and see that it's mostly still components from Material UI.
-
Let's start with the
Home
component. Navigate to it and notice that it is importingcars.json
and mapping through that to display the cars. We want to attach "cars" to the "props" of this component so that it can be re-used. Under the "containers" folder, create a new file calledHome.js
. -
Inside
Home.js
do the following:- Import "connect" from "react-redux"
- Import the
Home
component from the components folder - Write a
mapStateToProps
function that maps "cars" to "state.cars" - Export the connected component and mapStateToProps combination
-
Now, go to the
Router.js
file and see whereHome
is imported at the top of the file. Change its path so that it referencescontainers
instead ofcomponents
. -
Go to the
Home
component (in the components folder) and replace the use of "cars" with "props.cars". Everything should have stayed the same (that means it worked). If so, go ahead and remove the import forcars.json
at the top of the file. -
The
Dashboard
andCar
components need the exact same setup so follow the same instructions for them. Create files for them in thecontainers
folder. You can copy/paste from theHome
container but make sure to changeHome
to reference the appropriate component. -
Make the switch for
Dashboard
andCar
in theRouter.js
file just like we did forHome
. Make sure to useprops.cars
in these components as well and remove the import tocars.json
. -
Everything look good so far? If not, ask the instructor.
-
Navigate to the
Dashboard
component and see the text, "Welcome, Name". Let's change that. To do that we are going to add a user to our global state and then pull it into theDashboard
component. -
Go to the
redux/state.js
file and add a property called "user" above cars. Give it the following value:
{
username: 'test-user-1',
email: 'test-user@example.com'
}
-
Now, go back to the Dashboard container and add this user in the
mapStateToProps
function. -
Finally, in the Dashboard component, reference
props.user.username
to show the username instead of the word "NAME". Whenever another component needs access to the user, they can simply add it in their mapStateToProps function as well. -
Good work!