Complete React Tutorial (& Redux) #28 - Higher Order Components

Net Ninja · Beginner ·🌐 Frontend Engineering ·7y ago
Skills: React90%

Key Takeaways

Covers Higher Order Components in React from scratch

Full Transcript

or rather than higher-order components we've already seen one of them in the last video called with router but what exactly are they well essentially they're functions which wrap other components and give them superpowers or rather extra features so we'll have our component for example the navbar is we passing into a higher-order component and that higher-order components adds functionality to this component so that it comes out supercharged with different features so in the previous video we saw that the higher-order component was called with router and what that did was add props to the props object in the navbar component so that we can have information about the router inside this component so I'm not going to go into too much detail about higher-order components but I thought in this video we would make a simple custom one of our own so you can see the basics of how they work and if you want to explore further you can do all right then so we're going to create a simple higher-order component so let's create a new folder and I'm going to call this hoc for higher-order component and inside that and your file called rainbow Jas so the idea here is that we're going to create a higher-order component that is going to randomize the text color inside another component right so first of all we need to import react from react now then the higher-order component is essentially just going to be a function and I'll call this rainbow and it's a function like that now inside here as a parameter we can take the wrapped component so you know before when we use the with router higher-order component we wrapped this component so when we wrap something like that we receive the wrapped component right here okay so if we were to use this rainbow on another one of these components for example this then we'd receive the about component as a parameter right here that would be the wrapped component and we're going to return that later on because essentially if we want to return a supercharged component so before we do that I want to just get a random color or a class which represents a random color so that it can be styled that way so what I'll do is create a constant and this is called colors and I'll set that equal to an array with a few different colors in so I'll do red I'll do pink I'll do orange I'll do blue running out of colors for other green and then I'll also do yellow alright so we have our array now right here and I want to randomize which one of these that we choose so to do that what I'm going to do is create a new contest called the random color and I'll set that equal to colors like so and then array notation so we want this right here to be a random integer between 0 & 1 2 3 4 5 0 & 5 because the six elements right so to do that what we can say is math dot random this gets us a random number between 0 & 1 and then we can times it by 5 to get as a random number between 0 & 5 and then what we want to do because this is going to have decimals we want to floor that so it's literally just going to be 0 1 2 3 4 or 5 so we can say math but floor to do this and then passing brackets the random number so now this is going to be a random integer between 0 & 5 so that we can select one of these random colors okay so we have that now the random colors stored there now when we apply a materialized CSS class to change the color it's always the color name and then - text so something like this red - text or you know blue - text so we want to make the class name based on this random color so we'll say Const and then class name is equal to the random color plus - text working catenate in the random color that we have with - text to get that CSS class now remember we have to return the wrapped component at the end of the day supercharged so we'll do that now so we'll return a function and this function takes in the props which would have been passed into this component right so if this component received any props then they would be passed in here as well so this function needs to return some JSX and that JSX could just be the wrapped component or we can surround that wrap component with other JSX so I'll do that I'll do a div first of all and then this is going to have a class name equal to something dynamic so curly braces class name which is this thing right here the random color class okay so we're applying this class to this div which is then going to surround the wrapped component so let's pass that wrapped component in here like so okay so hopefully when we return this now it's going to return this component but there's going to be an extra div around this stuff and that div is going to have a class name of red text or pink text etc so hopefully it will color the text of that component makes sense all right then so what about these props well imagine this received props right here well in order to access them and use them inside this component and take them in here then we need to pass them into this component right here now the way we do that is just by using curly braces and then the spread operator props and that is going to pass the props into this component again okay so then we need to export this higher-order component so exports default rainbow-like so save that and now we can import it into another file so I'll import it here we'll say import rainbow from and it's going to be from dot dot forward slash to come out of the components folder then we want to go into the hoc folder then we want the rainbow file all right so now we have that imported we can go ahead and use it down here rainbow we surround the component we wrap it in that function save it and then if we check this out in the browser and we can see this is a random text now I'm going to refresh random refresh random okay so there we go we've created now a higher-order component which is giving the text a random color each time and all there is to it is to create a function right here we've taken the wrapped component we can do something in here we return the original component and extra stuff if we want to we exported and then we use it on the component itself now this is just scratching the surface when it comes to higher order components I would advise you to read more about them if you want to and there's more about them on the official documentation so I'll leave the link to those down below

Original Description

Hey gang, in this React tutorial I'll be talking a little bit about what Higher Order Components are, and how we can create one from scratch. ---------------------------------------- 🐱‍💻 Course Links: + VS Code editor - https://code.visualstudio.com/ + GitHub repository (course files) - https://github.com/iamshaunjp/react-redux-complete-playlist + React CDN - https://reactjs.org/docs/cdn-links.html ---------------------------------------- 🤑 Donate @ https://www.paypal.me/thenetninja 🎓Find me on Udemy @ https://www.udemy.com/user/47fd83f6-5e4a-4e87-a0f0-519ac51f91b6/
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

📰
Next.js vs Remix vs SvelteKit: Which Framework Should You Learn?
Learn how to choose between Next.js, Remix, and SvelteKit for your next project and why it matters for your career as a developer
Dev.to · Etrit Neziri
📰
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
Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →