React - Level 3 - React with Intermediate JS - Build a Fun Animal Gallery with React #253
akash-coded
started this conversation in
Tasks
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Title: Build a Fun Animal Gallery with React!
Hello, aspiring React developer!
Now that you've got a grasp of the basics of React, including components and props, let's put that knowledge into action with some classic JavaScript concepts like arrays, loops, and functions.
In this exercise, we'll create a simple but engaging Animal Gallery app. You'll display a list of animals with their names and images using React components, and along the way, we'll reinforce key JavaScript concepts.
Ready to dive in? Let's get started!
🌟 Exercise Overview
Objective: Create a React app that displays a gallery of animals using components, props, and JavaScript concepts like arrays and loops.
What You'll Learn:
🛠️ Let's Get Started!
Step 1: Set Up Your React App
First, let's set up a new React app:
npx create-react-app animal-gallery cd animal-gallery npm startThis will start the development server and open your app in the browser.
Step 2: Plan Your Components
We'll create the following components:
Step 3: Prepare Your Data
In
App.js, we'll define an array of animal objects. Each object will contain a name and an image URL.App.js
Concepts Used:
Step 4: Create the Gallery Component
Create a new file called
Gallery.jsin thesrcfolder.Gallery.js
📝 Hints:
AnimalCardcomponent. You can use the animal's name.nameandimageprops to theAnimalCardcomponent.Concepts Used:
map()to iterate over an array.Step 5: Create the AnimalCard Component
Create a new file called
AnimalCard.jsin thesrcfolder.AnimalCard.js
📝 Hints:
props.name.srcattribute of theimgtag toprops.image.props.namefor thealtattribute for accessibility.Concepts Used:
Step 6: Style Your App
Let's add some basic styling to make the gallery look nice. Update
App.css.App.css
Concepts Used:
Step 7: Run Your App
Save all your files, ensure your development server is running (
npm start), and check your app in the browser. You should see your animal gallery displayed!🔄 Recap and Concepts
map()): Iterating over arrays to render lists.🧩 Fill-in-the-Blanks Solutions
Step 4: Gallery.js
Explanation:
key={animal.name}: Using the animal's name as a unique key.name={animal.name}: Passing the animal's name as a prop.image={animal.image}: Passing the animal's image URL as a prop.Step 5: AnimalCard.js
Explanation:
{props.name}: Displaying the animal's name.src={props.image}: Setting the image source to the passed image URL.alt={props.name}: Providing an alt text for accessibility.🎉 Congratulations!
You've successfully built an Animal Gallery app using React components and fundamental JavaScript concepts. You've learned how to:
map()and unique keys.🌟 Let's Dive Deeper into JavaScript Concepts
1. Arrays and Objects
Example:
2. The
map()Functionmap()method creates a new array by applying a function to each element of the original array.Example:
In React,
map()is commonly used to render lists of components.3. Props in React
Example:
In the
AnimalCardcomponent, you can access these props:4. Keys in Lists
keyprop.Example:
5. Functions and Components
Example:
🌟 Extra Challenges
Let's make the app more engaging by incorporating more JavaScript concepts.
Challenge 1: Add More Animals
Expand your
animalsarray with more animal objects.Example:
Challenge 2: Sort the Animals Alphabetically
Use a JavaScript function to sort the animals by name before rendering.
App.js
Concepts Used:
sort()to order elements.Challenge 3: Group Animals by Category
Suppose animals have categories like 'Mammal', 'Bird', 'Reptile'. Update your data and modify your components to group animals by category.
App.js
Gallery.js
Concepts Used:
filter()to select items.Challenge 4: Use Functions to Format Data
Create a function to format animal names (e.g., uppercase the first letter).
App.js
AnimalCard.js
Concepts Used:
Challenge 5: Conditional Rendering
Display a message if there are no animals to show.
Gallery.js
Concepts Used:
ifto control rendering.Challenge 6: Add Animal Descriptions
Update your
animalsarray to include descriptions and display them.App.js
AnimalCard.js
Concepts Used:
Challenge 7: Use External Data
Fetch animal data from an external JSON file or API (without using state).
Note: Since we haven't introduced state or effects, you can simulate this by importing data.
Create a file
animalData.js:App.js
Concepts Used:
Understanding the Concepts
1. Arrays and Loops
Arrays store multiple values, and loops allow you to perform actions on each item.
For Loop:
For...of Loop:
Array Methods:
map(): Transforms each element.filter(): Selects elements based on a condition.sort(): Orders elements.2. Functions
Functions encapsulate reusable code.
3. Objects
Objects store data in key-value pairs.
Access properties with dot notation (
animal.name) or bracket notation (animal['name']).4. Conditional Statements
Control the flow of your program based on conditions.
If Statement:
5. Destructuring
Extract values from arrays or objects.
Use in function parameters:
🧠 Test Your Understanding
Try to answer these questions without looking back:
How do you pass data from a parent component to a child component?
Answer: By using props.
Why is it important to use a unique
keywhen rendering lists in React?Answer: To help React identify which items have changed, are added, or are removed, ensuring efficient updates.
How can you loop over an array in JavaScript to render multiple components?
Answer: By using the
map()method.What is the purpose of the
returnstatement in a function?Answer: To specify the value that the function outputs when called.
🚀 Keep Experimenting!
Feel free to modify and expand your Animal Gallery app. Here are some ideas:
HeaderorFooter, to organize your app.👩💻 Next Steps in Learning React
Once you're comfortable with components and props, the next concepts to explore are:
Keep up the great work and happy coding!
Beta Was this translation helpful? Give feedback.
All reactions