Exercises
Getting Started:
Check out the starter code for the exercises-2
in the part2
directory on the main
branch of react-exercises-and-studios
Code Review
App
is returning 2 components:Profile
andBanner
.- Open the
components
directory to see what it contains.You should see the following files:
Banner.jsx
- Creates a banner for the application
Button.jsx
- Creates a button for the application that uses the
alert
Window method.
- Creates a button for the application that uses the
oceans.json
- holds multiple objects, which we will use in our application
Profile.jsx
- Will become a list template that holds an image, name, facts, and a button for each
json
object inoceans.json
.
- Will become a list template that holds an image, name, facts, and a button for each
styles.css
contains the styling we will add to our application.
These are the files we will be working with for this project.
Part 1: The Banner Component
We would like to update the Banner.jsx
to fit the application better.
- Update the
Banner
function to return an level header that says “The Amazing Ocean” instead of “Hello World!”. - Apply the CSS styling for
header
.
Part 2: Button.jsx
We would like the button to do something. Right now, when you click on it nothing happens. For this application, we will create an alert method that will share a message with a water or ocean theme.
- Inside the
Button
function, create a new function calledonLearnMore
. This function will hold thealert
method. Your method should display a message like “Splish Splash” or something related to the ocean or water. - Update the
return
statement of theButton
function. It should activate theonLearnMore
alert when clicked.
Part 3: Profiles Component
This last component will create a list of profile objects. Each profile object will contain headers, an image, a list, and a button. We will use the map
function to help us create this list.
There are a lot of steps to make these profile objects. We are going to work on each part separately.
A. Create the listItems
function
- Open the
oceans.json
file. It contains 7JSON
objects. Each object has multiple key/value pairs, such as aname
andimage
. We will use these key/value pairs to create our list. - Import the
oceans.json
file. Give it a meaningful alias, such asoceans
. - In the
Profile
function, remove the<h3>
placeholder, but keep the<Button />
.
B. Add the Profile Image
Each profile object will have an emoji to represent it.
- We will use
map
to help us iterate through theimage
values in the JSON file.- Declare a variable called
listItem
and instantiate it equal tooceans.map()
. - Set the callback parameter of
listItem
toocean =>
. - The map function should return
ocean
which will include all elements required to create a profile object.
- Declare a variable called
- Each profile will be contained in its own
<div>
. To keep each<div>
unique, we will need to add akey
which is set to theid
property of each JSON object. We are working with JSON objects, so you will need curly braces. - Using an image tag, set the
src
attribute equal to the image’s url. This is stored in the JSON file. Thealt
attribute should be set to thename
property. - The starter code contains some CSS styling for the image called
img
. Apply the styling to the image by using theclassName
attribute. - Make sure we have all the correct closing tags.
- The
Profile
function needs to return an list oflistItems
and the<Button />
.
C. Background Colors
Before we start adding more details, let’s add some styling to help us visualize our list.
In the same
<div>
where you declared the key, add the styling “profile”.You should see a small window of color for each emoji. There should be space between each block.
Let’s use the background colors to help organize ocean creatures by type. If the creature is a fish, apply the
isAFish
styling. Else, keep theprofile
styling. Use thefishCheck
property of the JSON file to add conditional rendering to this project.
D. Names
Let’s add names to the profile objects.
Create an
<h1>
tag to display thename
property of the JSON file.
E. Fun Facts
Inside the JSON file, you will see that each JSON object has 3 facts. We are going to share those facts in a list within the profile.
Create a new header for the name of your list. Make sure that is smaller than the name.
You decide if you want an ordered list or unordered list.
Each list element should be a fact property.
F. Learn More
Let’s move the button into each profile object.
- Relocate
<Button />
insidelistItems
below the list of fun facts.
- Relocate
G. OPTIONAL Code Clean Up
Since Profile
is no longer returning multiple elements, you can remove the unnecessary fragment tags.
Final Steps
Congrats you have used the map function to iterate through a collection of JSON objects to create small profiles of various creatures found in the ocean.