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

Choosing Between Children or Props in React

In this lesson, we addressed a critical issue in our button component and explored dynamic content management using props and children in React. Here's what you need to know:


Fixing the Button Issue

Previously, our buttons had static text baked into the component, which violated the principle of reusability. We updated the button to accept dynamic content through props and children.

Steps to Fix the Button

  1. Convert Static Text to a Dynamic Label
    Replace the hardcoded text with a prop, such as label.

    const Button = (props) => {
        return <button>{props.label}</button>;
    };
    
  2. Pass the Label as a Prop
    When using the Button component, pass the desired label value:

    <Button label="Discover Things" />
    <Button label="Discover Stuff" />
    <Button label="Discover Things and Stuff" />
    

Leveraging props.children

Instead of relying solely on props, we demonstrated how to use the children prop for flexibility.

Example Using children:

Modify the button to render content based on props.label or fallback to props.children:

const Button = (props) => {
    return <button>{props.label || props.children}</button>;
};

Usage Example:

Pass inline content as children:

<Button>Explore the World</Button>

Or fallback to the label prop:

<Button label="Discover Adventures" />

Key Takeaways

  • Dynamic Content: Always structure components to allow for dynamic data through props or children.
  • Flexibility: Choose between props.label for explicit data or props.children for inline content based on the use case.
  • Best Practices: Prioritize reusable and readable components to simplify future updates.

In this video, we fixed our button and laid the foundation for the rest of the course, where we’ll dive into React state management and animations.

Video Transcript

00:00Before we could continue, we have
00:02to fix a problem with our button.
00:03So in the process, we'll fix it
00:05as we talk about the differences
00:07or the best ways to work between props or children.
00:09Just another extra bonus, because I didn't notice it.
00:12So let's go ahead and fix our button issues.
00:15Alright, let's continue down our animation journey.
00:19But before we do that, there's one thing that is glaring
00:22that we just have to fix.
00:24And that is, right now all
00:25of our buttons are saying discover things.
00:28But more than that, that actual value is actually,
00:33actually, actually actually baked
00:34into our button right here.
00:36Discover things that is
00:38horrible, horrible, horrible, horrible.
00:40So all this work about making everything dynamic,
00:43hopefully you noticed that before me,
00:45but for some reason I was daydreaming about that.
00:48So let me just go ahead here
00:49and just set our button to be self enclosed
00:52so we could then add into our button of value.
00:55We could have sent it also as a property if we wanted to.
00:58Hey, maybe that's what we want to do.
01:00Maybe we want to set our button where we have a label
01:05and our label will be whatever value that we want to set it.
01:08So let's say, we'll set it, discover, discover things,
01:13and let's set another label
01:15and let's set the next label of the next button
01:17to be discover stuff.
01:22And our last button will be
01:24discover things and stuff.
01:28And let me not make it an outline
01:30'cause it was, it's kind of hard to
01:31read when it's an outline.
01:33Okay? So all that we have to do is go into our button
01:37and in our button instead of having here, oh, word wrap.
01:41Why can't you always default to word wrap? Oh, okay.
01:45Besides that, it's a great editor. Okay, so discover things.
01:47I'm just gonna get rid of that.
01:49And I'm just gonna approach directly our this label.
01:54Now, by the way, by doing this, just as a reminder,
01:57what's actually gonna happen, we're gonna get an error.
01:59And the error or notice that we should be getting is
02:02that if we find it, I'm gonna ignore these transitions.
02:04We're gonna talk about that in a moment here.
02:06Okay, we didn't get an error,
02:08but we didn't get any text either.
02:09Let's check here what's going on here. This props label.
02:14Yeah, that would do it. And our copy is inside. Beautiful.
02:19So now we have really a dynamic button with dynamic content.
02:22And you could choose, do you wanna set it to be with, uh,
02:26self enclosed in a label using a property?
02:28Or maybe you prefer going it in a different route
02:32where if there is no label
02:34and we have your um button, we could go ahead
02:38and basically uppercase button by the way.
02:40And here I could here other way.
02:44And in our button here, I would put your maybe,
02:47obviously this is overkill.
02:48I would put here a label.
02:50Or if that label does not exist, go to this
02:53props children.
02:57So whatever
02:58Works more brilliantly for you, as long
03:01as you put in your content to be dynamic
03:03and you've really, we really captured everything there is
03:06to do about properties.
03:07And now really our button is completely dynamic.
03:10And we're finally ready in this last chapter
03:13in the next few lectures until we're done
03:15and wrapping up this title to talk about state
03:18and even a little bit of animation of state.
03:21In this lecture, we fixed our button to prepare it
03:23for really the rest of this section
03:25because in the next section we're gonna go back
03:27and work on the big topic, which is gonna be events itself.

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!