Exercises
Time to make a React app to share a hobby you love with your friends and family!
The solution in the react-part3-exercises-vite
branch of the react-exercises-and-studios
repo uses needlepoint as an example hobby. Whenever you see a reference to needlepoint in the solution to the exercises, substitute it for your own chosen hobby.
Create a new application called
exercises
in thepart3
directory in the react-exercises-and-studios repo.In
App.jsx
, remove the boilerplate content as you have done in previous lessons. Replace it with an<h1>
heading that says “My Hobby: " with your hobby coming after the colon.Create a new
components
directory insrc
.Create a new file called
Introduction.jsx
incomponents
and inside create a new functional component calledHobbyIntroduction
.This functional component return a
<div>
that contains the following:- An
<h2>
heading that says something to the effect of “3 Fun Facts About this Hobby”. - An ordered list that contains three facts that you want others to know about your chosen hobby.
- An
Import and Call the new
HobbyIntroduction
component under your<h1>
heading inApp.jsx
.Run the application to see your new fun facts! Once you are satisfied, turn off the server so we can practice with state.
Inside
src
, create a new file calleddata.json
. We are going to use this file to make a small gallery of images of projects or plans you have for your hobby. Inside this file, set up a new list of JSON objects calledprojects
. Your list should contain 5 JSON objects and each one should have 4 properties that make sense with what your hobby is.Create a new file called
Projects.jsx
incomponents
. This file will hold a functional component calledMyProjects
.Import the data from
data.json
anduseState
from'react'
at the top ofProjects.jsx
.Inside the
MyProjects
component, start by setting up a state variable for the index so we can loop through our list of projects. You can do this with the following code:const [index, setIndex] = useState(0);
.Now we need to set up some local variables and a return statement.
- First, set up a JavaScript variable that contains your list from
data.json
. Then set up a variable that contains the item in that list at the position determined byindex
. - Inside
return
, set up a button that contains the text, “Next”. Set theonClick
attribute to{handleClick}
. - Below your button, add HTML that will nicely display all four properties of your JSON objects.
- First, set up a JavaScript variable that contains your list from
Finally, above the return statement, we need to set up
handleClick()
like so:function handleClick() { if (index < data.projects.length-1) { setIndex(index + 1); } else { setIndex(0); } }
Return to
App.jsx
and call the completedMyProjects
component belowHobbyIntroduction
. Run the application to see your new gallery! Hit the Next button to see state in action!
Submitting Your Work
Commit and push your work to GitHub and submit the link to your repository on the assignment page in Canvas.