Build a Memory Game with React #4 - Creating a Card Component
Key Takeaways
The video demonstrates how to extract a card template into its own component in a React application, making it easier to apply logic to each card later on. The instructor creates a new file called SingleCard.js and imports the necessary CSS file, then cuts the card template from the main component and pastes it into the new SingleCard component.
Full Transcript
all right then gang so i think it's about time for a little challenge for you to do and we're going to keep it relatively simple for this first project challenge but then later on there's going to be a slightly more difficult challenge as well so what i want you to do is extract all of this card template into its own components because that's going to make it much easier to apply logic to each card later in the project so you can call that component singlecard.js and put it into the components folder which i've already created over here i've also created a single card.css file which we're going to hook up with our single card component later on as well but anyway when you're done everything should work exactly the same way as it does now the only difference will be that we've extracted the card template into its own component so pause the video here and give this a whirl then if you want to see my solution just hit play okay then so hopefully you had a good go at that now i'm going to show you how i did this so first of all let's create a new file inside the components folder called single card dot js and then inside here i'm going to say underscore rfc and hit tab to create this functional component called single card and then inside this component first of all i'm going to import the css now don't worry if you didn't do this you don't need to do that as part of the challenge i'm just doing that so that later on the card looks a bit nicer but anyway import and it's dot forward slash single card dot css all right then so now what we want to do is take all of this template right here this div and we want to place it inside the card component so let's cut it and place it right here so let's tidy this up we have this div with a class name of card and inside it we have another div with the two images now there's two problems here first of all we don't need the key right here because now we're not inside this map function right here we output the key down here so what i'm going to do is first of all nest our single card component inside this map function so that for each card we're outputting the single card component so let me do that single card oops we need to spell this correctly single card so we also need to import it at the top let me do that up here oh it's automatically done it for me cool so now we can add on the key property which is equal to the card dot id so this is where the key goes right inside this component where we're mapping through the cards now what we can do is we can take it out of this thing right here because we don't need it in this component now the other problem is that we're trying to access the card object right here for the source of the image and we don't have access to that right here it's only in this component that we have access to the cart but what we could do is pass down a prop like so so we'll call the prop card and send it in to that component so we're sending this thing in right here so if we save that now we can accept the prop up here by destructuring it from the props and now we have access to it in the template so this should all work the same way now but before we preview it i just wanted to show you the css that i've added so if we go over here we can see for each card we give it a position of relative and for each image we say width 100 display block border is 2 pixels solid and white and a border radius is 6 pixels so some very very simple styles all right then so let's try this out in the browser everything should still work the same way it's just that now we've moved all of our card template into its own component that can be reused and that's going to make it much easier in the future to apply logic to single cards inside this component and now in a browser everything still looks as though it's working the same way it looks a little bit nicer because we added those styles to the cards right here and if i click on new game they should all shuffle which they do awesome
Original Description
🐱👤 Get the full React Course here (discount auto-applied):
https://thenetninja.co.uk/udemy/react-and-firebase
🐱👤 My other Udemy courses:
https://thenetninja.co.uk/udemy/vue-and-firebase
https://thenetninja.co.uk/udemy/modern-javascript
https://thenetninja.co.uk/udemy/d3-and-firebase
🐱👤 Get access to all other premium courses on Net Ninja Pro:
https://netninja.dev/
🐱💻Free React course on YouTube:
https://www.youtube.com/watch?v=j942wKiXFu8&list=PL4cUxeGkcC9gZD-Tvwfod2gaISzfRiP9d
🐱💻 My Social Links:
Facebook - https://www.facebook.com/thenetninjauk
Twitter - https://twitter.com/thenetninjauk
Instagram - https://www.instagram.com/thenetninja/
🐱👤 Get access premium courses on Net Ninja Pro:
https://netninja.dev/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Net Ninja · Net Ninja · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
GraphQL Tutorial #5 - Express App Setup
Net Ninja
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
GraphQL Tutorial #8 - Root Query
Net Ninja
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
GraphQL Tutorial #12 - Author Type
Net Ninja
GraphQL Tutorial #13 - Type Relations
Net Ninja
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
GraphQL Tutorial #18 - Mutations
Net Ninja
GraphQL Tutorial #19 - More on Mutations
Net Ninja
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
GraphQL Tutorial #23 - Create React App
Net Ninja
GraphQL Tutorial #24 - Book List Component
Net Ninja
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
GraphQL Tutorial #28 - Add Book Component
Net Ninja
GraphQL Tutorial #29 - External Query File
Net Ninja
GraphQL Tutorial #30 - Updating Component State
Net Ninja
GraphQL Tutorial #31 - Composing Queries
Net Ninja
GraphQL Tutorial #32 - query variables
Net Ninja
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
GraphQL Tutorial #34 - Book Details Component
Net Ninja
GraphQL Tutorial #36 - Styling the App
Net Ninja
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
Updated Vue & Firebase Course (Udemy)
Net Ninja
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja
More on: React
View skill →Related Reads
📰
📰
📰
📰
Inside the Wayfair Frontend SDE-2 Interview: A Complete Breakdown
Medium · Programming
I Spent Two Years Maintaining a React SPA. HTMX Rebuilt It in a Week
Medium · Programming
The 5 Levels of Front End Engineering (And Where Most Developers Get Stuck)
Medium · Programming
Browser-Based PDF Editing with Vue 3 and pdf-lib
Dev.to · sunshey
🎓
Tutor Explanation
DeepCamp AI