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

Passing Properties to Components

Tutorial: Passing Properties to Components

In this tutorial, you’ll learn how to use React's props to create dynamic and reusable components. This is essential for building scalable React applications by passing unique data to each component.


What You Will Learn

  1. How to use JSX properties to pass data to components.
  2. The difference between props and state in React.
  3. How to dynamically render components with unique data.

Steps to Use Props in Components

1. Understand Props vs. State

  • Props: Immutable data passed from parent to child components.
  • State: Data managed within a component that can change over time.

2. Define Props in JSX

Add properties to your PortfolioItem component instances:

<PortfolioItem img="cabin" />
<PortfolioItem img="cake" />
<PortfolioItem img="circus" />
<PortfolioItem img="game" />
<PortfolioItem img="safe" />
<PortfolioItem img="submarine" />

3. Access Props in the Component

Use the this.props object to access the data passed to the component:

class PortfolioItem extends React.Component {
  render() {
    const path = `img/portfolio/${this.props.img}.jpg`;
    return (
      <div className="portfolio-item">
        <img src={path} alt={this.props.img} />
        <div className="overlay">{this.props.img}</div>
      </div>
    );
  }
}
  • Template Literals: Use backticks (`) to dynamically insert the prop value into the path.

4. Dynamic Rendering

Now, each PortfolioItem component will display a unique image based on the img prop.


Common Pitfalls and Tips

  1. Default Values for Props:
    Use default props to ensure a fallback value:
PortfolioItem.defaultProps = {
  img: 'default'
};
  1. Prop Validation:
    Ensure the correct type of data is passed using PropTypes:
import PropTypes from 'prop-types';

PortfolioItem.propTypes = {
  img: PropTypes.string.isRequired
};

Summary

By passing props, you’ve made your components more dynamic and reusable. Each instance of PortfolioItem can now display unique data, reducing redundancy and improving maintainability.

In the next lesson, you’ll learn how to pass entire components as props, further expanding your React knowledge.

Video Transcript

00:00In the last lecture, we put a component into a component.
00:04We saw it in action using JSX In this lecture,
00:07we really need to enable to send properties into components
00:12so we could then have more reusability practice with 'em.
00:14We don't only, we don't only want things
00:16that are exact duplicates.
00:18So we're gonna use JSX properties, see how they work,
00:22and we'll also understand what a property actually is
00:24and in what context do we want to use a property.
00:27And I'll note that before we even get to it.
00:30There are two types of data types you put
00:32into a react component.
00:34There's a property and there's a state.
00:37Property is something that we assume that won't change,
00:40or at least the component doesn't need
00:42to be aware that it could change.
00:44While a state on the contrary is something
00:47that we expect the component itself to be aware
00:50of, that it could change.
00:52Now, normally speaking, usually we want everything
00:54to be properties unless we must have a state.
00:57And we'll see what that actually means in a later lecture.
01:00But for now, let's get familiar with properties
01:03through the introduction of the props property.
01:08What we're gonna do next is really the heart
01:10of creating dynamic and smart and reusable react components.
01:15Now when we take a look at our portfolio, you'll notice
01:18that if you look through one by one,
01:20really there's only one thing that is different
01:23among each item, which is the image itself
01:25and really more specifically the actual name
01:28of the image itself that is being used while all
01:31the rest is the same.
01:33So for us to address this, we're gonna start using something
01:36that is called properties.
01:37Just like you have HDML properties.
01:40So do JSX components have properties,
01:44and the way you pass those properties is literally the same
01:46way by defining a property.
01:48So for example, I'll set here an image path
01:50and then I'll set here just the cake itself.
01:53'cause that's really the only part that is different.
01:56Now, to save us a little bit of time, I'm gonna go ahead
01:59and just duplicate all the different possible name
02:02combinations that I already prepared in advance,
02:05which was cabin cake, circus game safe and submarine.
02:09And I'm gonna go ahead
02:10and delete everything inside of this row
02:13because we don't really need it anymore.
02:15We're gonna make it into something dynamic.
02:17So now our row itself is a lot cleaner
02:19and all the different elements
02:21that are gonna be dynamic are gonna be controlled
02:23through our portfolio item itself.
02:25Now if I just go and save this and go back
02:27and refresh, we'll see
02:28that we're not gonna have anything really change it.
02:31Besides that, we'll have six elements
02:33that are exactly the same element.
02:34'cause right now we didn't implement it
02:36within our environment.
02:39So the next thing I want to do is I'm gonna go into our
02:42element and I'm just gonna end the render,
02:44create a new variable
02:45because I want to create here the full qualified
02:48path of the image.
02:50Now the full qualified path
02:51of image I'm gonna take advantage of
02:54of string te template strings.
02:57So for that, I'm gonna open up and
02:58Close with that weird co um, um, quote thing
03:03that is next to the digit one.
03:04I have no idea what it's called, but yeah,
03:06you send me an email, let me know what it's called.
03:07So in future videos I'll give it the right name.
03:10So within there, I'm gonna go ahead
03:12and just copy this full path that we have
03:14of our cabin right now.
03:16And now I'll be able to gain access to dynamic
03:19variable naming by going ahead
03:21and putting inside of here that do dollar open
03:23and close curly bracket.
03:25And what I wanna do is I wanna replace this
03:27with the variables
03:28that are coming in the properties of this component.
03:32The way I gain access to those properties is by typing this
03:36props and then the variable name,
03:38property name more accurately.
03:40And the property is called IMG for image.
03:43So by doing this, I've created now a fully qualified path.
03:46So even if I go ahead here
03:47and I just do a console call, console log,
03:52and I just log out that path
03:55and go back into our browser, you'll see that now
03:57as it's rendered, you'll see
03:59that all the different paths are right.
04:01All that's left for me to do is then
04:03to inject it into the area
04:05where our image source used to be.
04:07And I'm gonna go ahead here and delete it
04:08and use a JSX feature, which enable is very similar
04:13to the way that we've used
04:15with strings only this time it's not a string, I put it
04:17outside of any string.
04:19I open and close a curly bracket
04:20and in there I put my variable.
04:22So here we go. I've just put the path
04:24and I'm basically dynamically setting this variable path
04:27within our JSX
04:29and it will gain access to the variable that's in the scope
04:32of this function.
04:34So if I go ahead here
04:35and I say this, go back into my application and click
04:38and refresh, you'll see that now we'll have all
04:40of our component elements exactly the way we want it
04:43to have them now.
04:44Now it's beautiful that it's dynamic,
04:46but we still have a small issue.
04:48Now when you think about it, our issue is, is
04:50that we hardcoded into our portfolio all of these items,
04:54but in real, in a real world scenario, we would really love
04:58to be able to define these elements, really
05:01where in our client outside here,
05:02maybe in our portfolio even.
05:04The reason is, is because maybe we want in a different
05:07order, maybe we, we want different items.
05:09Maybe this item is not six, maybe it's just uh, eight,
05:12maybe it's four, maybe it's two.
05:14But because we hard coded it,
05:16we don't have any way to really make it dynamic.
05:18Our portfolio itself is not dynamic,
05:20although portfolio items are now to address that,
05:23we're gonna have to figure out exposing
05:25that portfolio as well.
05:27'cause literally what we could do is I'm gonna go ahead here
05:29and if I just, you know,
05:31let's do it in the next lecture, let's break it down.
05:33'cause in the next lecture we'll see how do we actually
05:36pass in these children dynamically without knowing a hard
05:40set amount of children in advance.
05:43In this lecture, we were introduced to the props property
05:46and you now know how
05:47to integrate dynamic content into your component.
05:51In the next lecture, we'll move from just passing in
05:54properties into actually passing components into components.

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!