Astro Crash Course #7 - Reusable Components

Net Ninja · Beginner ·🖌️ UI/UX Design ·3mo ago

Key Takeaways

Builds a content-driven website using Astro web framework and React components

Full Transcript

Okay, then. So, in the previous lesson we output a list of books, each one with their own bit of template for the author and the title, and we store those books in a kind of book card, right? So, this is completely fine, but we could also extract this book card template and the styles into its own reusable components, and then we could just drop that component into this template where we need it. Now, the benefit of that is that we're modularizing the application a little bit more then, so that we can reuse components where we need them, and we also keep our page components a little leaner. So then, let's do that in this lesson. We'll make a book card component to hold all of this template and the styles for the template as well, and then we'll use that component right here. First then, we'll need to make that component somewhere, and we're free to make that wherever we want inside the source directory, but a common convention is to make a components folder and put them all in there. So, we'll do that. We'll create a new components folder right here, and then inside that, we can create a new file, which I'm going to call bookcard.astro. Now, remember we use the astro because this is going to be an astro component. It's not a page, but it's still an astro component. Okay, so now inside this component, we're going to first of all add the front matter at the top using two sets of three dashes, and then below that, we want to add the template. So, I'm going to head back to the books page now, and I'm going to cut the template that we output for each book. Then, I'm going to replace that template with the new book card component we're making, and if we click on this option right here, it should automatically import that component up at the top, which we need to do. After we've done that, I'm going to come to the book card component and paste in the book card template that we want to output for each book. So, all we've done so far is extract the template into its own book card components, and then we're outputting the book card components for each list item on the books page, right? Now, we are going to get some errors when we do this, and that's because we're trying to access the book title and author in this template, and we don't have those values available to us yet. Now, they're available in the books page because that's where we initialize the books value, right? But they're not available in here. There's no book variable, right? So, to solve this, we need to pass that book variable as a prop into the book card component when we output it in the template. And now, prop is just short for property. It's basically a way to pass a value into a nested component like this, and we can have multiple props passed into components. So, we can name props whatever we want in astro. In this case, I'm going to add a prop to this component called book, and then as a value, we want to pass something dynamic, so we use curly braces, and then we want to pass the book we're currently iterating that we have access to right here in this function. So then, now we're passing the individual book into the book card components, and we're doing that for each book in the array, one after the other, right? Next then, let's head back to the book card component to access that book prop. Now, the way we accept props within a component is by destructuring them from a special props value given to us by astro. So, we can say const, then curly braces to destructure a prop, and we set that equal to astro.props. So, any prop we pass into this component will be stored on this props value given to us by astro, and then we can just destructure them right here. So, we passed in the book prop, and that means we can grab that book value like this. And now, we can go ahead and use that book value in the template, and you'll see that those errors that we had before have now gone away because we do have the book object available. The next thing I want to do is move all the styles from the books page into this new component. So, let's first of all copy all of those card styles, and once we've done that, in fact, you want to cut them, not copy them, and once you've done that, you can come back and make a style tag down here, and then paste them all in. Okay, so this should all work now the same as before. We've not added any new functionality, we've structured the app a little bit better by making this component reusable, but it should do the same thing. And in the browser, we can see that. They're all still here, they all look the same, it's still working identically as it was before, it's just that now we've restructured it. Okay, so currently we're accepting a prop into this book card component, but there's two things I want to highlight. First, when we're accessing properties on the prop in the template, we don't get any intellisense to tell us which properties are on the book, and I can demo that by deleting the title down here, and then I'm going to put a full stop after the book, and we don't see the properties. Second, as it stands, we could pass any prop that we wanted into this component, and there would be no checks or errors when we did so. So, I could come and pass another prop into the book card component, I don't know, called name, for example, and astro would currently let me do that, even though we're not using the name in that component, and we don't really want it. So, to combat both of these things, we can use a typescript interface within the front matter of the component to define how the props should look. And the way we do that is by coming up here and saying interface, and we call this props with a capital P, and then curly braces. And within this, we can define what props should be allowed into this component, and the fields and types of those props as well. In our case, we're allowing a book prop, and the type of that is going to be an object where two properties need to exist. So, curly braces first, right? Then, we can say we want a title property on the object, which is going to be a string, and we also want an author property on the object, which should be a string as well. And now, astro will automatically pick this props interface up and check that the correct props are being passed into the components. And now, if we try to pass an additional prop in, which I'm going to go back over here and show you, then we're going to get an error because that prop wasn't defined on the component, right? Also, because we defined that props interface, we now get intellisense when we use it in the template, which I can show you by coming back and deleting the title field again, and then I'm going to put a dot after book, and when we do that this time, we can see the two properties available to use. All right then, so we've made a reusable component now, and we've seen how to pass a prop into that component.

