React - Level 2 - React with Basic JS Exercise - Build a Dynamic Friends List with React #252
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 Dynamic Friends List with React!
Hey there, budding React developer!
You've learned about components and how to use child components in React. Now, let's take it up a notch by incorporating some essential JavaScript concepts like arrays, loops, and array functions into your React app. In this fun exercise, we'll create a dynamic friends list where you can display and manage a list of your friends. Ready to make your app more interactive and engaging? Let's dive in!
🌟 Exercise Overview
Objective: Create a React app that displays a list of friends using components and JavaScript array methods.
What You'll Learn:
map()function.🛠️ Let's Get Started!
Step 1: Set Up Your React App
If you haven't already, set up a new React app:
npx create-react-app friends-list cd friends-list npm startThis will start the development server and open your app in the browser.
Step 2: Plan Your Components
We'll need the following components:
Step 3: Create the Friend Component
Create a new file called
Friend.jsin thesrcfolder.📝 Hint: Use
propsto display the friend's name.Step 4: Create the FriendList Component
Create a new file called
FriendList.jsin thesrcfolder.📝 Hints:
map()function to iterate over thefriendsarray.keyprop to eachFriendcomponent.Step 5: Update the App Component
Open
App.jsand modify it to include theFriendListcomponent.📝 Hint: Render the
FriendListcomponent inside theAppcomponent.Step 6: Add Some Styling (Optional)
Update
App.cssto make your friends list look nicer:🚀 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 a list of your friends displayed!🔄 Recap and Concepts
map()function to render lists of elements in React.keyprop to each item in a list to help React identify which items have changed.🧩 Fill-in-the-Blanks Solutions
Step 3: Friend.js
Step 4: FriendList.js
Explanation:
friends.map(...): We use themap()function to iterate over each friend in thefriendsarray.key={index}: We use the index of the array as a unique key for eachFriendcomponent.name={friend}: We pass the friend's name as a prop to theFriendcomponent.Step 5: App.js
🎉 Congratulations!
You've successfully created a dynamic friends list in React! You've learned how to:
map()within React components.🌟 Extra Challenges
Add More Friends: Modify the
friendsarray to include more names.Make It Interactive: Allow users to add new friends to the list.
Hint: You'll need to use React's state management with the
useStatehook.Display Friend Details: Expand the
Friendcomponent to display more information like age or hobbies.Styling: Enhance the styling to make your app look even better.
Understanding the Concepts
1. The
map()FunctionIn JavaScript, the
map()function creates a new array by calling a function for every array element.In React,
map()is often used to render lists of components.2. Keys in Lists
Keys help React identify which items have changed, are added, or are removed. Keys should be given to the elements inside the array to give the elements a stable identity.
3. Props and Child Components
Props are arguments passed into React components. Props are passed to components via HTML attributes.
Keep Experimenting!
Don't stop here! Try modifying your app to deepen your understanding:
Sort the Friends List: Alphabetically sort the friends before rendering.
Hint: Use the
sort()method.Conditional Rendering: Highlight a friend's name if it meets certain criteria (e.g., name starts with 'A').
Loop Variations: Explore other array methods like
filter()andreduce().Happy Coding and Keep Building Amazing Things with React!
Beta Was this translation helpful? Give feedback.
All reactions