Solid JS Tutorial #5 - Using Props
Key Takeaways
The video demonstrates how to use props in Solid JS to pass custom properties and values into components, and how to use the children property to pass custom content and templates into components.
Full Transcript
okay then my friends so Sully JS much like other front-end Frameworks make use of something called props and props are basically just a way to pass custom Properties or values into other components for example I might want to pass some custom values into one of these card components so that we can use those values inside the component and every time we use a card component we can pass different values into it so they might be different each time around and the way we do this is almost identical to how we do it in react we would just pass in a prop name and we'd set it equal to some kind of value now that value could be a string or some other type like a number or a Boolean or an object Etc now if it's a string then we can just pass that string as a value in quotes but if it's a different type then we need to put the value inside curly braces Instead This Is How We output some kind of dynamic value or code in solid so if I passed in a number that would be in curly braces and the same would be true if I Boolean or an object or anything else that's not a string for now what I want to do is pass in a prop into this component called title which is a string and the value of that could be ninja t for t-shirt now we can accept that prop in the component by just using the props argument inside the component function that we automatically get now if you're coming from react this is where it's a little bit different because in react we'd probably just destructure values from the props object but inside we shouldn't do that because it could break its reactivity so make sure you don't destructure the props just keep it as props and then we can access the title prop on this props object in the template if we wanted to so now if we go to the browser we can see that title right here the value ninja T it's not in the other cards because we didn't pass it through but if we wanted to we could go back and we could add a title to each one of these and they could be different each time around so for example this could be ninja totes bag and then down here we could do another title which would be ninja hoodie like so so now if we save this we can see Ninja tote bag and ninja hoodie so this is a way to pass different data into the components okay so that's the title now also what I'd like to do is pass in some options for the card itself for example we have all these different styles right here now what I'd like to do is maybe have some props in these cards whereby we could say do we want a shadow or do we want round corners or something like that so that's what we're going to do now and what I'll do is I'll just comment out one of them because we only need two for this so we'll keep the titles but also I want a couple more props so those two props are going to be rounded and we're going to set that equal to true and then also flat and we're going to set that equal to false so this rounded one is going to be whether we want the card to have rounded Corners the Border radius if you like and this one right here is telling us whether we want the card to be flat or not and that refers to the Box Shadow so in this case we do want rounded Corners because this is true and we don't want it to be flat so we do want that box shadow and we're going to do something similar for this card down here but we'll have different values so rounded is going to be false and flat is going to be true okay then so let me save this now we need to use these different props over here in the cad and remember they're attached to this props object now the way we're going to work with this is by using some conditional classes because we want to conditionally output classes on this div dependent on the values of those props and the way we do that in solid is by using the class list attribute or rather property so we can say class list and we set that equal to something Dynamic and in fact it's an object and the way this works is we have properties and values now the properties are the class names we want to conditionally apply to it and the values of those are true or false if the value is true we apply that class if the value is false we don't so the classes or the property names we want here are going to be rounded MD and that's for this one right here rounded and also Shadow Ms because oops I think that should be Shadow MD alright not Ms so Shadow MD and that's for this prop right here flat so then we'll say in quotes rounded hyphen MD and the value of that is going to be props dots rounded and that's going to be true or false this thing right here yeah and then the next one is going to be shadow hyphen MD and that's going to be exclamation props dot flat and it's exclamation Because it's the opposite of this right so if flat is false then we do want the class so we reverse it okay and now we can take them off here because we don't want to apply them to every card only to cards based on these prop values all right so let's give this a whirl we should see that the first card is rounded and also has the Shadow the second one shouldn't be rounded and shouldn't have the shadow so let's check it out all right yeah rounded at the corners with a shadow not rounded and doesn't have a shadow awesome Okay cool so now we've seen how we can pass these props into components but using props like this does have its limitations for example what if I wanted to have lots of different content inside each of these cards so not just a different title but also maybe a different paragraph of text maybe a different image even a totally different template well then props wouldn't really be able to efficiently solve that because we don't want to be making props for loads of different content templates images all that kind of jazz that would get really messy right but what we can do is pass in custom content and templates using the children property on the props object so let me show you how that works so I'm first gonna get rid of the title prop in each of these components we don't need that anymore and you'll see why in a second and if we go to the component we can take out the props.title as well we don't need that and in fact I'm going to cut all of this so now we just have a div with no content inside the div just a div with classes now if we go back to app.jsx what I'd like to do is make this so now it's no longer self-closing so we have the closing tag as well and now I want to make it so we can pass a custom template and custom content into this card by nesting it inside these tags so I could paste in this content right here and I could put in a title for example Ninja t black like so now let me just scoot that in I'm going to copy that again I'm going to do the same thing down here your closing tag and then inside that I'm going to Output this template again we'll change the title and that's going to be ninja T and we'll do white for this we'll change the order of the content as well so let's grab that button and let's put it up here instead and maybe it will put a price at the bottom so only 10. pounds or something like that and we'll change the button text to view we'll do that on both so now we have different content going into each of these cards and we want to Output that content inside this div right here and the way we do that is just by using curly braces because we're outputting something Dynamic and then saying props dot children and this children property on the props represents whatever children we pass into the card and these are all the children right here the children elements if you like so it takes all of this content and it outputs it right here where we output props.children and now we should see that content this and this inside the cards so let's take a look and we do awesome so that's a nice way to pass custom templates and content into other components
Original Description
🚀🥷🏼Get early access to this entire course now on Net Ninja Pro:
https://netninja.dev/p/solid-js-tutorial
📂🥷🏼 Access the course files on GitHub:
https://github.com/iamshaunjp/solid-js-tutorial
💻🥷🏼 Modern JavaScript Tutorial:
On Net Ninja Pro - https://netninja.dev/p/modern-javascript-from-novice-to-ninja
On YouTube - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc
💻🥷🏼 Tailwind CSS Tutorial:
On Net Ninja Pro - https://netninja.dev/p/tailwind-css-tutorial
On YouTube - https://www.youtube.com/watch?v=bxmDnn7lrnk&list=PL4cUxeGkcC9gpXORlEHjc5bgnIi5HEGhw
🔗🥷🏼 Solid Playground - https://playground.solidjs.com/
🔗🥷🏼 Solid JS docs (getting started) - https://www.solidjs.com/guides/getting-started
🔗🥷🏼 Solid Router docs - https://github.com/solidjs/solid-router#getting-started
🔗🥷🏼 VS Code - https://code.visualstudio.com/
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
Related Reads
📰
📰
📰
📰
Creta Frontend Interview Experience | Remote Role
Medium · JavaScript
The Case of the Ghost Tooltip
Medium · JavaScript
How I made a scroll-scrubbed video portfolio fast (Next.js 15 + GSAP + canvas)
Dev.to · Pratham Sharma
5 Reasons HTML Is About to Change Frontend Development
Medium · Programming
🎓
Tutor Explanation
DeepCamp AI