Code Monkey home page Code Monkey logo

411_wk4_day1_react_router's Introduction

Setup

Clone this repo and run npm i and then npm start

Project Instructions

  • Two components have already been created for you, Home & About.

  • Look for the Router.js file under the src folder. This file is basically empty besides a couple imports. You need to create a functional component called Router in this file. You will return a Routes wrapper container that wraps two individual Route's. The two Route's should be the following:

    • "/" -> Home
    • "/about" -> About
  • Now we are going to take this Router and import it into App.js. Place it right underneath the Navigation component. You will need to import BrowserRouter from react-router-dom and use it to wrap both the Navigation and the Router components.

  • If you were successful you should see a homepage with a lot of different car details on it. You should also be able to add "/about" to the end of the url to see the about page. Typing this paths in the browser will soon get tiring so let's add them to the Navigation component. There are two commented areas for you to add links. Create one Link to the Home page and one to the About page. If you forget how to do that, reference the documentation here.

  • Next we want to fill out the Car component in src/Car.js but first we will need to be able to see it and Link to it. Inside of Router.js import the "Car" component and create a route for it. The path should be able to accept an id for the specific car as well. For example: "/car/:id".

  • Once that is complete, go to the Home component and look for the comment regarding the "a tag". Change that to a Link component and have the "to" property be: /car/${car.id}. You can leave the text, "See More Details" the same. Notice how you are routed to the "Car" component when you click on the Link. Try clicking on different cars, do you notice that the id is different each time?

  • Now that we can navigate to the Car component we want to fill out this page so that it looks nice and provides details about the specific car we passed in. To get the specific car we need to access the "id" property from the path. We can do that by accessing it using a hook from "react-router-dom". The useParams hook return an object of key/value pairs of the dynamic params from the current URL that were matched by the Route's path. At the top level, make sure to import { useParams } from "react-router-dom". Once we have that hook being imported, inside of our component, but before the return, we want to instantiate useParams() and destructure the object it returns. It will look like this: const {id} = useParams(). Now that we have destructured the object that useParams returns, we now have access to the key/value by using the id variable. Check out the docs for useParams here.

  • Note that if you would have called the parameter (id) something different in the router, say "carId", then that would change how we access it here as well const {carId} = useParams(). Also, if you feel more comfortable, instead of destructuring, we can simply access our dynamic values with this same line: const carId = useParams().carId.

  • Now that we have the specific id we need to find the car in the list that matches that id. You should know how to find items in an array by now so go ahead and find the specific car. Hint: the cars are in the "cars" variable at the top of the file.

  • Create the Car component so that it looks like the image below. To do that, import the MUI components as specified at the top of the file. You will use the MUI documentation to figure out how to style this page.

car component

411_wk4_day1_react_router's People

Contributors

hipperger avatar cesarrrc avatar slyssy avatar krchapman avatar mxviteri avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.