Complete React Tutorial (& Redux) #4 - React Components

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

Key Takeaways

Creates and uses React components to control parts of a website

Full Transcript

all right then gang so in this video we're going to create our very first react component now I've already said that react components are the lifeblood of react and we use them to take control of different things or different elements inside our websit such as the Navar search bar a contact form those kind of things in this video we'll create a react component to take control of this thing right here this div with an ID of app so we need to create that comp component in JavaScript so first of all let's do our script tags now how do we create a react component well there's a few different ways in this video we're going to use JavaScript classes a class-based component but I will show you other ways as we go forward through the course as well so to create a class-based component first of all we need the class keyword and by the way if you want to learn more about JavaScript classes I've got an objectoriented JavaScript tutorial Series right on this channel I'm going to leave that link down below but essentially classes are a way for us to blueprint objects in JavaScript but anyway we're going to create this class and give it a name called app you can call this whatever you want I'm calling it app since we're going to use it to control this app ID right here and this class is going to extends react. component so we have access to this react object right here because we added this script to react and we're using this this thing on the react object component to inherit all the base functionality of a component into this class so now this class right here is going to represent our component now class-based components must have inside at least one function and that function is called render so the render method right there that is what is responsible for rendering our template to whatever element we want to render to so what we're going to do inside this method is return a value and this value is going to be our jsx template now typically this return value is going to go inside parenthesis because a lot of the time our return value is going to go over one line all right then so remember jsx is a way for us to write HTML code inside JavaScript because that's what we're doing here we're inside a JavaScript block so we want to write some HTML code here so we'll use jsx to do that and it looks very much like HTML so let's create our jsx template we'll create a div first of all and then inside that div we'll just do an H1 so in there H1 and we'll just say hey ninjas all right so that there is our jsx template and it looks identical to HTML at the minute now there's a couple of limitations when using jsx that you should be aware of of the first one is that we should always generally return one root element at the top so div div like so we can Nest as many elements inside that root element but there should generally always be one root element at the top now if we did something like this at the moment and did three root elements this is not going to work right because there's three root elements we need to have just one root element there are ways around this but I'm not going to delve into those right now so the other limitation is that we can't use the keyword class to add a CSS class to this class in JavaScript is a reserved keyword we use it to create classes so we have to instead say class name equals to whatever so I'll call this app hyphen content so there's a couple of limitations that you should remember when working with jsx one roote element and we use class name instead of class for CSS classes all right then so we have our component right here but at the minute it's not really doing anything we want to render this component to this thing right here and nothing inside this JavaScript is saying to react do that yet now remember we added this second script up here called react Dom this is the glue layer that takes our components and can render them to the Dom so the way we take this component right now and render it to the Dom is by saying react Dom we get access access to that from here and we use a method called render and inside we pass in two parameters the first parameter is which class or which component we want to render to the Dom we want to render this one right here but we don't just say app like so we add it as a tag bit like jsx so we take the component name and put it inside a tack which is self closing the second parameter is going to be where we want to render it to to the D so we want to render it right here so we can say document. getet element by ID and then pass in app and that's going to tell react Dom to render app this component to this thing right here so let's give this a world and I'm going to right click over here and go to open with live server now I can do that because I've got a package installed called live server so if you you want to do the same thing you can install that and that means that on any um HTML page like this you can right click and go to open with live server that's going to open it in a browser for you well it should do anyway open with live server okay so at the minute we can see nothing is in here it's not taking this and it's not rendering it to the do and we should actually see an error in the console if we open this up and it says syntax error unexpected token angle bracket and that's referring to this jsx right here so it's not recognizing this stuff the jsx and that's because jsx out of the box is not supported in browsers so we need a way to take this code and transpile it into something that is supported in browsers now that is not complex to do at all we just have to use a tool called B so go to Babel js. then go to setup then go to in the browser and we're going to grab this thing right here this script tag so Babel is going to transpile our code right here into something that a browser does understand and we need to do one more thing we need to grab this stuff you see where we have a script tag we have to say type equals text SLB I'm just going to zoom in so you can see that and I'm going to grab that stuff and paste it over here so this should fingers crossed now work so if we go over to the browser now we should see hey ninjas rendered to the D and if we inspect this element then we can see that right here inside the div within ID of appap we're rendering our component so it doesn't get rid of the root div right here doesn't get rid of this it just nests our component inside that div make sense okay so this is all well and good but what is the point I mean at the end of the day I could have just grabbed this and just pasted it inside here hardcoded that right instead of creating this component then rendering it to the Dom now the idea of react is that we can actually output Dynamic content if it was just always static content like this it would be useless but we can output Dynamic content inside our components now how do we do that first of all let's do another tag not all that stuff A P tag and inside the P tag what we'll do is a couple of curly braces like this so this means that we can output Dynamic JavaScript content inside these curly braces so if I wanted to now I could do a bit of JavaScript like take the math object and say math. random this is just basic JavaScript then I'm going to multiply that by 10 so I want a random number which gets me a random number between 0 and one like. 764 whatever and I'm going to times that by 10 and then I'm outputting this dynamically to the template I couldn't just do this without the curly braces because that would just render this as a string and you'll see that if I save it view that in a browser we just get this back but if I output this inside curly braces then we're saying hey this is actually JavaScript uh Dynamic stuff we want to run here and I want you to render the result of this to the Dom so if I save this now then we should see that random number on the screen let me just zoom in you see that so I can refresh and get a different number each time because at the end of the day this is just rerunning every time we run the file okay so that's the power of react components and it's not always going to be random numbers like this again that would be useless but it could be data from a database or UI state is something open or is something closed and we're going to see all that as we get further into the course but for now that my friends is how to create a basic component in react

Original Description

Hey gang, in this React tutorial I'll be looking at Components and how we can create & use them to take control of a part of a website. Object Oriented JS: https://www.youtube.com/watch?v=4l3bTDlT6ZI&list=PL4cUxeGkcC9i5yvDkJgt60vNVWffpblB7 ---------------------------------------- 🐱‍💻 Course Links: + VS Code editor - https://code.visualstudio.com/ + GitHub repository (course files) - https://github.com/iamshaunjp/react-redux-complete-playlist/tree/lesson-4 + 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 AI Lessons

Up next
The masks we wear | Zora Krstić | TEDxLuxembourgCity
TEDx Talks
Watch →