React Skeleton Screen Tutorial #3 - Base Skeleton Component

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

Key Takeaways

Develops a base skeleton component in React using JavaScript and CSS

Full Transcript

all right then gang so now we've successfully grabbed all of that data what i'd like to do instead of showing this loading text is work on the skeleton components so we can show those different skeleton layouts instead now the way we're going to approach this is to make a reusable base skeleton element component and that can be configurable to be different shapes so that we could have for example a skeleton text to be a strip going across or a skeleton title which might be a bit thicker or a skeleton avatar which could be a circle or a skeleton thumbnail which could be a square where a thumbnail might go all of that kind of stuff i just want to create one single customizable component which could be any of those and then we could reuse that to create different skeleton layouts to create maybe a skeleton articulator or a skeleton profile and different things all right so it makes it really reusable so then what i'm going to do is come over to the source folder and create a new folder called skeletons this is where all of our skeleton components and skeleton css is going to live and then inside there i'm going to create a new file called skeleton elements dot js so this is going to be the base skeleton element component that will be reusable and customizable so all i'm going to do is first of all import react from react and then after that i'm going to create a functional component so i'm going to say const skeleton elements is equal to a function and inside here will return a simple template and that template is just going to be a div with a class name equal to something now we'll come back to that class name in a second and then close off the div now at the end i just want to export this so export default and then skeleton element okay so now this class right here will ultimately be the thing that determines what shape this skeleton element is going to be and we're going to be able to pass that in so we'll get it on the props that come in from when we use this so if i was to use this skeleton element inside the articles component i could pass in a prop to say what type of skeleton element i want is it going to be a title or some basic text or an avatar or a thumbnail etc so we can destructure that up here from props and we'll call it type and spell it correctly and then what we want to do down here is make up these classes based on the type that we pass in so i want this div to have a class of skeleton regardless so i could say down here skeleton but i also want it to have the class which we're passing right here now that could be avatar it could be thumbnail it could be text it could be title etc so what i'm going to do is create a constant right here called classes right and set that equal to a template string meaning we can output a variable inside here now i want one of the classes to be skeleton which we added down here right and the other class to be the type so now i can just output the type in the curly braces like so and now we have a classes string and instead of outputting this right here i can just say that this is equal to classes right so whatever this is here so now what i'd like to do is style the different types that we can pass in so i'm going to come to the skeletons folder and create a new file and call this skeleton.css and i just want to apply some basic styles to the different types of skeletons we can have so the different classes and also this class right here i want to style that as well so let me come over here and do a comment that says basic styles and then below that i'm going to grab the skeleton clasp first of all which is going to be present on every div right here and we're going to say for that we want a background of ddd which is a kind of light gray color and then the margin is going to be 10 pixels top and bottom and 0 left and right so if we have multiple different skeleton elements there's going to be a vertical margin between them the border radius is going to be about four pixels just to soften up the corners and then i want to also style the different types so i'm gonna say dot skeleton again and then if it has a class of text as well then we're gonna style it in a particular way so say for example i pass in the type to be text then it's going to get a class of text as well as skeleton so we're styling that up now and in this case we want the width to be 100 of whatever parent we pass this component into and the height is going to be 12 pixels so this means it's going to be a thin strip which is going to be colored light gray and that will represent a line of text right now i'm going to do another one which will be skeleton dot title so this is another type we could pass through this is going to be similar to text but it's not going to be as long the width of this will be 50 of whatever parent element we place in and then the height is going to be a little larger so it's a bit chunkier much as title text sometimes is it's going to be 20 pixels instead of 12 and then we're going to give it a slightly larger margin bottom so margin bottom is going to be 15 pixels instead of the default 10. all right okay so there are two different types we can pass in let's also do another one so skeleton dots avatar and this is going to be like a circle you know when we get a profile sometimes we get like a little avatar which is a circle image that's going to represent this so width is going to be 100 pixels the height is going to be 100 pixels and then the border radius is going to be 50 and with a border radius of 50 a square is turned into a circle right it rounds it at each corner such that it becomes a circle so that's the avatar and then finally i'll just do a square so skeleton dot thumbnail we'll call this and that will have a width of 100 pixels and a height of 100 pixels so the same as the avatar but square so we don't have to border radius okay so they're the different types that we can pass in which become classes inside this skeleton element component right so what i'm going to do is try out putting this skeleton component inside the articles over here and we'll pass in a different type each time so we can see what they look like so i'm not going to output them here or in the loading screen for now what i'm going to do is just output them at the top underneath where it says articles so we want the skeleton elements and if we hit enter it's going to import that for us at the top and then we want to pass through a type so let's say the type is equal to title for example i remember these types match up the classes we just created so if i save this and preview then refresh over here we don't see that so let's see what's going on and i think the reason this isn't working is because i've not actually imported this css anywhere we need to import this css into this file right here so that the skeleton element can use it so we're going to import and we want dot forward slash for the same directory skeleton dot css like so okay so now this should work yep now we see it so that is a title skeleton element now let's just go back to articles and duplicate this a few times and pass in the different types that we have so we had title we also had text we also had thumbnail and we also had avatar so if i save this we should see all types okay there's the title top the text which is longer and thinner the thumbnail which is kind of like a square and then the avatar which is a circle awesome now that looks pretty pants as is there and i wouldn't just place this as they are down here while this loads because it doesn't really look like anything except a random bunch of shapes so what we're going to do is create more components which uses these base components right here to create different skeleton layouts and then basically we can create as many different types of skeleton layouts using these base skeleton components or this one single base skeleton components with different types right here so we're going to start that process in the next video by creating a skeleton article component

