Astro Crash Course #7 - Reusable Components
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
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: UI Design
View skill →Related Reads
🎓
Tutor Explanation
DeepCamp AI