Solid JS Tutorial #5 - Using Props

Net Ninja · Beginner ·🌐 Frontend Engineering ·3y ago

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 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

This video teaches how to use props and the children property in Solid JS to build reusable and customizable UI components. By the end of this lesson, you will be able to pass custom properties and values into components, and use the children property to pass custom content and templates into components.

Key Takeaways
  1. Create a new Solid JS component
  2. Define props for the component
  3. Pass props into the component
  4. Use the class list attribute to apply conditional classes
  5. Use the children property to pass custom content and templates into the component
💡 The children property in Solid JS allows you to pass custom content and templates into components, making it easy to build reusable and customizable UI components.

Related Reads

Up next
How to Speed Up Your WordPress Website with WP Rocket ⚡Tutorial 2026
Matt Tutorials
Watch →