02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

Passing Components and Properties in React

Passing Components and Properties in React

In this tutorial, we will explore how to pass components and properties (props) in React. Props are one of the key features of React that allow you to create dynamic and reusable components by passing data between them. This content was originally created in 2016, and we have included some updates to reflect the current best practices in React.

What are Props?

Props (short for properties) are read-only inputs that allow you to pass data from a parent component to a child component. They are essential for building interactive and dynamic React applications.

Props make it easy to create reusable components. Instead of hardcoding data into a component, you can pass the data via props, making your component more flexible.

Example of Using Props

Here’s an example of a simple component called Greeting that accepts a name prop:

import React from 'react';

function Greeting(props) {
  return <h1>Hello, {props.name}!</h1>;
}

export default Greeting;

You can use the Greeting component and pass different values for name like so:

<Greeting name="Alice" />
<Greeting name="Bob" />

The output will vary based on the prop passed, such as "Hello, Alice!" or "Hello, Bob!".

Critical Update (2024): Today, it's common to destructure props directly in the function parameters for cleaner code:

function Greeting({ name }) {
  return <h1>Hello, {name}!</h1>;
}

Passing Components as Props

In React, you can even pass components as props, which allows you to build more flexible and composable UIs. Here's an example where we pass a Welcome component as a prop to another component.

Example of Passing Components as Props

import React from 'react';

function Welcome() {
  return <h2>Welcome to the tutorial!</h2>;
}

function Page({ header }) {
  return (
    <div>
      {header}
      <p>This is the main content of the page.</p>
    </div>
  );
}

export default function App() {
  return (
    <Page header={<Welcome />} />
  );
}

In this example, the Page component accepts a header prop, which can be any valid React element. We pass the Welcome component as the header prop when rendering the Page component.

Note: Passing components as props allows for highly customizable and flexible layouts, helping to build composable UIs.

Using Default Props

Sometimes you want to provide a default value for props if they are not explicitly provided by the parent component. React allows you to set default props for this purpose.

Example of Default Props

function Greeting({ name = 'Guest' }) {
  return <h1>Hello, {name}!</h1>;
}

In this example, if no name prop is provided, the component will default to "Guest".

Critical Update (2024): Default props for functional components can be set directly within the function parameter as shown above, making the code more concise.

Conclusion

In this tutorial, we covered the basics of passing components and properties in React. You learned how props can be used to pass data between components, making your React application more dynamic and interactive. We also explored passing components as props and how to use default props.

As React has evolved since 2016, destructuring props and using functional components with hooks are now considered best practices for writing cleaner and more maintainable code.

For more tutorials and to continue your learning journey, visit the full course at 02geek.

Video Transcript

