React Component Props
Published: 2023-05-09 22:06:26

React is a JavaScript library created and maintained by Meta used for building user interfaces. It is known for its component-based architecture, which allows developers to create reusable and modular code. Props are one of the most important tools to use in React components.

---

<h2>What are props?</h2>

In React, props are a way to pass data from a parent component to a child component. They allow components to be customized and constructed in different ways, without the need to rewrite the component's code.

<h4>For example:</h4>
Imagine you have a parent component called "Home" and a child component called "BlogList" that displays a list of blogs. The actual list of blogs is defined in the Home component in the form of an array of objects:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b89efkodmf40nckwr75c.png)

The BlogList component is the child component that maps over the array of blogs and displays each individual blog. We want to access the blog list array instead of having to copy the code from the Home component into the BlogList component: 


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ezg2jjxdk2a89ti7li1i.png)

We pass in props as an argument to BlogList and render the blogs array properties to a constant variable. By doing so, we can access the blogs array and use dot notation to access its key values. 

To make sure that our new, mapped array renders, we need to return the child component, BlogList, in the parent component, Home, and pass the props as attributes:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t61qinhhatcmysu6ukyg.png)

Instead of creating a constant variable for blogs and setting it to <code>props.blogs</code>, another way to access props is to simply pass props as an argument and access the values using dot notation, such as <code>props.blogs.value</code>. While either will work, there is a way to refactor our code and make it shorter and simpler to read.

---
<h2>Destructuring</h2>

A more effective way to pass in props as an argument is to destructure the props object. Destructuring props takes the keys from the object and creates variables with the same names. The value associated with the key is saved in the corresponding variable so it can be directly accessed.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8lzfdvqm6ly58dadrtc5.png)

This also makes it easier to tell what props are being passed down to a component. In the above example, we know exactly what values are being accessed into the BlogList component instead of having to look for through the entire component for each prop reference.

In the Home component, the title attribute is set to a string and passed down as a prop to BlogList as a header for our blogs array:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/irt6xoh71rfn4pvvcrdm.png)

Our blog, with a little CSS, would end up looking like this:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xlxa0iulqpa5jnx9ow4m.png)

<p>In the case that there is no value for a prop that is being passed as an argument, it is possible to set a default value for a prop. In the above example, if there was no title data, we could set the title to default to whatever we saw fit by simply assigning it a default value using an equal sign. (I.e. <code>function BlogList({ title = "whatever" }))</code>.</p>
---

Props can be used to customize components in a wide variety of ways. Here are some common examples:

<ul>
<li>Setting text, labels, and captions: Props can be used to set the text or label of a component.</li>

<li>Controlling behavior: Props can be used to control the behavior of a component. For example, you could pass a prop to a Modal component to specify whether it should be displayed or hidden.</li>

<li>Styling: Props can be used to apply styles to a component. For example, you could pass a prop to a Button component to specify its color or size.</li>

<li>Data: Props can be used to pass data to a component. As in the above example, you could pass an array of items to a List component to be rendered as a series of list items.</li>

</ul>

<h2>Conclusion</h2>

Props are an important part of React's component-based architecture, as they allow components to be composed and reused in different contexts. By passing different props to a component, you can create variations of that component that can be used in different parts of your application without having to rewrite the component's code.