React | Props
Published: 2021-01-14 08:09:56

Hello developers! In my last blog, we talked about <i>components</i> and what Stateful, Stateless, and Presentational Components mean in the world of React. That's fantastic! At this point, you're feeling confident creating components and that would allow you to bring to life a flexible and organized application. 

If you're learning React  I can assume you are familiar with JavaScript. If you're familiar with JavaScript you know what Objects Properties are. So at this point, you know something is missing and you're thinking: <i>"Well, components are great and everything, but how can I pass my data from component to component?"</i> You probably already guessed the answer. Yes, we use <strong>props</strong>.

Something really functional and practical about components is that they're reusable. As a developer, we don't like repeating code. Using components is the best way in react to avoid that. Let's take a look at this example: 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/aowvcgac0f2vw3ihs2ga.png)

The first thing you have to know about passing components is that they need to be imported and exported. Our component can be exported thanks to the following line of code:
<pre><code>export default ComponentEx;</code></pre>

Ok, now let's see what's going on here. Let's start with line 2:
<pre><code>import ComponentEx from "./ComponentEx;"</code></pre>
This code is quite self-explanatory. In this case, we're importing the ComponentEx from the file "./ComponentEx. Something worth knowing is that <strong>ComponentEx</strong> is just a placeholder for the file. You could name this <strong>LalaLand</strong> and would work the same. For code etiquette (and logic) we use the component name to represent the component we're importing. 

Now, let's take a look at line 13: 
<pre><code> < ComponentEx name={"Ethan"} age={"8"} user={user} / > </code></pre>

We want to pass data from one component to another because we don’t want to have a component rendering static data. The goal is to pass dynamic data to your component instead. That’s where React’s props come into play. The way we pass data in React is by defining custom HTML attributes.  In our example, we have three props been pass to our component: 

<pre><code>name={"Ethan"}
age={"8"}
user={user}</code></pre>

We'll assign our data using JSX syntax. <strong>Don’t forget the curly braces!</strong> As you can see your data can be pass in different forms. In this case, I'm passing a string, a number, and an object. Pretty great, right?

Let's take a look at our <strong>ComponentEx</strong> component:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/635w9cuwst8nm7dn8lzx.png)

As we can see, we pass props as an argument in our functional component. Once you have your "props" you're ready to use the data. In this case, I'm outputting the data on a <strong>p tag</strong>. The way we access the data using props is the following:

<pre><code> {props.name} </code></pre>

Again, we're using JSX so<strong> don’t forget the curly braces!</strong>

...and that's it! Now, you're ready to pass data between components! 

I would like to discuss three important things before finishing this blog:

<strong>1.</strong> Props are inherited from the parent components to the child component. Keep this in mind when creating the logistics or your application. 
<strong>2.</strong> When using a class just add <strong>this</strong> before props. For example: 

<pre><code> {this.props.name} </code></pre>

Remember, when we use a class we're extending <strong>React.Component</strong> which include the <strong>props</strong> properties. I know, Magic! 

<strong>3.</strong> Props are read-only. Passing props from component to component doesn’t make the component interactive. To do so, we need to use something call <strong>state.</strong>  

Now, that's it! For real this time. In the next blog, we will learn more about our new friend <strong>state</strong>.

Thank you for reading! 😊