Learn React 18: Passing Data With Props

In React, we primarily use props to pass data to our components. 

In this lesson from our free full-length beginner course on React 18, you’ll learn how. 

Working with software means that you will have to deal with data. You will take some input data, do some processing, and then optionally output the final result.

In React, we primarily use props to pass data to our components. The word props is basically a shorter term used for properties. You have seen them in action in last few tutorials where we created components to display information about countries.

In this tutorial, we will focus on learning about props in more detail. This tutorial will simply cover the basics and then we will move to more advanced topics later in the series.

Props are Like Attributes

The easiest way to understand props is to think of them like the attributes that you can pass to HTML elements during web development. However, props are much more advanced.

Any prop will have two elements. The first is the prop name which is simply the attribute name. The second is the prop value which is the value of that attribute. You can assign as many props to your component as you like.

There are two rules that you have to follow when assigning a name to different props.

  1. The prop name cannot be a reserved keyword from JavaScript. This is because the JSX we write will ultimately be converted to JavaScript and using reserved keywords will mess things up. This is why we use className instead of class and htmlFor instead of for as prop name.
  2. The prop names should be in camelCase.

As I said earlier, you can pass as many props to a component as you like. However, a component is not required to use all the props.

You can think of passing props to components as passing parameters to functions. Components are like functions in that sense and just like you can pass any kind of value as parameter to functions, you can pass any kind of value as a prop.

In the above example, we replaced the JSX for our Country component with a function call to Country and stored the result inside countryElement. However, rendering out countryElement in the end gave us the same result.

Props Must be Read-Only

A component is not supposed to modify the value of its props. The functions or classes that we define to create our component must keep the props as read-only. This behavior is enforced when you create a React app by running the command:

In such cases, the following code will give you an error about “name” being read-only.

The reason props must be immutable is because components are supposed to use them to get information from their parents. If you actually want to modify some information in a component, using state is your best bet. State is basically data that is maintained within the component and React will automatically update the DOM based on any changes in state. We will learn more about that later in the series.

Keep in mind that data in React flows from parent to child and so on further down the list.

Final Thoughts

I hope you now have a basic understanding of props in React. In later tutorials, we will discuss how to validate props or provide default values for them.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Nitish Kumar

In React, we primarily use props to pass data to our components. 

In this lesson from our free full-length beginner course on React 18, you'll learn how. 

Working with software means that you will have to deal with data. You will take some input data, do some processing, and then optionally output the final result.

In React, we primarily use props to pass data to our components. The word props is basically a shorter term used for properties. You have seen them in action in last few tutorials where we created components to display information about countries.

In this tutorial, we will focus on learning about props in more detail. This tutorial will simply cover the basics and then we will move to more advanced topics later in the series.

Props are Like Attributes

The easiest way to understand props is to think of them like the attributes that you can pass to HTML elements during web development. However, props are much more advanced.

Any prop will have two elements. The first is the prop name which is simply the attribute name. The second is the prop value which is the value of that attribute. You can assign as many props to your component as you like.

There are two rules that you have to follow when assigning a name to different props.

  1. The prop name cannot be a reserved keyword from JavaScript. This is because the JSX we write will ultimately be converted to JavaScript and using reserved keywords will mess things up. This is why we use className instead of class and htmlFor instead of for as prop name.
  2. The prop names should be in camelCase.

As I said earlier, you can pass as many props to a component as you like. However, a component is not required to use all the props.

You can think of passing props to components as passing parameters to functions. Components are like functions in that sense and just like you can pass any kind of value as parameter to functions, you can pass any kind of value as a prop.

In the above example, we replaced the JSX for our Country component with a function call to Country and stored the result inside countryElement. However, rendering out countryElement in the end gave us the same result.

Props Must be Read-Only

A component is not supposed to modify the value of its props. The functions or classes that we define to create our component must keep the props as read-only. This behavior is enforced when you create a React app by running the command:

In such cases, the following code will give you an error about "name" being read-only.

The reason props must be immutable is because components are supposed to use them to get information from their parents. If you actually want to modify some information in a component, using state is your best bet. State is basically data that is maintained within the component and React will automatically update the DOM based on any changes in state. We will learn more about that later in the series.

Keep in mind that data in React flows from parent to child and so on further down the list.

Final Thoughts

I hope you now have a basic understanding of props in React. In later tutorials, we will discuss how to validate props or provide default values for them.


This content originally appeared on Envato Tuts+ Tutorials and was authored by Nitish Kumar


Print Share Comment Cite Upload Translate Updates
APA

Nitish Kumar | Sciencx (2022-09-23T23:12:30+00:00) Learn React 18: Passing Data With Props. Retrieved from https://www.scien.cx/2022/09/23/learn-react-18-passing-data-with-props/

MLA
" » Learn React 18: Passing Data With Props." Nitish Kumar | Sciencx - Friday September 23, 2022, https://www.scien.cx/2022/09/23/learn-react-18-passing-data-with-props/
HARVARD
Nitish Kumar | Sciencx Friday September 23, 2022 » Learn React 18: Passing Data With Props., viewed ,<https://www.scien.cx/2022/09/23/learn-react-18-passing-data-with-props/>
VANCOUVER
Nitish Kumar | Sciencx - » Learn React 18: Passing Data With Props. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/09/23/learn-react-18-passing-data-with-props/
CHICAGO
" » Learn React 18: Passing Data With Props." Nitish Kumar | Sciencx - Accessed . https://www.scien.cx/2022/09/23/learn-react-18-passing-data-with-props/
IEEE
" » Learn React 18: Passing Data With Props." Nitish Kumar | Sciencx [Online]. Available: https://www.scien.cx/2022/09/23/learn-react-18-passing-data-with-props/. [Accessed: ]
rf:citation
» Learn React 18: Passing Data With Props | Nitish Kumar | Sciencx | https://www.scien.cx/2022/09/23/learn-react-18-passing-data-with-props/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.