{"id":15828,"date":"2022-12-26T12:00:39","date_gmt":"2022-12-26T06:30:39","guid":{"rendered":"https:\/\/www.guvi.in\/blog\/?p=15828"},"modified":"2025-10-07T12:58:17","modified_gmt":"2025-10-07T07:28:17","slug":"how-to-use-props-in-react","status":"publish","type":"post","link":"https:\/\/www.guvi.in\/blog\/how-to-use-props-in-react\/","title":{"rendered":"How to use Props in React [in 3 simple steps]"},"content":{"rendered":"\n<p>In this blog, we\u2019ll learn what are props in react and how we can use them in our application to use dynamic data. We\u2019ll also learn about props syntax, the different ways to pass props and the setting up of the default value of props.<\/p>\n\n\n\n<p><a href=\"https:\/\/react.dev\/\" target=\"_blank\" data-type=\"link\" data-id=\"https:\/\/react.dev\/\" rel=\"noreferrer noopener\">React<\/a> &#8211; A very popular framework for developing beautiful UI web applications. It is an open-source library developed by Facebook and a commonly used library. It is a JavaScript-based library that consists of multiple extensions for the entire application.<\/p>\n\n\n\n<p>With the help of React, we can create dynamic applications, as it needs less code and gives more functionality. Not only this, it also provides a dedicated tool for debugging the application to find errors or bugs.&nbsp;<\/p>\n\n\n\n<p>As react allows us to create dynamic web apps therefore we can pass different data to the components with the help of props. Props are used in passing data from one react component to another component. In simple terms, it is how an app shares data across different parts or components of the application.&nbsp;<\/p>\n\n\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Pre-requisites<\/strong><\/h2>\n\n\n\n<p>Before moving on to learning about react props, make sure to create a react application project so that we can use props. We have already explained how to create a new react application in one of our blogs. <a href=\"https:\/\/www.guvi.in\/blog\/how-to-fetch-and-display-data-from-api-in-react\/\" target=\"_blank\" data-type=\"URL\" data-id=\"https:\/\/www.guvi.in\/blog\/how-to-fetch-data-using-api-in-react\/\" rel=\"noreferrer noopener\">Learn more<\/a>.<\/p>\n\n\n\n<p>Before diving into the next section, ensure you&#8217;re solid on full-stack development essentials like front-end frameworks, back-end technologies, and database management. If you are looking for a detailed Full Stack Development career program, you can join HCL <strong><a href=\"https:\/\/www.guvi.in\/zen-class\/full-stack-development-course\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=how-to-use-props-in-react\">GUVI\u2019s Full Stack Development Career Program<\/a><\/strong> with Placement Assistance. You will be able to master the <a href=\"https:\/\/www.guvi.in\/blog\/guide-for-mern-stack\/\" target=\"_blank\" rel=\"noreferrer noopener\">MERN stack<\/a> (MongoDB, Express.js, React, Node.js) and build real-life projects.<\/p>\n\n\n\n<p>Additionally, if you want to explore ReactJS through a self-paced course, try HCL<a href=\"https:\/\/www.guvi.in\/courses\/web-development\/advanced-javascript\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=article_title\"> <\/a><strong><a href=\"https:\/\/www.guvi.in\/courses\/web-development\/react-js\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=how-to-use-props-in-react\">GUVI\u2019s ReactJS self-paced certification course.<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What are Props in React?<\/strong><\/h2>\n\n\n\n<p>Props or properties in react are a functional argument by which different components communicate with each other. Props is just a data\/information that allows us to pass in JSX tags. With the help of props, we can pass values like JavaScript objects, arrays or functions, etc.&nbsp;<\/p>\n\n\n\n<p>Props are passed from a parent component to the child component. Now, if you haven\u2019t understood props yet, it allows us to reuse a component logic dynamically which means that the data or information in the component will not be static. We\u2019ll understand this with the help of an example later on.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Props Syntax<\/strong><\/h2>\n\n\n\n<p>We can use the react props in two different ways and both have their own different syntaxes. The two ways of using props in react app are:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Without destructuring:<\/strong><\/h3>\n\n\n\n<p>The syntax of defining props without destructuring is given below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Course(props) {\n    &lt;div&gt;{props.courseName}&lt;\/div&gt;\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>With destructuring:<\/strong><\/h3>\n\n\n\n<p>The syntax of defining props as a destructuring argument is given below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Course({courseName}) {\n    &lt;div&gt;{courseName}&lt;\/div&gt;\n}\n<\/code><\/pre>\n\n\n\n<p>The detailed working of both the given methods is explained below in this blog.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>How to use Props in React?<\/strong><\/h2>\n\n\n\n<p>Now let\u2019s see a full example and understand how we can use props in a react app. Props are very easy to understand. In this example, we\u2019ll need two things: first one is the main function component <strong>(App.js)<\/strong> and a second component <strong>(Course) <\/strong>which we is going to render in our App.js component.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>App.js Component File:<\/strong><\/h3>\n\n\n\n<p>This is the App component (parent) which is the main file of a React application and in this component, we\u2019ll be rendering our second component i.e., Course component which acts as a child component in which we\u2019ll be passing props.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\nimport React from \"react\";\n\nfunction App() {\n\n  return (\n    &lt;div&gt;\n\t&lt;h1&gt;Guvi Courses&lt;\/h1&gt;\n    &lt;\/div&gt;\n  );\n}\n\nexport default App;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Course component<\/strong><\/h3>\n\n\n\n<p>This component will have a prop called <strong><em>courseName <\/em><\/strong>which will get its value dynamically after rendering inside the App.js component.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Course(props) {\n  return (\n    &lt;div&gt;\n      &lt;ul&gt;\n        &lt;li&gt;{props.courseName}&lt;\/li&gt;\n      &lt;\/ul&gt;\n    &lt;\/div&gt;\n  )\n}\n<\/code><\/pre>\n\n\n\n<p>Now render the <strong>Course<\/strong> component into the App component to pass the props. Finally your App.js file should look like below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import React from \"react\";\n\nfunction Course(props) {\n  return (\n    &lt;div&gt;\n      &lt;ul&gt;\n        &lt;li&gt;{props.courseName}&lt;\/li&gt;\n      &lt;\/ul&gt;\n    &lt;\/div&gt;\n  )\n}\n\nfunction App() {\n\n  return (\n    &lt;div&gt;\n      &lt;h1&gt;Guvi Courses&lt;\/h1&gt;\n      &lt;Course courseName=\"Full Stack Development\"  \/&gt;\n    &lt;\/div&gt;\n  );\n}\n\nexport default App;\n<\/code><\/pre>\n\n\n\n<p>In the above code, first we have accessed the information from props in the &lt;li\/&gt; element and then after that we are passing a prop called &#8220;courseName&#8221; in our &lt;Course \/&gt; component reading the value of the prop as &#8220;Full Stack Development&#8221;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Different ways of using props in react<\/strong><\/h2>\n\n\n\n<p>Till now we have understood the basic syntax of using props in react and now we\u2019ll learn what are the ways through which we can use props in a react app. So, there are two ways of using props and those are discussed in detail below.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>using Props without Destructuring<\/strong><\/h3>\n\n\n\n<p>The First way of using props is to use them without destructuring. When using props without destructuring we pass an argument known as <strong>props <\/strong>in the function. Let\u2019s understand it step by step with the help of an example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Pass &#8220;props&#8221; as argument in function<\/strong><\/h3>\n\n\n\n<p>To use props, we first pass the \u201cprops\u201d keyword as an argument into our function which can be seen as follows.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Course(props) {\n\t\u2026\n}\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Using variables directly in JSX template<\/strong><\/h3>\n\n\n\n<p>Here we are not declaring the variables before rather we are using them directly into our JSX template i.e., into our &lt;li&gt; element.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Course(props) {\n\treturn (\n    &lt;ul&gt;\n       \t        &lt;li&gt;{props.courseName}&lt;\/li&gt;\n      \t    &lt;\/ul&gt;\n)\n}\n\nexport default Course;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 3: Pass data in App.js component<\/strong><\/h3>\n\n\n\n<p>Now our data will be passed to the main component i.e. App.js and will look like we are passing the attributes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function App() {\n\n  return (\n    &lt;div className=\"container\"&gt;\n      &lt;Course courseName=\"Full Stack Development\"  \/&gt;\n    &lt;\/div&gt;\n  );\n}\n\nexport default App;\n<\/code><\/pre>\n\n\n\n<p>Styles given below are optional to use.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\n\/\/App.css file\n\n.container {\n  margin-left: 20px;\n}\n\n.container h1 {\n  color: green\n}\n\n.container li {\n  font-size: 20px;\n}<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Output:<\/strong><\/h3>\n\n\n\n<p>After passing the props, your output will look like below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"396\" height=\"260\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2022\/12\/screely-1671693385911.png\" alt=\"\" class=\"wp-image-15835\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2022\/12\/screely-1671693385911.png 396w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2022\/12\/screely-1671693385911-300x197.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2022\/12\/screely-1671693385911-150x98.png 150w\" sizes=\"(max-width: 396px) 100vw, 396px\" title=\"\"><figcaption class=\"wp-element-caption\">Props without destructuring<\/figcaption><\/figure><\/div>\n\n\n<p>So, now we can use data dynamically for any component with the above method and can declare multiple props as per our requirement. Next we\u2019ll see how to use props with destructuring.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. <strong>using Props with Destructuring<\/strong><\/h2>\n\n\n\n<p>Compared to the above method (without destructuring), this is the most commonly used method to use props as it becomes very easy to modify the function and also it takes less time therefore making it as an optimized way.<\/p>\n\n\n\n<p>In this method, we don&#8217;t declare variables first or pass them directly to the JSX template. Instead of passing the props as an argument we destructured and passed in the variables like the function argument. Let\u2019s understand this in detail and see it step by step with the help of an example.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 1: Destructuring the Course component<\/strong><\/h3>\n\n\n\n<p>Here we are not declaring the variables rather we are destructuring and passing the variables as function arguments. We can read these props by listing their names which are available inside <strong>({ and }) <\/strong>directly after the Course component.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Course({ courseName, courseType }) {\n     return (\n       &lt;ul&gt;\n       \t   &lt;li&gt;{courseName}&lt;\/li&gt;\n           &lt;li&gt;{courseType}&lt;\/li&gt;\n       &lt;\/ul&gt;\n     )\n}\n\nexport default Course;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Step 2: Pass data in App.js component<\/strong><\/h3>\n\n\n\n<p>Now our data will be passed to the main component i.e. App.js and will look like we are passing the attributes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function App() {\n\n  return (\n    &lt;div className=\"container\"&gt;\n      &lt;h1&gt;Guvi Courses&lt;\/h1&gt;\n      &lt;Course courseName=\"Full Stack Development\" courseType=\"Development\" \/&gt;\n    &lt;\/div&gt;\n  );\n}\n\nexport default App;\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Output:<\/strong><\/h3>\n\n\n\n<p>After passing the props, your output will look like below.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img decoding=\"async\" width=\"404\" height=\"272\" src=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2022\/12\/screely-1671693750341.png\" alt=\"\" class=\"wp-image-15836\" srcset=\"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2022\/12\/screely-1671693750341.png 404w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2022\/12\/screely-1671693750341-300x202.png 300w, https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2022\/12\/screely-1671693750341-150x101.png 150w\" sizes=\"(max-width: 404px) 100vw, 404px\" title=\"\"><figcaption class=\"wp-element-caption\">Props with destructuring<\/figcaption><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\"><strong>How to specify default value of prop<\/strong><\/h2>\n\n\n\n<p>Sometimes, we don\u2019t want the props value to be empty or when no value is specified, at that time we can specify a default value just by destructuring and putting <strong>= <\/strong>sign and the default value right after the parameter as shown below.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function Course({ courseName, courseType = \"development\" }) { \n     ... \n}\n<\/code><\/pre>\n\n\n\n<p>So, now when the &lt;Course \/&gt; is rendered with no course type prop, it will be automatically set to &#8220;development&#8221;.<\/p>\n\n\n\n<p>Kickstart your Full Stack Development journey by enrolling in HCL <a href=\"https:\/\/www.guvi.in\/zen-class\/full-stack-development-course\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=how-to-use-props-in-react\"><strong>GUVI&#8217;s certified Full Stack Development Career Program<\/strong><\/a><strong> with Placement Assistance<\/strong> where you will master the MERN stack (MongoDB, Express.js, React, Node.js) and build interesting real-life projects. This program is crafted by our team of experts to help you upskill and assist you in placements. <\/p>\n\n\n\n<p>Alternatively, if you want to explore ReactJS through a self-paced course, try HCL <strong><a href=\"https:\/\/www.guvi.in\/courses\/web-development\/react-js\/?utm_source=blog&amp;utm_medium=organic&amp;utm_campaign=how-to-use-props-in-react\">GUVI\u2019s ReactJS self-paced course.<\/a><\/strong><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this blog, we learned about props in react, how they are used, their syntax, and different ways of using props. We hope you get a better understanding of how the default value of props are set. To read props, we saw two ways i.e., with and without destructuring. To get more understanding of props, make sure to do practice of them in a project.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this blog, we\u2019ll learn what are props in react and how we can use them in our application to use dynamic data. We\u2019ll also learn about props syntax, the different ways to pass props and the setting up of the default value of props. React &#8211; A very popular framework for developing beautiful UI [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":15829,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[737],"tags":[803,802,804],"views":"15561","authorinfo":{"name":"GUVI Geek","url":"https:\/\/www.guvi.in\/blog\/author\/admin\/"},"thumbnailURL":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2022\/12\/react-props-300x169.png","jetpack_featured_media_url":"https:\/\/www.guvi.in\/blog\/wp-content\/uploads\/2022\/12\/react-props.png","_links":{"self":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/15828"}],"collection":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/comments?post=15828"}],"version-history":[{"count":25,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/15828\/revisions"}],"predecessor-version":[{"id":88929,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/posts\/15828\/revisions\/88929"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media\/15829"}],"wp:attachment":[{"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/media?parent=15828"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/categories?post=15828"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.guvi.in\/blog\/wp-json\/wp\/v2\/tags?post=15828"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}