{"id":723,"date":"2024-01-30T09:21:40","date_gmt":"2024-01-30T09:21:40","guid":{"rendered":"https:\/\/www.cmsgalaxy.com\/blog\/?p=723"},"modified":"2024-02-05T11:03:43","modified_gmt":"2024-02-05T11:03:43","slug":"props-in-react-js","status":"publish","type":"post","link":"https:\/\/www.cmsgalaxy.com\/blog\/props-in-react-js\/","title":{"rendered":"Props in React.js"},"content":{"rendered":"\n<p>In React.js, <code>props<\/code> (short for properties) are a way to pass data from a parent component to a child component. They are a fundamental part of React&#8217;s component-based architecture and are used to make components reusable and customizable.<\/p>\n\n\n\n<p>Here&#8217;s how <code>props<\/code> work in React:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Passing Props:<\/strong><br>Parent components can pass data to child components by including attributes in the JSX markup when rendering the child component. These attributes are then accessible within the child component as <code>props<\/code>. Example:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   \/\/ ParentComponent.js\n   import React from 'react';\n   import ChildComponent from '.\/ChildComponent';\n\n   const ParentComponent = () =&gt; {\n       return &lt;ChildComponent name=\"John\" age={30} \/&gt;;\n   };\n\n   export default ParentComponent;<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>   \/\/ ChildComponent.js\n   import React from 'react';\n\n   const ChildComponent = (props) =&gt; {\n       return (\n           &lt;div&gt;\n               &lt;p&gt;Name: {props.name}&lt;\/p&gt;\n               &lt;p&gt;Age: {props.age}&lt;\/p&gt;\n           &lt;\/div&gt;\n       );\n   };\n\n   export default ChildComponent;<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"2\">\n<li><strong>Accessing Props:<\/strong><br>Inside the child component, you can access the props passed from the parent component using the <code>props<\/code> object. Each prop passed by the parent is accessible as a property of the <code>props<\/code> object. Example:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   const ChildComponent = (props) =&gt; {\n       return (\n           &lt;div&gt;\n               &lt;p&gt;Name: {props.name}&lt;\/p&gt;\n               &lt;p&gt;Age: {props.age}&lt;\/p&gt;\n           &lt;\/div&gt;\n       );\n   };<\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\" start=\"3\">\n<li><strong>Immutable Nature:<\/strong><br>Props are immutable, meaning that they cannot be modified by the child component. They are read-only, and any changes to props should be managed by the parent component.<\/li>\n\n\n\n<li><strong>Default Props:<\/strong><br>You can define default values for props using the <code>defaultProps<\/code> property. If a prop is not provided by the parent component, the default value will be used instead. Example:<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>   const ChildComponent = (props) =&gt; {\n       return (\n           &lt;div&gt;\n               &lt;p&gt;Name: {props.name}&lt;\/p&gt;\n               &lt;p&gt;Age: {props.age}&lt;\/p&gt;\n           &lt;\/div&gt;\n       );\n   };\n\n   ChildComponent.defaultProps = {\n       name: 'Anonymous',\n       age: 0\n   };<\/code><\/pre>\n\n\n\n<p>Props play a crucial role in React development by enabling components to be customizable, reusable, and easily composed together to build complex user interfaces. They facilitate the flow of data throughout your application&#8217;s component hierarchy, making it easier to manage and maintain your code.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In React.js, props (short for properties) are a way to pass data from a parent component to a child component.<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-723","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/723","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/comments?post=723"}],"version-history":[{"count":1,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/723\/revisions"}],"predecessor-version":[{"id":724,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/posts\/723\/revisions\/724"}],"wp:attachment":[{"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/media?parent=723"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/categories?post=723"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.cmsgalaxy.com\/blog\/wp-json\/wp\/v2\/tags?post=723"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}