Original Description

In this React Skelton tutorial we'll create the base skeleton component that will provide the building blocks of our skeleton layouts. 🐱‍👤🐱‍👤 JOIN THE GANG - https://www.youtube.com/channel/UCW5YeuERMmlnqo4oq8vwUpg/join ---------------------------------------- 🐱‍💻 🐱‍💻 My Udemy Courses: + Modern JavaScript - https://www.thenetninja.co.uk/udemy/modern-javascript + Vue JS & Firebase - http://www.thenetninja.co.uk/udemy/vue-and-firebase + D3.js & Firebase - https://www.thenetninja.co.uk/udemy/d3-and-firebase 🐱‍💻 🐱‍💻 Course Files: + https://github.com/iamshaunjp/react-skeleton-screens 🐱‍💻 🐱‍💻 Helpful Links: + Full React Course - https://www.youtube.com/watch?v=OxIDLw0M-m0&list=PL4cUxeGkcC9ij8CfkAY2RAGb-tmkNwQHG + React Context & Hooks course - https://www.youtube.com/watch?v=6RhOzQciVwI&list=PL4cUxeGkcC9hNokByJilPg5g9m2APUePI + Async JavaScript Course - https://www.youtube.com/watch?v=ZcQyJ-gxke0&list=PL4cUxeGkcC9jx2TTZk3IGWKSbtugYdrlu + Modern JavaScript Course (free version) - https://www.youtube.com/watch?v=iWOYAxlnaww&list=PL4cUxeGkcC9haFPT7J25Q9GRB_ZkFrQAc + Get VS Code - https://code.visualstudio.com/ 🐱‍💻 🐱‍💻 Social Links: Facebook - https://www.facebook.com/thenetninjauk Twitter - https://twitter.com/thenetninjauk Instagram - https://www.instagram.com/thenetninja/
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 AI Lessons

Had my Frontend Developer interview with Capgemini (Application Developer) today, and I wanted to…
Prepare for a frontend developer interview with Capgemini by reviewing JavaScript fundamentals and practicing common interview questions
Medium · JavaScript
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with 10 essential tools for modern web app development
Medium · Programming
10 Frontend Developer Tools to Boost Productivity in 2026
Boost frontend productivity with top 10 developer tools in 2026
Medium · JavaScript
The US Frontend Engineer Market in 2026: A Data-Driven Reality Check (and the Bias That Stops Us Seeing It)
US frontend engineer hiring demand peaked in 2022 and remains flat-depressed in 2026, contrary to common assumptions
Dev.to AI
Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →