This repository is a part of CSI-248 at Renton Technical College.
-
Clone the repository to your local machine. (Do not use OneDrive for assignments in this course!)
-
Make note of the folder where you cloned the repository.
-
After you have cloned this repository navigate to your local repository using the cd command.
-
Open the repository in Visual Studio Code by typing
code . -
Open the terminal in Visual Studio Code by hitting ctrl + ` or cmd + ` on mac.
-
Create a new React project in the current directory using vite
-
Name the project citylist
-
Select React as the framework
-
Select JavaScript as the variant
-
cd into the new project folder and run:
-
npm install -
npm install react-router-dom
- Run
mkdir Screenshotsto create a Screenshots folder.
- Delete the contents of the App.jsx, App.css and index.css files.
- Create a basic h2 inside of a fragment for App.jsx.
- Take a screenshot and save it to your screenshot folder.
- Included in the repository is a cities.js file, paste the contents of that file at the top of App.jsx
- When complete your App.jsx should look like this. Notice that I have collapsed the cities array here, it is taking up 27 lines of code.
- In Terminal, type
npm run dev. Make sure you are in the citylist folder. - You should have just App as an H2 showing on the page.
- Take a screenshot of the browser and save it to the Screenshots folder.
git add .git commit -m "project setup complete"git push
- Create a new folder inside of src called Pages.
- Within this folder create the following Components. Create.jsx, Details.jsx, Home.jsx, and List.jsx
- Within each component simply create an h2 with the name of the component.
- Let's first test these new components out and make sure they are working.
- Inside App.jsx import each of these components and display them on the page.
- Take a screenshot of your browser and save it to your Screenshots folder.
- Once you have verified they are working you can remove the components from inside of the return(). We are going to render these based on a route.
- Lets create some Routes. We need to import some Components from react-router-dom. Add this to the top of App.jsx
import { BrowserRouter, Route, Routes } from "react-router-dom";
- The BrowserRouter component must wrap all components that will use Routing
- Within the BrowserRouter we will define Routes using the Routes and Route components
- Replace the code inside of App.jsx with the following:
- We have defined 4 routes. If no route is provided we will show the Home Page.
- If we navigate to /cities we will show the List page.
- If we navigate to /cities/create we will show the Create page.
- If we navigate to /cities/1 we will show the Details page
- Verify that these routes are working by running the app and navigating to them.
- Take a screenshot of each page and save them to your screenshots folder.
git add .git commit -m "Routes working"git push
- Create a Components folder inside of the src folder.
- Add a Navigation.jsx component to that folder.
- At its core a navbar is simply a styled list of links.
- We will be using the Link component from react-router-dom.
- Replace the code inside of Navigation.jsx with the following:
- Notice that the to attribute of these links is the same as some of the routes you define earlier.
- Import Nagivation to App.jsx and render it within the browser router but before the routes.
- Run the app and verify that Nagivation is working:
- Take a screenshot of the browser and add it to your Screenshots folder.
- It is now time to useContext.
- Context is a global state container that can be used by any component without passing via props.
- We are going to add two things to context. The list of cities and a function for creating a new city.
- citiesList will be tracked by state in App.jsx but shared with the other pages via context.
- Similarly the createCity function will also be in App.jsx but accessible to the other pages via context.
- Lets first create the citiesList state and createCity functions.
- At the top of App.jsx add an import statement for useState and createContext.
import { useState, createContext } from "react";
- Inside of the App() function but before the return statement add the cities to a state variable called citiesList.
- Also inside of App() and before the return statement create a function that takes a city as a parameter, assigns it an id and then uses setCitiesList to update the list.
- We are now ready to create a ContextContainer. This is done using the createContext method that we imported earlier.
- Create a CitiesContext before the function App()
- Our new CitiesContext is ready to be used. We need to wrap all elements that will have access to this Context in a CitiesContext.Provider component. The value attribute will be used to specify which object we would like to provide through this context.
- We have wrapped all of our routes with our new Context and added both citiesList and createCity to that context. We now must also export the CitiesContext so that it can be imported by our other pages. Add the following to the bottom of App.jsx
git add .git commit -m "Context added"git push
- Lets work on the Details page first.
- In the route created for the details page we can see that details takes an id.
- We will need to get that id from the route and then go through the citiesList to find the city with a matching id.
- We will need access to the citiesList from context as well as the useParams method from react-router-dom to get the id from the route.
- import the following at the top of Details.jsx
- useContext allows us to access our CitiesContext, useParams() will give us the id from the url
- Replace the Details function with the following code:
- Run the app and test it out. If you navigate to /cities/1 you should see the following:
-
Providing a different id will give you a different city.
-
Take a screenshot of the browser and save it to your Screenshots folder.
-
Now we are ready to work on the list. This list page will show all of the cities in the list and provide a link for each city to the Details page.
-
The List page will import useContext, CitiesContext, and Link
-
At the top of the List page add the following:
- Replace the List Function with the following:
- You should now have a functioning list page that links to the details page.
- Take a screenshot of the browser and save it to your Screenshots folder.
git add .git commit -m "List and Details Complete"git push
- Our create page will allow the user to add a new city to the list.
- It will contain a form and need access to the creatCity function that we added to Context.
- We will need useContext and the CitiesContext for create.
- Add these import statements to the top of Create.jsx
- Lets start with the HTML for this page first since it is fairly long.
- Create an HTML form for adding a city and set the onSubmit event to call a function called handleSubmit
-
Now lets work on our handleSubmit function. This function will take in an event. The event will have the information from the form. By default events cause a page refresh which we do not want since we are handling the client side. Prevent the default refresh and then get the data from the form. We then pass the data to the createCity function and reset the form.
-
Replace the code inside of Create() with the following:
- Use the form to create a new City.
- Navigate to the Cities List and you should see your new City added to the list.
- Click on the new city and observe the Details page.
- Add styling to the navbar so that it looks like navigation and not simply a list of links.
- Style the form so that it is centered and inside of a container.
- You may use any css library that you wish or write your own.
- Here is an example. You do not need to match it exactly.
git add .git commit -m "Assignment Complete"git push
If you have any questions about this assignment please reach out to myself or our TA for this course.