00:00In the last lecture, we created our first
00:03complete real component.
00:05But our problem with the component that we created is
00:07that it did not have really any point in a
00:10real world scenario.
00:11We would want the component to have real properties such
00:14as maybe a title that changes.
00:16And we would want it to have content,
00:18which in our case we're gonna call it children.
00:20What we're gonna do in this lecture, we're gonna learn about
00:22how do you actually add those dynamic properties
00:25and how do you add children components
00:27that are not baked into our component as we do this.
00:31One thing that is really important to note is
00:33that the properties that we're adding in are stateless.
00:37What does stateless mean?
00:39It means that once we put it into our component,
00:41we assume that this is it.
00:42Our component is not going to change at least what from
00:47how it comes from React.
00:48React is not gonna need to re-render it.
00:51We're gonna learn in the next lecture about
00:52what do we do in a case where the property that we want
00:55to add, we expect it to change
00:57and will want to re-render with React.
01:00Okay, let's continue our journey
01:02to actually making this more dynamic
01:04and really usable as a component.
01:06And the first thing that we wanna do is actually change this
01:08title to reflect whatever it actually is.
01:11So we wanna make sure that we could send this property
01:13and not have it hard coded inside
01:15of our component to do that.
01:17What we're gonna do in kind of similarly to,
01:20as we were talking about previously,
01:22that you could add properties inside of this note
01:25or this element that is created.
01:26So I could send you a property such as title
01:29and this title could be login.
01:32And what happens is that when the element is created,
01:35it's sending in those variables that we've added
01:37as properties to the element.
01:39So let's go ahead and create that element very, very simply,
01:42just going into our rendering function here,
01:44and let me just break a few lines here
01:46and let me just, for the sake of making things easier
01:49to look at, I'm gonna go ahead here and set my layout
01:52and set a title
01:53and I'll set my title to be, let's say login.
01:56'cause that would be the first item
01:57that we wanna actually present.
01:59And this is sitting outside of the actual component of login
02:03that we're gonna be creating shortly.
02:05Okay? So now inside of our layout itself, I wanna show you
02:08how do you put inside dynamic content.
02:11Now the way you do that, I'm gonna delete that title
02:13and instead of that I'm gonna open and close a curly bracket
02:17and then approach a variable that belongs to this class
02:20or to this instance more accurately.
02:22To do that, I'm gonna go to this props,
02:25which are the properties that are sent to this element,
02:27and I'm gonna go to to that element name,
02:30which in this case is title.
02:32If I go ahead here and I click here on refresh,
02:34we should see now that our title is gonna be passed through
02:37and that title should be now login.
02:39Now with that said, one thing that I want you to very,
02:42very close attention to is that you never change properties.
02:46You'll never go inside of your render and go to this props
02:50and then go and changes to be title equals something else.
02:56Never do that. It's a big no-no,
02:58Absolutely do not do that.
02:59Even if it worked,
03:01which I don't know if you're gonna get an
03:02error or not, here you go.
03:03You're getting an error type title is read only.
03:06You'll never do that. That's not something you wanna do
03:09beyond that, not only that,
03:11you can't, you don't want to do it.
03:14The idea behind of these instances a property is something
03:16that is consistent and doesn't change,
03:19although it's dynamic in the nature
03:20that it's not hardcoded inside of your layout.
03:23Once it's inside, we assume that this will never change
03:26and you should assume that as well.
03:28We're gonna take a look at a later lecture.
03:30How do you put content that you know might change
03:32or will change and how do you deal
03:34with that type of content?
03:36But properties are assumed
03:38to be something that does not change.
03:39So anytime you send in something as a property,
03:42you're really saying, I'm sending this in as something
03:45that isn't changing or isn't presumed to change.
03:48Okay, so with that said, let's go ahead
03:50and create now two new classes, one for login
03:54and one for register.
03:56So I'm gonna go ahead here and after here,
03:57after this layout, I'll just go ahead here
03:59and create a new login.
04:02And our login is gonna basically be the same thing.
04:04We're gonna create a class, we're gonna use the render.
04:07And let me just close that and let me just close that
04:10and let me just close that
04:12and let me see set here the return.
04:15And let's go ahead into our login logic itself
04:17and just copy the login information here.
04:20I'm just gonna go ahead here and copy this whole form
04:22and let's just make sure that everything here is
04:24okay pre first of all.
04:26So first thing that I want to do is I don't really need
04:28this, I just copied all these stuff from from my website.
04:32So I'm just gonna go ahead here
04:33and just delete the things that are not really needed.
04:36Um, that's our input, that's fine, that's uh, hidden.
04:40We don't need the hidden one for our sake
04:42and purposes right now and we are fine with all the rest.
04:47Let's just double check that we don't have any
04:49fours and no classes.
04:51So I could see, or here we have a four,
04:52so I'm gonna change it to html.
04:55HTML four. Do we have any classes?
04:58I don't see any, but if there are any then we'll get an
05:01error at some point when we're running the application.
05:03Oh, I see a couple of them. So let's fix
05:04them already inside of our app.
05:05And I'm gonna go ahead here
05:08and let me just find those classes
05:10and change it to class name
05:13and let me just copy that class name.
05:16Um, that all looks to me more or less.
05:19Okay, so lemme go ahead and save this
05:21and lemme show you how do I actually connect it.
05:24So let's go ahead and in our layout itself,
05:26there's two steps that I wanna do.
05:28One is I'm gonna change my layout from being just a self
05:31enclosed element to actually be a full layout component.
05:35And what I wanna do is I wanna place inside of this layout,
05:38I wanna place this new login that I've just created.
05:40So lemme go ahead here and just send in the login
05:42as the child of that layout.
05:45Now this isn't enough because if I do this
05:47and I go back into my application
05:48and I click on refresh, it's actually gonna show it.
05:51And more than that, we still have your small error.
05:54So we have here in our input, we didn't close one
05:58of the inputs, so lemme go ahead here
05:59and search if in my HTML
06:01where did I not close one of my inputs.
06:04So this one is closed, this one is closed.
06:08Our label starts here and ends here,
06:10but our input right here is not closed.
06:13So let me just go ahead and add here a close here.
06:16Very critical in these type of applications,
06:19JSX is not very forgiving if you don't close down everything
06:22that needs to be closed down.
06:24So we have your label
06:25and our input, our close, let's go ahead
06:26and just give it another refresh
06:28and we should get now our application,
06:30but we're not gonna get the actual login.
06:32To get the actual login inside.
06:34We have to do one more thing.
06:36And the one more thing that we need
06:38to do is we actually need to go
06:39and address inside of our layout itself
06:42and ask for it to basically display the children.
06:45Now to display the children is not gonna happen on
06:49its own automatically.
06:50And that's something that could be confusing.
06:52But instead of that, what you wanna do is you want to go
06:55to the area inside of your app, inside of your layout,
06:58really in our case deciding
07:00where do you wanna put those children.
07:02In this case I want to put the children inside
07:04of my box right underneath our H two.
07:07To do that again, I'm gonna open a curly bracket
07:09and close a curly bracket to mark, um, react js
07:13and JSX that there's gonna be code inside here.
07:16And I'm gonna go ahead and set here this props again.
07:19And I'm gonna ask for the children,
07:20which is gonna be a property that is created dynamically
07:23that has the children of this element.
07:26Now the children could be one element,
07:27but they could also be multiple elements.
07:30So this child didn't have to be the lockin,
07:32it also could have been just HTML.
07:34As long as my root element above of the actual,
07:37of each component is only one item, then we're good.
07:41But we could have had more than one
07:42item inside of our layout.
07:43In this case, our children is only one child
07:46and if I go ahead and save this
07:47and click here on refresh in my browser,
07:50we should now get our login if we didn't have an error.
07:54So let's see, I provided type, okay, so this is a problem
07:57that I have be and it's something
07:58that we're gonna be approaching a little bit later on.
08:01So I'm gonna go ahead and just take all
08:03of this information here
08:05and I'm gonna get rid of my remember me just for now, just
08:08for the sake of our application working,
08:10let me just put it right here 'cause we're gonna get back
08:12to it in the next lecture.
08:14So let me just go ahead here and just save this
08:17and not next,
08:18but very, very soon we're gonna approach that as well.
08:21Alright, so there we go. And we have our login inside.
08:23Go ahead and implement OTO the register.
08:26We're gonna do that in between the videos.
08:28And then when we're back, I'm gonna talk about one more
08:30thing related to limiting
08:33or enforcing that you must have a child in this case,
08:36if I didn't send in a child,
08:38if I didn't went into my application, you know what?
08:40Let's do it actually. Now, if I didn't send in a child,
08:43right, and there is no login, I didn't send in the login
08:46and we go ahead and click here on refresh,
08:48our application will just put nothing.
08:50But what if we didn't want that to happen
08:52and we wanted to enforce
08:53that there won't be more than one element.
08:56Not only that, there won't be more than one,
08:58that there must be one.
09:00So in this case, I'm gonna go ahead here
09:02and I'm gonna say, Hey, you have to have children.
09:04If you don't have children, then don't run.
09:06And in this case, I'm gonna go
09:07and add a new property here, which is called prop types.
09:10And I'm gonna go ahead and set inside of you're an object.
09:15And in this object I'm gonna set a property,
09:17which is called children.
09:20By the way, we don't wanna do this in the logins.
09:22Let me just snatch that out and go back into our layout.
09:25Inside of our layout. We want to do that.
09:26But what we wanna do is we wanna make sure
09:28that our children follow a certain rule.
09:30So react prop types and there's a few different ones.
09:34We're not gonna get into it in this title.
09:36This is just like a side note in many ways, uh,
09:38an advanced side note
09:39and I'm just gonna say is required
09:42basically telling our application
09:43that expect to get a child.
09:45Now if there is no child
09:46and I click, you're on refresh on my application,
09:48now it's going to give me, uh, error telling me, whoa,
09:51we expect a child but a child was not provided.
09:55And only once we actually put inside that child back in
09:57by setting our login back inside of of our layout,
10:01if I click here on refresh,
10:02that error is not gonna appear anymore
10:04and it's gonna render our element with all the element,
10:06everything that we expected it to do.
10:08Okay? So we put in a lot of stuff here.
10:10We've seen our second component in this lecture.
10:13You've learned how to also set in properties, any amount
10:17of properties you really want.
10:18And so how to use those properties.
10:20You've also seen how you tell a component
10:23to render your its own children in this lecture as well.
10:27Not only that, you also learned how to enforce,
10:29enforce the layout to make sure that it has children,
10:32making sure that it's not a self enclosed component.
10:35And that's it. In this lecture we learned about how
10:38to add children and properties that don't change
10:42or we don't expect them to change once they're rendered
10:45to be able to solve the problem
10:47that you're probably thinking about.
10:48So what do you do in a case
10:49where you want something to change?
10:51And that's exactly what we're gonna cover in the next
10:53lecture.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!