Original Description

In this Astro tutorial series, you'll learn how to use the Astro web framework to make a content-driven website. You'll also learn how to add React components to the site, and deploy the finished application to Netlify. 🍿👇 Get early access to the whole course on NetNinja.dev https://netninja.dev/p/astro-crash-course 🔥👇 Get access to ALL Masterclasses & premium courses with a Net Ninja Pro membership: https://netninja.dev/p/net-ninja-pro/#prosignup 🔗👇 Course files on GitHub: https://github.com/iamshaunjp/Astro-Crash-Course 🔗👇 Astro Docs: https://docs.astro.build/en/getting-started/
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 Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Regular Expressions (RegEx) Tutorial #14 - Matching a Username
Net Ninja
2 Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Regular Expressions (RegEx) Tutorial #15 - Email RegEx Pattern
Net Ninja
3 Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Regular Expressions (RegEx) Tutorial #16 - Finishing Touches
Net Ninja
4 GraphQL Tutorial #1 - Introduction to GraphQL
GraphQL Tutorial #1 - Introduction to GraphQL
Net Ninja
5 GraphQL Tutorial #2 - A Birdseye View of GraphQL
GraphQL Tutorial #2 - A Birdseye View of GraphQL
Net Ninja
6 GraphQL Tutorial #3 - Project (stack) Overview
GraphQL Tutorial #3 - Project (stack) Overview
Net Ninja
7 GraphQL Tutorial #4 - Making Queries (front-end preview)
GraphQL Tutorial #4 - Making Queries (front-end preview)
Net Ninja
8 GraphQL Tutorial #5 - Express App Setup
GraphQL Tutorial #5 - Express App Setup
Net Ninja
9 GraphQL Tutorial #6 - Setting up GraphQL
GraphQL Tutorial #6 - Setting up GraphQL
Net Ninja
10 GraphQL Tutorial #7 - GraphQL Schema
GraphQL Tutorial #7 - GraphQL Schema
Net Ninja
11 GraphQL Tutorial #8 - Root Query
GraphQL Tutorial #8 - Root Query
Net Ninja
12 GraphQL Tutorial #9 - The Resolve Function
GraphQL Tutorial #9 - The Resolve Function
Net Ninja
13 GraphQL Tutorial #10 - Testing Queries in Graphiql
GraphQL Tutorial #10 - Testing Queries in Graphiql
Net Ninja
14 GraphQL Tutorial #11 - GraphQL ID Type
GraphQL Tutorial #11 - GraphQL ID Type
Net Ninja
15 GraphQL Tutorial #12 - Author Type
GraphQL Tutorial #12 - Author Type
Net Ninja
16 GraphQL Tutorial #13 - Type Relations
GraphQL Tutorial #13 - Type Relations
Net Ninja
17 GraphQL Tutorial #14 - GraphQL Lists
GraphQL Tutorial #14 - GraphQL Lists
Net Ninja
18 GraphQL Tutorial #15 - More on Root Queries
GraphQL Tutorial #15 - More on Root Queries
Net Ninja
19 GraphQL Tutorial #16 - Connecting to mLab
GraphQL Tutorial #16 - Connecting to mLab
Net Ninja
20 GraphQL Tutorial #17 - Mongoose Models
GraphQL Tutorial #17 - Mongoose Models
Net Ninja
21 GraphQL Tutorial #18 - Mutations
GraphQL Tutorial #18 - Mutations
Net Ninja
22 GraphQL Tutorial #19 - More on Mutations
GraphQL Tutorial #19 - More on Mutations
Net Ninja
23 GraphQL Tutorial #20 - Updating the Resolve Functions
GraphQL Tutorial #20 - Updating the Resolve Functions
Net Ninja
24 GraphQL Tutorial #21 - GraphQL NonNull
GraphQL Tutorial #21 - GraphQL NonNull
Net Ninja
25 GraphQL Tutorial #22 - Adding a Front-end
GraphQL Tutorial #22 - Adding a Front-end
Net Ninja
26 GraphQL Tutorial #23 - Create React App
GraphQL Tutorial #23 - Create React App
Net Ninja
27 GraphQL Tutorial #24 - Book List Component
GraphQL Tutorial #24 - Book List Component
Net Ninja
28 GraphQL Tutorial #25 - Apollo Client Setup
GraphQL Tutorial #25 - Apollo Client Setup
Net Ninja
29 GraphQL Tutorial #26 - Making Queries from React
GraphQL Tutorial #26 - Making Queries from React
Net Ninja
30 GraphQL Tutorial #27 - Rendering Data in a Component
GraphQL Tutorial #27 - Rendering Data in a Component
Net Ninja
31 GraphQL Tutorial #28 - Add Book Component
GraphQL Tutorial #28 - Add Book Component
Net Ninja
32 GraphQL Tutorial #29 - External Query File
GraphQL Tutorial #29 - External Query File
Net Ninja
33 GraphQL Tutorial #30 - Updating Component State
GraphQL Tutorial #30 - Updating Component State
Net Ninja
34 GraphQL Tutorial #31 - Composing Queries
GraphQL Tutorial #31 - Composing Queries
Net Ninja
35 GraphQL Tutorial #32 - query variables
GraphQL Tutorial #32 - query variables
Net Ninja
36 GraphQL Tutorial #33 - Re-fetching Queries
GraphQL Tutorial #33 - Re-fetching Queries
Net Ninja
37 GraphQL Tutorial #34 - Book Details Component
GraphQL Tutorial #34 - Book Details Component
Net Ninja
38 GraphQL Tutorial #36 - Styling the App
GraphQL Tutorial #36 - Styling the App
Net Ninja
39 GraphQL Tutorial #35 - Making a Single Query
GraphQL Tutorial #35 - Making a Single Query
Net Ninja
40 Build Apps with Vue & Firebase - Udemy Course
Build Apps with Vue & Firebase - Udemy Course
Net Ninja
41 Updated Vue & Firebase Course (Udemy)
Updated Vue & Firebase Course (Udemy)
Net Ninja
42 Vue & Firebase Real-time Chat (Preview) #1 - Intro
Vue & Firebase Real-time Chat (Preview) #1 - Intro
Net Ninja
43 Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Vue & Firebase Real-time Chat (Preview) #2 - Project Structure
Net Ninja
44 Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Vue & Firebase Real-time Chat (Preview) #3 - Firestore Setup
Net Ninja
45 Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Vue & Firebase Real-time Chat (Preview) #4 - Welcome Screen
Net Ninja
46 Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Vue & Firebase Real-time Chat (Preview) #5 - Props in Routes
Net Ninja
47 Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Vue & Firebase Real-time Chat (Preview) #6 - Route Guards
Net Ninja
48 Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Vue & Firebase Real-time Chat (Preview) #7 - Chat Window
Net Ninja
49 Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Vue & Firebase Real-time Chat (Preview) #8 - New Message Component
Net Ninja
50 Object Oriented JavaScript Tutorial #1 - Introduction
Object Oriented JavaScript Tutorial #1 - Introduction
Net Ninja
51 Object Oriented JavaScript Tutorial #2 - Object Literals
Object Oriented JavaScript Tutorial #2 - Object Literals
Net Ninja
52 Object Oriented JavaScript Tutorial #3 - Updating Properties
Object Oriented JavaScript Tutorial #3 - Updating Properties
Net Ninja
53 Object Oriented JavaScript Tutorial #4 - Classes
Object Oriented JavaScript Tutorial #4 - Classes
Net Ninja
54 Object Oriented JavaScript Tutorial #5  - Class Constructors
Object Oriented JavaScript Tutorial #5 - Class Constructors
Net Ninja
55 Object Oriented JavaScript Tutorial #6 - Class Methods
Object Oriented JavaScript Tutorial #6 - Class Methods
Net Ninja
56 Object Oriented JavaScript Tutorial #7 - Method Chaining
Object Oriented JavaScript Tutorial #7 - Method Chaining
Net Ninja
57 Object Oriented JavaScript Tutorial #8 - Class Inheritance
Object Oriented JavaScript Tutorial #8 - Class Inheritance
Net Ninja
58 Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Object Oriented JavaScript Tutorial #9 - Constructors (under the hood)
Net Ninja
59 Object Oriented JavaScript Tutorial #10 - Prototype
Object Oriented JavaScript Tutorial #10 - Prototype
Net Ninja
60 Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Object Oriented JavaScript Tutorial #11 - Prototype Inheritance
Net Ninja

Related Reads

📰
Meet Crumbie
Learn about Crumbie, a UX design concept, and its relevance to designers
Medium · UX Design
📰
Build an Accessible Optimistic Toggle That Can Admit It Was Wrong
Learn to build an accessible optimistic toggle that maintains alignment between keyboard focus, announced state, and server truth
Dev.to · babycat
📰
AI Won’t Replace Designers. Average Designers Should Be Worried Instead.
AI won't replace talented designers, but average designers should be worried about being replaced by AI, highlighting the need for unique skills and creativity
Medium · AI
📰
AI Won’t Replace Designers. Average Designers Should Be Worried Instead.
Average designers should focus on developing unique skills to stand out, as AI won't replace talented designers, but mediocrity might
Medium · UX Design
Up next
How to Build a Doctor Appointment Booking Website in WordPress | CLDoctor Theme
Quick Tips - Web Desiign & Ai Tools
Watch →