<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>React - TheCodeBuzz</title>
	<atom:link href="https://thecodebuzz.com/category/react/feed/" rel="self" type="application/rss+xml" />
	<link>https://thecodebuzz.com</link>
	<description>Best Practices for Software Development</description>
	<lastBuildDate>Wed, 24 Jan 2024 03:03:56 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	

<image>
	<url>https://thecodebuzz.com/wp-content/uploads/2022/11/cropped-android-chrome-512x512-1-1-51x51.jpg</url>
	<title>React - TheCodeBuzz</title>
	<link>https://thecodebuzz.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Sonar Qube &#8211; Code Quality of Angular, React, and Vue applications</title>
		<link>https://thecodebuzz.com/continuous-code-quality-in-angular-7-applications-using-sonar/</link>
					<comments>https://thecodebuzz.com/continuous-code-quality-in-angular-7-applications-using-sonar/#comments</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Tue, 01 Mar 2022 00:00:00 +0000</pubDate>
				<category><![CDATA[Angular]]></category>
		<category><![CDATA[Code Smell]]></category>
		<category><![CDATA[React]]></category>
		<category><![CDATA[Vue.js]]></category>
		<category><![CDATA[angular 4 code review tool]]></category>
		<category><![CDATA[angular 6 code review tool]]></category>
		<category><![CDATA[angular 7 code review checklist]]></category>
		<category><![CDATA[Angular ESLint]]></category>
		<category><![CDATA[Angular linting]]></category>
		<category><![CDATA[angular sonarqube coverage]]></category>
		<category><![CDATA[Angular TSLint]]></category>
		<category><![CDATA[Code analysis angular]]></category>
		<category><![CDATA[Code analysis React]]></category>
		<category><![CDATA[Code analysis Vue]]></category>
		<category><![CDATA[Code Quality of Angular]]></category>
		<category><![CDATA[code review checklist javascript]]></category>
		<category><![CDATA[Codelyzer vs Sonar  angular 8]]></category>
		<category><![CDATA[codelyzer Vs Sonar angular ]]></category>
		<category><![CDATA[codelyzer vs tslint vs SonarLint]]></category>
		<category><![CDATA[coding issues Angular]]></category>
		<category><![CDATA[command is used to run static code analysis of angular application]]></category>
		<category><![CDATA[Javascript linting]]></category>
		<category><![CDATA[node js static code analysis tools]]></category>
		<category><![CDATA[react code review checklist]]></category>
		<category><![CDATA[react code review tools]]></category>
		<category><![CDATA[react code smells]]></category>
		<category><![CDATA[react coding standards]]></category>
		<category><![CDATA[React ESLint]]></category>
		<category><![CDATA[React lint]]></category>
		<category><![CDATA[react native code review checklist]]></category>
		<category><![CDATA[React or Vue applications using Sonar.]]></category>
		<category><![CDATA[React quality]]></category>
		<category><![CDATA[reactjs code review checklist]]></category>
		<category><![CDATA[Sonar lint]]></category>
		<category><![CDATA[sonar lint vscode extension]]></category>
		<category><![CDATA[sonar-project.properties angular]]></category>
		<category><![CDATA[sonar-scanner for angular]]></category>
		<category><![CDATA[sonarqube angular 7]]></category>
		<category><![CDATA[sonarqube angular 8]]></category>
		<category><![CDATA[sonarqube angular code coverage]]></category>
		<category><![CDATA[sonarqube for angular projects]]></category>
		<category><![CDATA[sonarqube integration with angular]]></category>
		<category><![CDATA[static code analysis of angular application]]></category>
		<category><![CDATA[typescript coding best practices]]></category>
		<category><![CDATA[typescript static analysis]]></category>
		<category><![CDATA[Visual Studio sonar lint]]></category>
		<category><![CDATA[Vue ESLint]]></category>
		<category><![CDATA[Vue linting]]></category>
		<guid isPermaLink="false">https://thecodebuzz.com/?p=2398</guid>

					<description><![CDATA[<p>Continuous Code Quality UI (Angular, React, or Vue) using SonarLint Today in this article we shall see how to use the Sonar Lint IDE extension for VSCode and manage Code Quality of Angular, React, and Vue applications with Sonar Qube. Most of us understand the importance of code quality. Good coding practices are language agnostic [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/continuous-code-quality-in-angular-7-applications-using-sonar/">Sonar Qube – Code Quality of Angular, React, and Vue applications</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading"><strong>Continuous Code Quality UI (Angular, React, or Vue</strong>) <strong>using SonarLint</strong></h1>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="624" src="https://www.thecodebuzz.com/wp-content/uploads/2020/09/Code-Quality-of-Angular-React-Vue-applications-using-Sonar-1024x624.jpg" alt="Code Quality of Angular, React, Vue applications with Sonar Qube" class="wp-image-12361" srcset="https://thecodebuzz.com/wp-content/uploads/2020/09/Code-Quality-of-Angular-React-Vue-applications-using-Sonar-1024x624.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/09/Code-Quality-of-Angular-React-Vue-applications-using-Sonar-300x183.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/09/Code-Quality-of-Angular-React-Vue-applications-using-Sonar-768x468.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/09/Code-Quality-of-Angular-React-Vue-applications-using-Sonar-785x478.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2020/09/Code-Quality-of-Angular-React-Vue-applications-using-Sonar.jpg 1446w" sizes="(max-width: 1024px) 100vw, 1024px" /></figure>



<p>Today in this article we shall see how to use the Sonar Lint<strong> IDE </strong>extension for <strong>VSCode</strong> and manage Code Quality of Angular, React, and Vue applications with Sonar Qube.</p>



<p></p>



<p>Most of us understand the importance of code quality. </p>



<p></p>



<p>Good coding practices are language agnostic and help an organization deliver clean, highly reliable, secure, and maintainable code. </p>



<p></p>



<p>Today in this article, we shall cover the below aspects,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-getting-started-code-quality-of-angular-react-vue-applications-with-sonar">Getting started &#8211; Code Quality of Angular, React, and Vue applications with Sonar</a><ul><li><a href="#aioseo-prerequisites-and-configuration">Prerequisites and Configuration:</a><ul><li><a href="#aioseo-java-run-time-jre-8">Java Run time (JRE) 8</a></li><li><a href="#aioseo-install-sonar-lint-extension-in-vscode">Install Sonar Lint extension in VSCode</a></li><li><a href="#aioseo-setting-json-update">Setting.json update</a></li></ul></li></ul></li><li><a href="#aioseo-user-settings-sonar-connected-mode">User Settings Sonar Qube &#8211;  connected mode</a><ul><li><a href="#aioseo-connected-mode-in-user-settings">Connected Mode in User Settings</a></li></ul></li><li><a href="#aioseo-workspace-settings-sonar-connected-mode">Workspace settings Sonar  &#8211;  connected mode</a><ul><li><a href="#mce_54">Connected Mode in Workspace Settings</a></li><li><a href="#aioseo-summary">Summary :</a></li></ul></li></ul></div>



<p></p>



<p>Software code quality and its maintenance is like a big fat elephant and often eat away a big chunk of project budget allocation. Re-engineering and refactoring of buggy code cost organization time, cost, and quality. </p>



<p></p>



<p>These small-looking coding issues become bigger problems in the future and potentially affect the complete architecture and design of your application.</p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p> <strong>“Any fool can write code that a computer can understand. Good programmers write code that humans can understand” &nbsp;-Martin Fowler</strong></p>
</blockquote>



<p></p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><strong>“Code is maintained more often than it is written”</strong></p>
</blockquote>



<p></p>



<p></p>



<p><strong>SonarLint </strong>is an IDE extension that helps to detect and fix code quality issues as you type the code in IDE. </p>



<p></p>



<p><strong>SonarLint</strong> extension combines all the best of the linting process and supports multiple analyzers and multiple languages.  </p>



<p></p>



<ul class="wp-block-list">
<li><strong>Typescript &#8211; Sonar TS analyzer </strong></li>
</ul>



<ul class="wp-block-list">
<li><strong>JavaScript, </strong>React, Vue &#8211; SonarJS<strong>, ESLint  analyzer</strong></li>
</ul>



<p></p>



<p>SonarLint IDE extension is currently supported by the below <strong><em>IDE </em></strong>overall,</p>



<p></p>



<figure class="wp-block-image is-resized"><img decoding="async" width="1228" height="924" src="https://i0.wp.com/thecodebuzz.com/wp-content/uploads/2019/04/SonarLint-Vscode-IDE-extension-.jpg?fit=785%2C590&amp;ssl=1" alt="Angular sonar lint" class="wp-image-2399" style="width:542px;height:407px" srcset="https://thecodebuzz.com/wp-content/uploads/2019/04/SonarLint-Vscode-IDE-extension-.jpg 1228w, https://thecodebuzz.com/wp-content/uploads/2019/04/SonarLint-Vscode-IDE-extension--300x226.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2019/04/SonarLint-Vscode-IDE-extension--1024x771.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2019/04/SonarLint-Vscode-IDE-extension--768x578.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2019/04/SonarLint-Vscode-IDE-extension--691x520.jpg 691w" sizes="(max-width: 1228px) 100vw, 1228px" /></figure>



<p></p>



<p>Sonar defines coding issues in the below categories,</p>



<ul class="wp-block-list">
<li><strong>Bug</strong></li>



<li><strong>Code Smell</strong></li>



<li><strong>Vulnerability </strong></li>
</ul>



<p></p>



<p>Each above categories can be further classified based on severity as below,</p>



<p></p>



<ul class="wp-block-list">
<li><strong>Blocker</strong></li>



<li><strong>Critical </strong></li>



<li><strong>Major</strong></li>



<li><strong>Minor</strong></li>



<li><strong>Info</strong></li>
</ul>



<p></p>



<p>Let&#8217;s look into SonarLint VS Code plugin extension setup and configuration details.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-getting-started-code-quality-of-angular-react-vue-applications-with-sonar">Getting started &#8211; Code Quality of Angular, React, and Vue applications with Sonar</h2>



<p></p>



<h3 class="wp-block-heading" id="aioseo-prerequisites-and-configuration"><strong>Prerequisites and Configuration:</strong></h3>



<p></p>



<h4 class="wp-block-heading has-text-align-left" id="aioseo-java-run-time-jre-8">Java Run time (JRE) 8 </h4>



<p></p>



<p>Once Java run time is installed, please verify using the version using the below command,</p>



<p></p>



<figure class="wp-block-image is-resized"><img decoding="async" width="1117" height="208" src="https://i1.wp.com/thecodebuzz.com/wp-content/uploads/2019/04/image-20.png?fit=785%2C146&amp;ssl=1" alt="React sonar lint" class="wp-image-2401" style="width:560px;height:104px" srcset="https://thecodebuzz.com/wp-content/uploads/2019/04/image-20.png 1117w, https://thecodebuzz.com/wp-content/uploads/2019/04/image-20-300x56.png 300w, https://thecodebuzz.com/wp-content/uploads/2019/04/image-20-1024x191.png 1024w, https://thecodebuzz.com/wp-content/uploads/2019/04/image-20-768x143.png 768w, https://thecodebuzz.com/wp-content/uploads/2019/04/image-20-785x146.png 785w" sizes="(max-width: 1117px) 100vw, 1117px" /></figure>



<p>Further verification can be done by checking the below folder on your Windows machine,</p>



<p></p>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="719" height="124" src="https://www.thecodebuzz.com/wp-content/uploads/2019/04/image-21.png" alt="react code review checklist" class="wp-image-2402" srcset="https://thecodebuzz.com/wp-content/uploads/2019/04/image-21.png 719w, https://thecodebuzz.com/wp-content/uploads/2019/04/image-21-300x52.png 300w" sizes="auto, (max-width: 719px) 100vw, 719px" /></figure>



<p></p>



<h4 class="wp-block-heading" id="aioseo-install-sonar-lint-extension-in-vscode">Install Sonar Lint extension in VSCode</h4>



<p></p>



<figure class="wp-block-image is-resized"><img loading="lazy" decoding="async" width="2686" height="564" src="https://i1.wp.com/thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-vscode-extension-angular.png?fit=785%2C165&amp;ssl=1" alt="Vue sonar lint" class="wp-image-2403" style="width:562px;height:117px" srcset="https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-vscode-extension-angular.png 2686w, https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-vscode-extension-angular-300x63.png 300w, https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-vscode-extension-angular-1024x215.png 1024w, https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-vscode-extension-angular-768x161.png 768w, https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-vscode-extension-angular-1536x323.png 1536w, https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-vscode-extension-angular-2048x430.png 2048w, https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-vscode-extension-angular-785x165.png 785w" sizes="auto, (max-width: 2686px) 100vw, 2686px" /></figure>



<p></p>



<h4 class="wp-block-heading" id="aioseo-setting-json-update">Setting.json update</h4>



<p></p>



<p>Click on Settings -&gt;Edit in Settings.Json</p>



<p></p>



<figure class="wp-block-image is-resized"><img loading="lazy" decoding="async" width="1945" height="807" src="https://i1.wp.com/thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-vscode-extension-angular-settings-configuration-.jpg?fit=785%2C326&amp;ssl=1" alt="Javascript sonar lint" class="wp-image-2404" style="width:564px;height:233px" srcset="https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-vscode-extension-angular-settings-configuration-.jpg 1945w, https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-vscode-extension-angular-settings-configuration--300x124.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-vscode-extension-angular-settings-configuration--1024x425.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-vscode-extension-angular-settings-configuration--768x319.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-vscode-extension-angular-settings-configuration--1536x637.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-vscode-extension-angular-settings-configuration--785x326.jpg 785w" sizes="auto, (max-width: 1945px) 100vw, 1945px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-user-settings-sonar-connected-mode">User Settings Sonar Qube &#8211;  connected mode </h2>



<p></p>



<p>These settings will be global and will be applied at the VSCode IDE level and will be effective for all projects.</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">{
"sonarlint.ls.javaHome": "C:\Program Files\Java\jdk1.8.0_201"
}</pre>



<p></p>



<p>If connecting your Organization enterprise Sonar Server please add the below setting within<strong> <em>&#8220;sonarlint.connectedMode.servers&#8221;</em></strong></p>



<p></p>



<p><strong>Note:</strong></p>



<p><br><strong>SonarLint</strong> uses the same rule set as configured on the SonarQube server thereby streamlining the code analysis and avoiding any ambiguity of issues reported on server vs desktop. </p>



<p>If not connected to the Sonar Server default ruleset ( which comes with plugin installation) will be used.</p>



<p></p>



<h4 class="wp-block-heading" id="aioseo-connected-mode-in-user-settings">Connected Mode in User Settings</h4>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">"sonarlint.connectedMode.servers": [
 {
"serverId": "your server id if any",
"serverUrl": "https://your-org/server",//Your Organization server URL
"organizationKey": "my_organization",// Organization Key
"token": "V2VkIE1…" //Organization Auth token
 }
]</pre>



<p></p>



<h2 class="wp-block-heading" id="aioseo-workspace-settings-sonar-connected-mode">Workspace settings Sonar  &#8211;  connected mode  </h2>



<p></p>



<p>This setting will be specific to an individual project. This setting can be checked in along with your source project to SCM.</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">{<br>"sonarlint.ls.javaHome": "C:\Program Files\Java\jre1.8.0_201"<br>}</pre>



<p></p>



<h4 class="wp-block-heading" id="mce_54">Connected Mode in Workspace Settings</h4>



<p></p>



<p>If connecting to your organization&#8217;s Sonar Server additionally add below within workspace settings.</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">"sonarlint.connectedMode.project": {
  "serverId": "your server id if any",
  "projectKey": "your project name in sonar server"
}</pre>



<p></p>



<p>Once done with the above settings, you shall be all set to use Sonar linting. The output windows will display that SonarTS Server and the engine have started successfully. </p>



<p></p>



<p>If not connected to the server,  Sonar Lint will launch the Sonar server( with different analyzers) locally by opening a local port and will use the default rule set in such a case. </p>



<p></p>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="2560" height="673" src="https://i0.wp.com/thecodebuzz.com/wp-content/uploads/2019/04/SoanrTS-server-conenction-issue-resolution-1-2.jpg?fit=785%2C206&amp;ssl=1" alt="Sonarlint connected mode Vscode" class="wp-image-2416" srcset="https://thecodebuzz.com/wp-content/uploads/2019/04/SoanrTS-server-conenction-issue-resolution-1-2.jpg 2560w, https://thecodebuzz.com/wp-content/uploads/2019/04/SoanrTS-server-conenction-issue-resolution-1-2-300x79.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2019/04/SoanrTS-server-conenction-issue-resolution-1-2-1024x269.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2019/04/SoanrTS-server-conenction-issue-resolution-1-2-768x202.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2019/04/SoanrTS-server-conenction-issue-resolution-1-2-1536x404.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2019/04/SoanrTS-server-conenction-issue-resolution-1-2-2048x538.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2019/04/SoanrTS-server-conenction-issue-resolution-1-2-785x206.jpg 785w" sizes="auto, (max-width: 2560px) 100vw, 2560px" /></figure>



<p></p>



<p>If using Sonar Server in &#8220;Connected&#8221; mode kindly run the below command from the Command pallet,</p>



<p></p>



<pre class="wp-block-preformatted has-medium-font-size">Update SonarLint binding to SonarQube/SonarCloud </pre>



<p></p>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="1800" height="325" src="https://i2.wp.com/thecodebuzz.com/wp-content/uploads/2019/04/image-22.jpg?fit=785%2C142&amp;ssl=1" alt="Vscode Sonarlint " class="wp-image-2423" srcset="https://thecodebuzz.com/wp-content/uploads/2019/04/image-22.jpg 1800w, https://thecodebuzz.com/wp-content/uploads/2019/04/image-22-300x54.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2019/04/image-22-1024x185.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2019/04/image-22-768x139.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2019/04/image-22-1536x277.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2019/04/image-22-785x142.jpg 785w" sizes="auto, (max-width: 1800px) 100vw, 1800px" /></figure>



<p>As you type your code you shall be able to see the best recommendation from Sonar lint based on configured rules. </p>



<p></p>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="2033" height="652" src="https://i0.wp.com/thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-rules-configuration-window.jpg?fit=785%2C251&amp;ssl=1" alt="Sonarlint connected mode setting" class="wp-image-2476" srcset="https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-rules-configuration-window.jpg 2033w, https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-rules-configuration-window-300x96.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-rules-configuration-window-1024x328.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-rules-configuration-window-768x246.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-rules-configuration-window-1536x493.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2019/04/sonarlint-rules-configuration-window-785x252.jpg 785w" sizes="auto, (max-width: 2033px) 100vw, 2033px" /></figure>



<p></p>



<p>As we mentioned above Sonar lint uses the same rules set as configured on the SonarQube server and streamlines the code analysis by avoiding any ambiguity of reports on server vs desktop. </p>



<p></p>



<figure class="wp-block-image"><img loading="lazy" decoding="async" width="700" height="418" src="https://www.thecodebuzz.com/wp-content/uploads/2019/04/Continuous-Code-Quality-of-Angular-React-or-Vue-applications-using-Sonar.gif" alt="Sonarlint connected mode setting" class="wp-image-2639"/></figure>



<p></p>



<p>For server-side reporting SonarQube need to be used which is a licensed and enterprise version. </p>



<p></p>



<p>Sonar scan can be configured as part build and deploy process for the CI-CD pipeline. </p>



<p></p>



<p><strong><em>SonarQube </em></strong>provides the overall health of the portfolio within the organization with nice dashboards detailing code health.   </p>



<p></p>



<p>Happy coding !!</p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<p></p>



<p><strong><em>References: </em></strong></p>



<p></p>



<ul class="wp-block-list" id="block-ab9b38d2-be73-4989-8171-39a3b3b0203e">
<li><a href="https://www.thecodebuzz.com/static-code-quality-analysis-ndepend/" target="_blank" rel="noreferrer noopener"><strong><em>Continuous Code Quality using NDepend</em></strong></a></li>
</ul>



<p></p>



<ul class="wp-block-list" id="block-ab9b38d2-be73-4989-8171-39a3b3b0203e">
<li><a href="https://www.thecodebuzz.com/continuous-code-quality-of-using-sonar/" target="_blank" rel="noreferrer noopener" title="Continuous Code Quality of .NET with SonarQube"><strong><em>Code Quality of .NET Application using Sonar</em></strong></a></li>
</ul>



<p></p>



<h4 class="wp-block-heading" id="aioseo-summary">Summary : </h4>



<p></p>



<p>Today we learned about Sonar lint setup and configuration for Visual Studio Code IDE. <strong>Good coding practices are language agnostics and help the organization to deliver clean, highly reliable, secure, and maintainable projects.</strong> Buggy code slipping through the cracks has the potential to cost a company an entire business. Organizations can address these problems by following best practices of software development like continuous code quality and continuous code review processes.</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/continuous-code-quality-in-angular-7-applications-using-sonar/">Sonar Qube – Code Quality of Angular, React, and Vue applications</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/continuous-code-quality-in-angular-7-applications-using-sonar/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>React &#8211; Pass Data From One Component to Other Component</title>
		<link>https://thecodebuzz.com/react-pass-data-parent-child-component-redux-props-hooks/</link>
					<comments>https://thecodebuzz.com/react-pass-data-parent-child-component-redux-props-hooks/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Tue, 15 Jun 2021 17:01:00 +0000</pubDate>
				<category><![CDATA[React]]></category>
		<category><![CDATA[how to pass data from one component to another in react js using hooks]]></category>
		<category><![CDATA[pass data from child to parent react functional component]]></category>
		<category><![CDATA[pass data from one component to another on button click react]]></category>
		<category><![CDATA[pass data from one component to another react native]]></category>
		<category><![CDATA[pass data from one component to another react-router]]></category>
		<category><![CDATA[pass object to another component react]]></category>
		<category><![CDATA[pass value from one component to another angular]]></category>
		<category><![CDATA[passing data from one component to another angular 8]]></category>
		<guid isPermaLink="false">https://thecodebuzz.com/?p=11074</guid>

					<description><![CDATA[<p>React &#8211; Pass Data From Child to Parent and Parent to Child Component Today in this article we shall learn how to perform component interactions in React UI framework application. You may find a need to Pass Data From One Component to another Component including React &#8211; Pass Data From One Component to Other Component [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/react-pass-data-parent-child-component-redux-props-hooks/">React – Pass Data From One Component to Other Component</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">React &#8211; Pass Data From Child to Parent and Parent to Child Component</h1>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/React-Parent-to-child-Component-and-Child-to-Parent-compoent-communication.jpg" alt="React - Pass Data From One Component to Other Component,react child to parent communication" class="wp-image-11092" width="528" height="381" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/React-Parent-to-child-Component-and-Child-to-Parent-compoent-communication.jpg 631w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-Parent-to-child-Component-and-Child-to-Parent-compoent-communication-300x216.jpg 300w" sizes="auto, (max-width: 528px) 100vw, 528px" /></figure>



<p>Today in this article we shall learn how to perform component interactions in React UI framework application. You may find a need to <strong>Pass Data From One Component to another Component including React &#8211; Pass Data From One Component to Other Component</strong> <strong>with Child to Parent Component from parent to child communication</strong></p>



<p></p>



<p>We shall cover below basic aspects in today&#8217;s article,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-getting-started">Getting Started</a></li><li><a href="#aioseo-what-is-props-system">What is Props System</a></li><li><a href="#aioseo-create-parent-component---usercomponent">Create Parent Component &#8211; UserComponent</a></li><li><a href="#aioseo-create-child-component---moviecomponent">Create Child Component &#8211; MovieComponent</a></li><li><a href="#aioseo-naming-pattern-for-props">Naming Pattern for Props</a></li><li><a href="#aioseo-react-parent-to-child-component-using-props">React Parent to Child Component using Props</a></li><li><a href="#aioseo-summary">Summary</a></li></ul></div>



<p>Before we get started I am assuming you already have a basic understanding of React applications.</p>



<p></p>



<p>If not, kindly go through a series of articles on React,</p>



<p></p>



<ul class="wp-block-list"><li><a href="https://www.thecodebuzz.com/getting-started-with-react/" target="_blank" rel="noreferrer noopener"><strong><em>Getting started with React</em></strong></a></li></ul>



<ul class="wp-block-list"><li><a href="https://www.thecodebuzz.com/react-components-with-state-and-props-best-practices/" target="_blank" rel="noreferrer noopener"><strong><em>Understanding Component with State and Props</em></strong></a></li></ul>



<p></p>



<br>



<h2 class="wp-block-heading" id="aioseo-getting-started">Getting Started </h2>



<p></p>



<p>Let&#8217;s get started step by step from basics to advanced topics.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-what-is-props-system">What is Props System</h2>



<p></p>



<p><strong>Props are actually a short form of Properties</strong>. It&#8217;s a mechanism of passing the data from Parent components to child components. Using <strong>Props </strong>data must flow uni-directionally.</p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Using <strong>Props </strong>data must flow uni-directionally i.e Parent to Child only</p></blockquote>



<p></p>



<p>This data transfer happens in two stages. In the first stage, we provide information from the parent to the child components.</p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Parent-Component-to-child-Component.jpg" alt="react child to parent communication,react pass data from child to parent" class="wp-image-11083" width="525" height="588" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Parent-Component-to-child-Component.jpg 554w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Parent-Component-to-child-Component-268x300.jpg 268w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Parent-Component-to-child-Component-464x520.jpg 464w" sizes="auto, (max-width: 525px) 100vw, 525px" /></figure>



<p></p>



<p>So let&#8217;s first focus on how we provide information from the parent to the child and then in the second stage we will see how the Child Component consumes or makes use of that information.</p>



<p></p>



<p>I shall be using very basic two components defined as below,</p>



<p></p>



<ul class="wp-block-list"><li>Parent Component &#8211; <strong><em>UserComponent</em></strong></li><li>Child Component &#8211; <strong><em>MovieComponent</em></strong></li></ul>



<p></p>



<h2 class="wp-block-heading" id="aioseo-create-parent-component---usercomponent">Create Parent Component &#8211; <strong><em>UserComponent</em></strong></h2>



<p></p>



<p>Let&#8217;s create a simple Parent Component &#8211; <strong><em>UserComponent</em></strong> as below,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
class UserComponent extends React.Component {

  render() {
    return (
      &lt;div className=&quot;content&quot;&gt;
        Hi There, UserComponent
      &lt;/div&gt;
    );
  }
}

export default UserComponent;


</pre></div>


<p></p>



<h2 class="wp-block-heading" id="aioseo-create-child-component---moviecomponent">Create Child Component &#8211; <strong><em>MovieComponent</em></strong></h2>



<p></p>



<p>Similarly, Let&#8217;s create a simple Child Component &#8211; <strong><em><strong><em>MovieComponent</em></strong></em></strong> as below,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
class MovieComponent extends React.Component {
  render() {

    return (
      &lt;div className=&quot;content&quot;&gt;
        Hi There, MovieComponent
      &lt;/div&gt;
    );
  }
}

export default MovieComponent;
</pre></div>


<p></p>



<p>Now, these components are currently independent and there is no parent-child relationship so far,</p>



<p></p>



<p>Below is<strong><em> index.json</em></strong> referencing them individually,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
ReactDOM.render(
  &lt;React.StrictMode&gt;

    &lt;UserComponent/&gt;
    &lt;MovieComponent/&gt;

  &lt;/React.StrictMode&gt;,
  document.getElementById(&#039;root&#039;)
);

</pre></div>


<p></p>



<p>Once you launch the app, you shall see both components execute and produce the below result,</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Parent-to-child-Component-1024x333.jpg" alt="React Child to Parent data transfer" class="wp-image-11085" width="450" height="146" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Parent-to-child-Component-1024x333.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Parent-to-child-Component-300x98.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Parent-to-child-Component-768x250.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Parent-to-child-Component-1536x500.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Parent-to-child-Component-785x255.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Parent-to-child-Component.jpg 1736w" sizes="auto, (max-width: 450px) 100vw, 450px" /></figure>



<p>So far so good !!</p>



<p></p>



<p>Let&#8217;s now define the relationship which Parent to Child relationship!</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-naming-pattern-for-props">Naming Pattern for Props</h2>



<p></p>



<p>Before doing that let&#8217;s understand the naming convention and patterns that needs to be used while using props in the component.</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="761" height="234" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/React-What-is-Props.jpg" alt="React - Pass Data From parent to child, react child to parent communication" class="wp-image-11086" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/React-What-is-Props.jpg 761w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-What-is-Props-300x92.jpg 300w" sizes="auto, (max-width: 761px) 100vw, 761px" /></figure>



<p></p>



<p>Lets&#8217; see a few examples,</p>



<p></p>



<p><strong>Example</strong>,</p>



<pre class="wp-block-verse"><em>&lt;ChildComponent  userName ="TheCodeBuzz"/&gt;</em></pre>



<pre class="wp-block-verse"><em>&lt;MovieComponent movieName ="Animators"/&gt;</em></pre>



<p></p>



<h2 class="wp-block-heading" id="aioseo-react-parent-to-child-component-using-props"><strong>React <em>Parent to Child Component</em> <em>using Props</em></strong></h2>



<p></p>



<p>Let&#8217;s update the above-discussed component for Parent to Child communication. Our goal here would be to transfer data from Parent Component <strong><em>UserComponent</em></strong> to the Child component i.e <em><strong>MovieComponent</strong></em></p>



<p></p>



<p>Update code on Parent as below,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
class UserComponent extends React.Component {

  render() {
    return (
      &lt;div className=&quot;content&quot;&gt;

        &lt;div&gt; Hi There, UserComponent&lt;/div&gt;

        &lt;MovieComponent movie=&quot;Animators&quot; /&gt;

      &lt;/div&gt;
    );
  }
}

export default UserComponent;
</pre></div>


<p></p>



<p>So what we did above as below,</p>



<p></p>



<ul class="wp-block-list"><li>Above we have defined our <em><strong>Props</strong></em> name as <em><strong>movie </strong></em>to pass the data to the child component.</li></ul>



<ul class="wp-block-list"><li><em><strong>UserComponent</strong></em> should be able to pass the movie object which has a value of &#8220;<em><strong>Animators&#8221;</strong></em> defined using the expression <em><span class="has-inline-color has-pale-pink-color">MovieComponent movie=&#8221;Animators&#8221;</span></em></li></ul>



<p></p>



<p>Similarly, we shall be retrieving the same <em><strong>Props</strong></em> i.e <em><strong>movie </strong></em>in the Child component as below,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
class MovieComponent extends React.Component {
  render() {

    return (
      &lt;div className=&quot;content&quot;&gt;
        &lt;div&gt;Hi There, MovieComponent&lt;/div&gt;

        Movie selected by User: {this.props.movie}

      &lt;/div&gt;
    );
  }
}

export default MovieComponent;

</pre></div>


<p></p>



<p>So what we did above as below,</p>



<p></p>



<ul class="wp-block-list"><li>Child consumes or makes use of Parent information using Props</li></ul>



<ul class="wp-block-list"><li>Retrieves the same <em><strong>Props</strong></em> i.e <em><strong>movie </strong></em>in the Child component</li></ul>



<p></p>



<p>Once executed, the app shall below result on the UI,</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/React-Pass-Data-From-Child-to-Parent-component-1024x414.jpg" alt="pass data from child to parent react" class="wp-image-11088" width="472" height="190" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/React-Pass-Data-From-Child-to-Parent-component-1024x414.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-Pass-Data-From-Child-to-Parent-component-300x121.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-Pass-Data-From-Child-to-Parent-component-768x311.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-Pass-Data-From-Child-to-Parent-component-785x317.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-Pass-Data-From-Child-to-Parent-component.jpg 1338w" sizes="auto, (max-width: 472px) 100vw, 472px" /></figure>



<p>That&#8217;s All!</p>



<p></p>



<p>I have discussed Child to Parent communication in the below article,</p>



<p></p>



<p><strong><em>References </em></strong></p>



<p></p>



<ul class="has-medium-font-size wp-block-list"><li><a href="https://www.thecodebuzz.com/react-passing-data-from-child-component-to-parent-component/" target="_blank" rel="noreferrer noopener" title="React- Passing Data From Child Component to Parent Component"><strong><em>React- Child to </em></strong></a><a href="https://www.thecodebuzz.com/react-passing-data-from-child-component-to-parent-component/" target="_blank" rel="noreferrer noopener" title="React- Passing Data From Child Component to Parent Component"><strong><em>Parent </em></strong></a><a href="https://www.thecodebuzz.com/react-passing-data-from-child-component-to-parent-component/" target="_blank" rel="noreferrer noopener" title="React- Passing Data From Child Component to Parent Component"><strong><em>communication</em></strong></a></li></ul>



<p></p>



<p>I shall be covering Redux Architecture and its usage in my next article! Until then stay tuned!</p>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-summary">Summary </h2>



<p></p>



<p>Today in this article we understood how Component interacts in React programming. We understood the<strong><em> prop system and understood the naming pattern</em></strong> and used it for passing data from Parent to Child.</p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/react-pass-data-parent-child-component-redux-props-hooks/">React – Pass Data From One Component to Other Component</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/react-pass-data-parent-child-component-redux-props-hooks/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>React &#8211; Child To Parent Component Communication</title>
		<link>https://thecodebuzz.com/react-passing-data-from-child-component-to-parent-component/</link>
					<comments>https://thecodebuzz.com/react-passing-data-from-child-component-to-parent-component/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 09 Aug 2020 20:12:00 +0000</pubDate>
				<category><![CDATA[React]]></category>
		<category><![CDATA[child to parent communication in react functional component]]></category>
		<category><![CDATA[get value from child component react]]></category>
		<category><![CDATA[how do we access the parent component in a child and vice versa in react]]></category>
		<category><![CDATA[pass data from child component to parent component react hooks]]></category>
		<category><![CDATA[pass data from child to parent react class component]]></category>
		<category><![CDATA[pass data from child to parent react functional component typescript]]></category>
		<category><![CDATA[pass state from parent to child react]]></category>
		<category><![CDATA[react parent child component example]]></category>
		<guid isPermaLink="false">https://thecodebuzz.com/?p=11098</guid>

					<description><![CDATA[<p>React &#8211; Passing Data From Child Component To Parent Component communication Today in this article we shall learn how to perform component interactions in the react application where we shall see React &#8211; Child To Parent Component communication and passing data from the Parent component to the Child Component. In our last article on Passing [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/react-passing-data-from-child-component-to-parent-component/">React – Child To Parent Component Communication</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">React &#8211; Passing Data From Child Component To Parent Component communication</h1>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/React-Parent-to-child-Component-and-Child-to-Parent-compoent-communication.jpg" alt="react child to parent communication" class="wp-image-11092" width="528" height="381" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/React-Parent-to-child-Component-and-Child-to-Parent-compoent-communication.jpg 631w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-Parent-to-child-Component-and-Child-to-Parent-compoent-communication-300x216.jpg 300w" sizes="auto, (max-width: 528px) 100vw, 528px" /></figure>



<p>Today in this article we shall learn how to perform component interactions in the react application where we shall see  React &#8211; Child To Parent Component communication and<strong> </strong>passing data from the <strong><em>Parent component</em></strong> to the <strong><em>Child Component.</em></strong></p>



<p></p>



<p>In our last article on<a href="https://www.thecodebuzz.com/react-pass-data-parent-child-component-redux-props-hooks/" target="_blank" rel="noreferrer noopener" title="React – Pass Data From One Component to Other Component"> <strong><em>Passing Data From Parent to Child Component</em></strong></a>, we already learned how to pass data using the <em><strong>Props system </strong></em>from a Parent Component to Child Component easily.</p>



<p></p>



<p>We shall cover below basic aspects in today&#8217;s article,</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-getting-started">Getting Started</a></li><li><a href="#aioseo-create-parent-component---loginusercomponent">Create Parent Component &#8211; LoginUserComponent</a></li><li><a href="#aioseo-react-create-child-component---selectmoviecomponent">React Create Child Component &#8211; SelectMovieComponent</a></li><li><a href="#aioseo-update-parent-state-object-with-child-data">Update Parent State object with Child data</a></li><li><a href="#aioseo-what-is-redux-and-what-use-cases-around-its-usage">What is Redux and what use cases around its usage?</a></li><li><a href="#aioseo-i-shall-be-covering-redux-usage-and-when-to-use-it-and-when-not-in-my-next-article-until-then-please-stay-tuned">I shall be covering Redux usage and when to use it and when not in my next article! Until then please stay tuned!</a></li><li><a href="#aioseo-summary">Summary</a></li></ul></div>



<p></p>



<p>Before we get started,  I am assuming you already have a basic understanding of React applications.</p>



<p></p>



<p>If not, kindly go through a series of articles on React,</p>



<p></p>



<ul class="wp-block-list"><li><strong><em><a href="https://www.thecodebuzz.com/react-pass-data-parent-child-component-redux-props-hooks/" target="_blank" rel="noreferrer noopener">Passing data from Parent to Child Componentusing Props</a></em></strong></li></ul>



<ul class="wp-block-list"><li><strong><em><a href="https://www.thecodebuzz.com/react-components-with-state-and-props-best-practices/" target="_blank" rel="noreferrer noopener">Understanding Component with State and Props -React Best Practices</a></em></strong></li></ul>



<p></p>



<h2 class="wp-block-heading" id="aioseo-getting-started">Getting Started </h2>



<p></p>



<p>Let&#8217;s get started step by step to perform data transfer from the Child to the Parent component </p>



<p></p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-child-Component-to-Parent-Component.jpg" alt="react child to parent communication" class="wp-image-11106" width="300" height="396" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-child-Component-to-Parent-Component.jpg 472w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-child-Component-to-Parent-Component-227x300.jpg 227w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-child-Component-to-Parent-Component-394x520.jpg 394w" sizes="auto, (max-width: 300px) 100vw, 300px" /></figure>



<p></p>



<p>So initially we shall be focussing on how we provide information from the Child to the Parent.</p>



<p>Then In the second stage, we shall see how to consume data from the Parent components.</p>



<p></p>



<p>I shall be using very basic two components defined as below</p>



<p></p>



<ul class="wp-block-list"><li>Parent Component &#8211; <strong><em>LoginUserComponent</em></strong></li><li>Child Component &#8211; <strong><em>SelectMovieComponent</em></strong></li></ul>



<p></p>



<h2 class="wp-block-heading" id="aioseo-create-parent-component---loginusercomponent">Create Parent Component &#8211; <strong><em>LoginUserComponent</em></strong></h2>



<p></p>



<p>Let&#8217;s create a simple Parent Component &#8211; <strong><em>UserComponent</em></strong> as below,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
class LoginUserComponent extends React.Component {
  onSearchSubmit(movie) {
    console.log(&#039;Input received in Parent Component&#039;);
    console.log(movie);
  }
  render() {
    return (
      &lt;div className=&quot;ui container&quot;&gt;
        &lt;SelectMovieComponent onSubmit= { this.onSearchSubmit } /&gt;
      &lt;/div&gt;
    );
  }
}
export default LoginUserComponent;


</pre></div>


<p>Above we have used the <strong><em>SelectMovieComponent </em></strong>using a naming pattern as we discussed in our last article on<em><strong> <a href="https://www.thecodebuzz.com/react-pass-data-parent-child-component-redux-props-hooks/" title="React – Pass Data From One Component to Other Component" target="_blank" rel="noreferrer noopener">Props system</a></strong></em>,</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="761" height="234" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/React-What-is-Props-1.jpg" alt="React- Passing Data From Child Component to Parent Component" class="wp-image-11108" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/React-What-is-Props-1.jpg 761w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-What-is-Props-1-300x92.jpg 300w" sizes="auto, (max-width: 761px) 100vw, 761px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-react-create-child-component---selectmoviecomponent">React Create Child Component &#8211; <strong><em>SelectMovieComponent</em></strong></h2>



<p></p>



<p>Similarly, Let&#8217;s create a simple Child Component &#8211; <strong><em><strong><em>SelectMovieComponent</em></strong></em></strong> as below,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
class SelectMovieComponent extends React.Component {

  state = { movieSelected: &#039;&#039; };

  onMoveSelected = (event) =&gt; {
    event.preventDefault();
    this.props.onSubmit(this.state.movieSelected);
    console.log(this.state.movieSelected);
  }
  render() {
    return (
      &lt;div className=&quot;ui segment&quot;&gt;
        &lt;form onSubmit={this.onMoveSelected} className=&quot;ui form&quot;&gt;
          &lt;div className=&quot;field&quot;&gt;
            &lt;label&gt;Movie Search&lt;/label&gt;
            &lt;input
              type=&quot;text&quot;
              onChange={e =&gt; this.setState({ movieSelected: e.target.value })}
              value={this.state.movieSelected}
            /&gt;
            &lt;div&gt;
              Movie selected by User: {this.state.movieSelected}
            &lt;/div&gt;
          &lt;/div&gt;
        &lt;/form&gt;
      &lt;/div&gt;
    );
  }
}

export default SelectMovieComponent;

</pre></div>


<p></p>



<p>Above in Child Component  &#8211; <strong><em>SelectMovieComponent</em></strong> we performed the following,</p>



<ul class="wp-block-list"><li>Create a Class-Based component <strong><em>SelectMovieComponent</em></strong></li><li>Create a State object <strong><em>movieSelected</em></strong></li><li>Update State object <strong><em>movieSelected</em></strong> in the <strong><em>OnChange </em></strong>event with the current value</li><li>Submit the Form by calling <em><strong>onMovieSelected</strong></em></li><li>Update the props with the same name as indicated in the Parent i.e using <span class="has-inline-color has-vivid-green-cyan-color"><em> </em></span><em><span class="has-inline-color has-luminous-vivid-orange-color"><strong>this.props.onSubmit(this.state.movieSelected);</strong></span></em></li></ul>



<p></p>



<p></p>



<p>Once you launch the app, you shall see both the components execute and produces the below result on the <strong><em>UI</em></strong>,</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="227" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/React-Pass-Data-From-Child-Component-to-Parent-Components-in-React-1-1024x227.jpg" alt="React - Child To Parent Component communication" class="wp-image-11112" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/React-Pass-Data-From-Child-Component-to-Parent-Components-in-React-1-1024x227.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-Pass-Data-From-Child-Component-to-Parent-Components-in-React-1-300x66.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-Pass-Data-From-Child-Component-to-Parent-Components-in-React-1-768x170.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-Pass-Data-From-Child-Component-to-Parent-Components-in-React-1-1536x340.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-Pass-Data-From-Child-Component-to-Parent-Components-in-React-1-2048x453.jpg 2048w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-Pass-Data-From-Child-Component-to-Parent-Components-in-React-1-785x174.jpg 785w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>Above we console printed the result from the <strong><em>Child into the Parent component.</em></strong></p>



<p></p>



<p>Let&#8217;s now enhance and use the data in Parent as a state object.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-update-parent-state-object-with-child-data">Update Parent State object with Child data</h2>



<p></p>



<p>Please update the Parent code to receive Child data into the state object of the Parent component as below,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: jscript; title: ; notranslate">
class LoginUserComponent extends React.Component {

  state = { movieReceived: &#039;&#039; };

  onSearchSubmit(movie) {
    console.log(&#039;Input received in Parent Component&#039;);
    console.log(movie);
    this.setState({movieReceived:movie})
  }
  render() {
    return (
      &lt;div className=&quot;ui segment&quot;&gt;
        &lt;div className=&quot;ui container&quot;&gt;
          &lt;SelectMovieComponent onSubmit={this.onSearchSubmit.bind(this)} /&gt;
        &lt;/div&gt;
        &lt;div&gt;
          Movie received by User(parent): {this.state.movieReceived}
        &lt;/div&gt;
      &lt;/div&gt;
    );
  }
}

</pre></div>


<p></p>



<p>Kindly note call-back method is updated with the <em><strong>bind(this) </strong></em>object.</p>



<p></p>



<p>Once you execute you shall see the result in the browser as below,</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Child-to-Parent-Component-1024x347.jpg" alt="React Child Component to Parent" class="wp-image-11175" width="568" height="192" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Child-to-Parent-Component-1024x347.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Child-to-Parent-Component-300x102.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Child-to-Parent-Component-768x260.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Child-to-Parent-Component-1536x520.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Child-to-Parent-Component-785x266.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-How-to-Pass-Data-From-Child-to-Parent-Component.jpg 1557w" sizes="auto, (max-width: 568px) 100vw, 568px" /></figure>



<p></p>



<p>That&#8217;s All!</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-what-is-redux-and-what-use-cases-around-its-usage">What is Redux and what use cases around its usage?</h2>



<p></p>



<p>Once we go beyond and master component communication, you might need to think of proper state management in your application (especially if dealing with large enterprise applications).  </p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-i-shall-be-covering-redux-usage-and-when-to-use-it-and-when-not-in-my-next-article-until-then-please-stay-tuned">I shall be covering <em><strong>Redux </strong></em>usage and when to use it and when not in my next article! Until then please stay tuned!</h2>



<p></p>



<p><strong><em>References:</em></strong></p>



<p></p>



<ul class="wp-block-list"><li><a href="https://www.thecodebuzz.com/react-pass-data-parent-child-component-redux-props-hooks/" target="_blank" rel="noreferrer noopener"><strong><em>React Passing Data From Parent to Child Component</em></strong></a></li></ul>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-summary">Summary </h2>



<p></p>



<p>Today in this article we understood further Component communication where we performed <strong>Child </strong>to <strong>Parent </strong>communication in React programming. We understood the <strong><em>props </em></strong>system and understood the naming pattern and also used the <strong>State </strong>object and <em><strong>callback </strong></em>event for passing data from Child to Parent easily.</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/react-passing-data-from-child-component-to-parent-component/">React – Child To Parent Component Communication</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/react-passing-data-from-child-component-to-parent-component/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>React – Using Fetch HTTP POST Request Examples</title>
		<link>https://thecodebuzz.com/react-fetch-http-post-request-state-hooks-examples/</link>
					<comments>https://thecodebuzz.com/react-fetch-http-post-request-state-hooks-examples/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Wed, 10 Jun 2020 23:01:35 +0000</pubDate>
				<category><![CDATA[React]]></category>
		<category><![CDATA[post request react axios]]></category>
		<category><![CDATA[React – Using Fetch HTTP POST Request Examples]]></category>
		<category><![CDATA[react post form data to api]]></category>
		<category><![CDATA[react post request on button click]]></category>
		<category><![CDATA[react post request with parameters]]></category>
		<category><![CDATA[react-fetch api]]></category>
		<category><![CDATA[reactjs fetch api example]]></category>
		<category><![CDATA[send post request from react to node]]></category>
		<guid isPermaLink="false">https://thecodebuzz.com/?p=11025</guid>

					<description><![CDATA[<p>React – Using Fetch HTTP POST Request Examples In this article, we shall see how to write React – POST request with easy to understand examples. Below are the high-level steps which can be performed to be able to use Http services in React application, Create a&#160;React Component &#8211; Function-based and Class-based Fetch POST API [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/react-fetch-http-post-request-state-hooks-examples/">React – Using Fetch HTTP POST Request Examples</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h1 class="wp-block-heading">React – Using Fetch HTTP POST Request Examples</h1>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="414" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/React-fetch-post-request-example-Hooks-State-objects-1024x414.jpg" alt="React – Using Fetch HTTP POST Request Examples" class="wp-image-11030" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/React-fetch-post-request-example-Hooks-State-objects-1024x414.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-fetch-post-request-example-Hooks-State-objects-300x121.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-fetch-post-request-example-Hooks-State-objects-768x311.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-fetch-post-request-example-Hooks-State-objects-785x318.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-fetch-post-request-example-Hooks-State-objects.jpg 1048w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<p>In this article, we shall see how to write React – POST request with easy to understand examples.</p>



<p></p>



<p>Below are the high-level steps which can be performed to be able to use Http services in React application,</p>



<p></p>



<ul class="wp-block-list"><li><strong>Create a&nbsp;<em>React Component</em> </strong>&#8211; <strong>Function-based and Class-based </strong></li><li><strong><em>Fetch </em> POST API using State </strong></li><li><strong><em>Fetch</em> POST API using StateHooks</strong></li><li><strong>Performing POST operation</strong></li></ul>



<p></p>



<p>Before we get started I am assuming you already have a basic understanding of React application.</p>



<p></p>



<p>If not, kindly go through a series of articles on React,</p>



<ul class="wp-block-list"><li><a href="https://www.thecodebuzz.com/getting-started-with-react/" target="_blank" rel="noreferrer noopener" title="Getting Started with React">Getting started with React</a></li><li><a href="https://www.thecodebuzz.com/react-components-with-state-and-props-best-practices/" target="_blank" rel="noreferrer noopener" title="React Components with State and Props – Best Practices">Understanding Component with State and Props</a></li><li><a href="https://www.thecodebuzz.com/react-js-http-get-put-post-delete-request-hooks/" target="_blank" rel="noreferrer noopener" title="React – HTTP GET, PUT, POST and DELETE Request Examples">React -Fetch HTTP GET example</a></li></ul>



<p></p>



<p>We shall see both approaches like using <em><strong>Function-based</strong></em> Component and <strong><em>Class-based component</em></strong></p>



<p></p>



<h2 class="wp-block-heading">Getting Started</h2>



<p></p>



<p>Lets create your new React.js Application.</p>



<p>As we saw in our last article&nbsp;<a href="https://www.thecodebuzz.com/getting-started-with-react/" target="_blank" rel="noreferrer noopener">Getting started with React</a>&nbsp;, we used a function-based component where we rendered the UI with ‘Hello World’.</p>



<p></p>



<p>Here is below example of&nbsp;<em><strong>class based Component</strong></em>,</p>



<p></p>



<p></p>



<h2 class="wp-block-heading">Fetch POST example &#8211; Using State Object </h2>



<p></p>



<p>Let’s add new&nbsp;CustomHttpRequestPost as below. CustomHttpRequestPostHooks is a class-based component and works with Sate objects easily.</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
import React from &#039;react&#039;;
import logo from &#039;./logo.svg&#039;;
import &#039;./App.css&#039;;

class CustomHttpRequestPost extends React.Component {

  constructor(props) {
    super(props)
    this.state = {
      date: new Date(),
      name: null,
      id: null,
    }
  }

  render() {
    const { name } = this.state;
    const { id } = this.state;
    return (
      &amp;lt;div className=&quot;App&quot;&gt;/
        &amp;lt;header className=&quot;App-header&quot;&gt;
          &amp;lt;img src={logo} className=&quot;App-logo&quot; alt=&quot;logo&quot; /&gt;
          &amp;lt;p&gt;
            Hello, TheCodeBuzz Community }
          &amp;lt;/p&gt;
          &amp;lt;div className=&quot;card-body&quot;&gt;
            &amp;lt;p&gt;Name: {name}&amp;lt;/p&gt;
            &amp;lt;p&gt;Id: {id} &amp;lt;/p&gt;
          &amp;lt;/div&gt;
        &amp;lt;/header&gt;
      &amp;lt;/div&gt;
    );
  }
}

export default CustomHttpRequestPost;

</pre></div>


<p></p>



<p>In the above example, we shall be updating the State objects like <strong><em>name </em></strong>and <strong><em>id </em></strong>using HTTP GET API response.</p>



<p></p>



<p>We shall be using <em><strong>componentDidMount</strong></em> to call fetch API and we shall re-render the UI with latest values.</p>



<p>Lets define FETCH for POST in <strong><em>componentDidMount </em></strong>a life cycle method,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
componentDidMount() {

      fetch(&#039;https://localhost:5001/api/values&#039;, {
        method: &#039;POST&#039;, // *GET, POST, PUT, DELETE, etc.
        mode: &#039;cors&#039;, // no-cors, *cors, same-origin
        headers: {
          &#039;Content-Type&#039;: &#039;application/json&#039;
        },
        body: JSON.stringify({ id: &#039;123&#039;, name : &#039;qweq&#039; }) 
        // body data type must match &quot;Content-Type&quot; header
      })
      .then(response =&gt; response.json())
      .then(data =&gt; this.setState({ name: data.name, id: data.id }))
  }
</pre></div>


<p><em><strong>componentDidMount</strong></em> will get called immediately after component is mounted.</p>



<p></p>



<h2 class="wp-block-heading">Fetch Post example &#8211; Using Hooks</h2>



<p></p>



<p>Let’s add new&nbsp;<em>CustomHttpRequestPost</em>Hooks component. <em>CustomHttpRequestPost</em>Hooks is function based component and works with Hooks.</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
function CustomHttpRequestPostUsingHooks(){

  const &#x5B;name, setData] = useState(null);
  const &#x5B;id, setId] = useState(null);

  useEffect(() =&gt; {

    fetch(&#039;https://localhost:5001/api/values&#039;, {
        method: &#039;POST&#039;, // *GET, POST, PUT, DELETE, etc.
        mode: &#039;cors&#039;, // no-cors, *cors, same-origin
        cache: &#039;no-cache&#039;, // *default, no-cache, reload, force-cache, only-if-cached
        credentials: &#039;same-origin&#039;, // include, *same-origin, omit
        headers: {
          &#039;Content-Type&#039;: &#039;application/json&#039;
        },
        redirect: &#039;follow&#039;, // manual, *follow, error
        referrerPolicy: &#039;no-referrer&#039;, // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
        body: JSON.stringify({ id: &#039;123&#039;, name : &#039;qweq&#039; }) // body data type must match &quot;Content-Type&quot; header
      })
      .then(response =&gt; response.json())
      .then(data =&gt; 
        {
          setData(data.name);
          setId(data.id)
        })
  },&#x5B;]);
  
    return (
      &amp;lt;div className=&quot;App&quot;&gt;/
        &amp;lt;header className=&quot;App-header&quot;&gt;
          &amp;lt;img src={logo} className=&quot;App-logo&quot; alt=&quot;logo&quot; /&gt;
          &amp;lt;p&gt;
            Hello, TheCodeBuzz Community ! 
          &amp;lt;/p&gt;
          &amp;lt;div className=&quot;card-body&quot;&gt;
            &amp;lt;p&gt;Name: {name}&amp;lt;/p&gt;
            &amp;lt;p&gt;Id: {id} &amp;lt;/p&gt;
          &amp;lt;/div&gt;
        &amp;lt;/header&gt;
      &amp;lt;/div&gt;
    );
  
}

export default CustomHttpRequestPostUsingHooks;
</pre></div>


<p>Above we are making use of <em><strong>useEffect</strong></em> method for putting the Fetch method call as example.</p>



<p></p>



<h2 class="wp-block-heading">Service POST API returning JSON</h2>



<p></p>



<p>Let assume you are getting below HTTP JSON response from Server(Node.js or ASP.NET Core or Spring )</p>



<p>Sample JSON,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: xml; title: ; notranslate">
{
    &quot;id&quot;: &quot;d95c1c74-f1c7-480d-8ea1-621327197980&quot;,
    &quot;name&quot;: &quot;TheCodeBuzz&quot;
}
</pre></div>


<p></p>



<p></p>



<p><em><strong>componentDidMount</strong></em> will get called immediately after component is mounted.</p>



<p>Data will be mapped to Fetch result as define in the <strong>State </strong>or <strong>Hooks</strong>,</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="151" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/React-React-fetch-post-request-1024x151.jpg" alt="React – Using Fetch HTTP POST Request Examples" class="wp-image-11031" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/React-React-fetch-post-request-1024x151.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-React-fetch-post-request-300x44.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-React-fetch-post-request-768x113.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-React-fetch-post-request-785x116.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-React-fetch-post-request.jpg 1288w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p></p>



<h2 class="wp-block-heading">Connecting API/Service endpoint</h2>



<p></p>



<p>You are now ready to connect to any REST API endpoint.</p>



<p>I already have below API endpoint which we shall be returning as Employee model (which we have defined as&nbsp;<em><strong>Observable</strong></em>&nbsp;above in getEmployees())</p>



<p>I am using the ASP.NET Core service below. However, as we know REST APIs are language agnostic, you can use Spring, Node.js, Java or Python-based services, etc.</p>



<p></p>



<p>We are connecting to below Service method defined,</p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
  
        &#x5B;HttpPost]
        public ActionResult&amp;lt;Employee&gt; Post(&#x5B;FromBody] Employee value)
        {
            //Add Employee to Database
            listDBEmployee.Add(value);
            value.Id = &quot;Post:&quot;+ Guid.NewGuid().ToString();
            value.Name = &quot;TheCodebuzz&quot;;

            return Ok(value);
        }

</pre></div>


<p></p>



<h2 class="wp-block-heading">CORS on Server-side</h2>



<p></p>



<p>Please note your Server should allow CORS request from your React UI domain. </p>



<p>Please visit for more details:&nbsp;<a href="https://www.thecodebuzz.com/access-to-xmlhttprequest-at-from-origin-has-been-blocked-by-cors-policy/" target="_blank" rel="noreferrer noopener">How to Enable CORS in ASP.NET Core REST API</a></p>



<p>Once the&nbsp;<strong><em>CORS&nbsp;</em></strong>is enabled on your server-side, you shall see the post method hits your API and gives you required response.</p>



<p></p>



<p></p>



<p>Finally , you could see the result in the browser,</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/React-fetch-post-request-example-1024x574.jpg" alt="React – Using Fetch HTTP POST Request Examples" class="wp-image-11032" width="430" height="241" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/React-fetch-post-request-example-1024x574.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-fetch-post-request-example-300x168.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-fetch-post-request-example-768x431.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-fetch-post-request-example-1536x861.jpg 1536w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-fetch-post-request-example-785x440.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-fetch-post-request-example.jpg 1714w" sizes="auto, (max-width: 430px) 100vw, 430px" /></figure>



<p></p>



<p>I shall be covering more PUT and Delete methods in my next article.</p>



<p></p>



<p>Kindly stay tuned!!</p>



<p></p>



<p></p>



<p style="font-size:18px">Do you have any <strong>comments or ideas or any better </strong>suggestions to share?</p>



<p class="has-small-font-size"></p>



<p style="font-size:18px">Please sound off your comments below.</p>



<p class="has-medium-font-size"></p>



<p class="has-medium-font-size"><strong>Happy Coding </strong>!!</p>



<p></p>



<p></p>



<h2 class="wp-block-heading">Summary</h2>



<p></p>



<p>Today in this article we learned how to write simple React– Fetch POST method using <strong>State </strong>and <strong>Hooks </strong>using easy to understand examples.</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/react-fetch-http-post-request-state-hooks-examples/">React – Using Fetch HTTP POST Request Examples</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/react-fetch-http-post-request-state-hooks-examples/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>React Components Lifecycle with State and Props &#8211; Best Practices</title>
		<link>https://thecodebuzz.com/react-components-with-state-and-props-best-practices/</link>
					<comments>https://thecodebuzz.com/react-components-with-state-and-props-best-practices/#respond</comments>
		
		<dc:creator><![CDATA[admin]]></dc:creator>
		<pubDate>Sun, 07 Jun 2020 03:32:00 +0000</pubDate>
				<category><![CDATA[React]]></category>
		<category><![CDATA[componentdidmount]]></category>
		<category><![CDATA[componentdidupdate]]></category>
		<category><![CDATA[react class component]]></category>
		<category><![CDATA[react component lifecycle]]></category>
		<category><![CDATA[React Components Lifecycle – Best Practices]]></category>
		<category><![CDATA[react functional component lifecycle]]></category>
		<category><![CDATA[react setstate]]></category>
		<category><![CDATA[react state management]]></category>
		<guid isPermaLink="false">https://thecodebuzz.com/?p=10948</guid>

					<description><![CDATA[<p>React Components Lifecycle with State and Props &#8211; Best Practices Today in this article we shall learn React Components Lifecycle with State and Props and we shall see easy-to-follow Best Practices. Before we jump on to the main topics, I shall talk about the Class-based component, its usage, and its benefits. In React Programming creating [&#8230;]</p>
<p>The post <a href="https://thecodebuzz.com/react-components-with-state-and-props-best-practices/">React Components Lifecycle with State and Props – Best Practices</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></description>
										<content:encoded><![CDATA[<h2 class="wp-block-heading">React Components Lifecycle with State and Props &#8211; Best Practices</h2>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/React-Class-based-component-Best-practices.jpg" alt="React Components Lifecycle with State and Props - Best Practices" class="wp-image-10995" width="523" height="253" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/React-Class-based-component-Best-practices.jpg 837w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-Class-based-component-Best-practices-300x146.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-Class-based-component-Best-practices-768x373.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-Class-based-component-Best-practices-785x381.jpg 785w" sizes="auto, (max-width: 523px) 100vw, 523px" /></figure>



<p>Today in this article we shall learn React Components Lifecycle with State and Props and we shall see easy-to-follow Best Practices.</p>



<p>Before we jump on to the main topics, I shall talk about the Class-based component, its usage, and its benefits.</p>



<p></p>



<div class="wp-block-aioseo-table-of-contents"><ul><li><a href="#aioseo-getting-started">Getting started</a></li><li><a href="#aioseo-thing-to-consider-while-creating-a-class-based-component">Thing to consider while creating a class-based  Component,</a></li><li><a href="#aioseo-state-in-react-component">State in React Component</a></li><li><a href="#aioseo-lifecycle-methods-in-react-component">Lifecycle methods in React Component</a></li><li><a href="#aioseo-constructor">Constructor</a></li><li><a href="#aioseo-render">Render</a></li><li><a href="#aioseo-componentdidmount">componentDidmount</a></li><li><a href="#aioseo-componentdidupdate">componentDidUpdate</a></li><li><a href="#aioseo-componentwillunmount">componentWillunMount</a></li><li><a href="#aioseo-best-practices-for-state-in-react-components">Best Practices for State in React components</a></li><li><a href="#aioseo-do-not-modify-state-object-directly">Do not modify the State object directly</a></li><li><a href="#aioseo-always-use-setstate-to-modify-state-object">Always use SetState to modify the State object</a></li><li><a href="#aioseo-asynchronous-state-update-by-react">Asynchronous State update by React</a></li><li><a href="#aioseo-each-state-object-managed-independently">Each State object is managed independently</a></li><li><a href="#aioseo-state-objects-data-is-owned-by-the-component-only">State objects data is owned by the Component only</a></li><li><a href="#aioseo-understanding-props-systems-and-component-communication">Understanding Props Systems and Component Communication</a></li><li><a href="#aioseo-summary">Summary</a></li></ul></div>



<p></p>



<p>In React Programming creating class-based components provides a lot of benefits and it is a preferred way of creating and initializing the components.</p>



<p></p>



<p>A few benefits of using class base components are listed below, </p>



<p></p>



<ul class="wp-block-list"><li><strong><em>Easy code Maintainance </em></strong></li><li><strong><em>Easy to handle input and events</em></strong></li><li><strong><em>Easy life cycle event management</em></strong></li><li><strong><em>Easy data initialization</em></strong></li><li><strong><em>Allows to use of &#8216;State&#8217; (usable in Class-based component)</em></strong></li><li><strong><em>Easy way to manage asynchronous API</em></strong></li></ul>



<p></p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Class-Based Components should be created using  <em><strong>Javascript</strong></em> classes. Javascript classes were introduced in 2015 following <strong>ECMA </strong>standards</p></blockquote>



<p></p>



<h2 class="wp-block-heading" id="aioseo-getting-started">Getting started </h2>



<p></p>



<p>As we saw in our last article <a href="https://www.thecodebuzz.com/getting-started-with-react/" target="_blank" rel="noreferrer noopener" title="Getting Started with React">Getting started with React</a>, we used a function-based component where we rendered the UI with &#8216;Hello World&#8217;.</p>



<p></p>



<p>Here is an example of a function based Component,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
import React from &#039;react&#039;;
import logo from &#039;./logo.svg&#039;;
import &#039;./App.css&#039;;

function App() {
  return (
    &amp;lt;div className=&quot;App&quot;&gt;
      &amp;lt;header className=&quot;App-header&quot;&gt;
        &amp;lt;img src={logo} className=&quot;App-logo&quot; alt=&quot;logo&quot; /&gt;
        &amp;lt;p&gt;
          Hello, World!
        &amp;lt;/p&gt;
      &amp;lt;/header&gt;
    &amp;lt;/div&gt;
  );
}

export default App;
</pre></div>


<p></p>



<h2 class="wp-block-heading" id="aioseo-thing-to-consider-while-creating-a-class-based-component">Thing to consider while creating a class-based  <em><strong>Component</strong></em>,</h2>



<p></p>



<ul class="wp-block-list"><li>The class should be extended from <strong><em>React.Component</em></strong> class</li><li>Every class created should define a render method that returns JSX. One can use a constructor or super method additionally to initialize the data.</li></ul>



<p></p>



<p>We shall now use Class based component and simply replace that as below,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
class App extends React.Component {
  render() {
    return (
      &lt;div className=&quot;App&quot;&gt;
        &lt;header className=&quot;App-header&quot;&gt;
          &lt;img src={logo} className=&quot;App-logo&quot; alt=&quot;logo&quot; /&gt;
          &lt;p&gt;
            Hello, World!
        &lt;/p&gt;
        &lt;/header&gt;
      &lt;/div&gt;
    );
  }
}
</pre></div>


<p></p>



<p>The above code can easily be migrated to the class-based component as below,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: csharp; title: ; notranslate">
class App extends React.Component {
  render() {
    return (
      &lt;div className=&quot;App&quot;&gt;
        &lt;header className=&quot;App-header&quot;&gt;
          &lt;img src={logo} className=&quot;App-logo&quot; alt=&quot;logo&quot; /&gt;
          &lt;p&gt;
            Hello, World!
        &lt;/p&gt;
        &lt;/header&gt;
      &lt;/div&gt;
    );
  }
}
</pre></div>


<p></p>



<p>Below are high-level changes from<em><strong> Function-based</strong></em> to <strong><em>Class-based </em></strong><em><strong>Component </strong></em></p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="1024" height="288" src="https://www.thecodebuzz.com/wp-content/uploads/2020/05/Class-based-component-in-React-Best-practices-1024x288.jpg" alt="React Components Lifecycle with State and Props - Best Practices" class="wp-image-10975" srcset="https://thecodebuzz.com/wp-content/uploads/2020/05/Class-based-component-in-React-Best-practices-1024x288.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/05/Class-based-component-in-React-Best-practices-300x84.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/05/Class-based-component-in-React-Best-practices-768x216.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/05/Class-based-component-in-React-Best-practices-785x221.jpg 785w, https://thecodebuzz.com/wp-content/uploads/2020/05/Class-based-component-in-React-Best-practices.jpg 1346w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></figure>



<p>So overall, we have followed the below steps to create a class-based component,</p>



<ul class="wp-block-list"><li>Create an <strong><em>ES6 </em></strong>class <strong><em>App</em></strong>, extending React.Component.</li><li>We must add the render() method as reacting expects it.</li><li>We moved the body of the function into the render() method.</li></ul>



<p></p>



<h2 class="wp-block-heading" id="aioseo-state-in-react-component">State in React Component </h2>



<p></p>



<p>Let&#8217;s start using the state in our React component.</p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p><em><strong>Using State with function-based React component is tricky. However, The State works very nicely with a class-based React component.</strong></em></p></blockquote>



<p></p>



<p><strong>What is State:</strong></p>



<p></p>



<p>State is a Javascript object which can hold the data for the given Component.</p>



<p></p>



<p>We shall initialize the state in the React component as below.</p>



<p>The state can be declared using the below conventions.</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="795" height="248" src="https://www.thecodebuzz.com/wp-content/uploads/2020/05/State-in-component-in-React-Best-practices.jpg" alt="React Components Lifecycle with State and Props - Best Practices" class="wp-image-10981" srcset="https://thecodebuzz.com/wp-content/uploads/2020/05/State-in-component-in-React-Best-practices.jpg 795w, https://thecodebuzz.com/wp-content/uploads/2020/05/State-in-component-in-React-Best-practices-300x94.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/05/State-in-component-in-React-Best-practices-768x240.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/05/State-in-component-in-React-Best-practices-785x245.jpg 785w" sizes="auto, (max-width: 795px) 100vw, 795px" /></figure>



<p></p>



<p><strong><em>This assignment to state can only be done in the Constructor i.e  just once when components get created </em></strong></p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>You must never assign a State object directly. You must use the <strong>SetState() </strong>function to assign or update the State object anytime. </p></blockquote>



<p></p>



<h2 class="wp-block-heading" id="aioseo-lifecycle-methods-in-react-component">Lifecycle methods in React Component </h2>



<p></p>



<p>Let&#8217;s now understand the overall life cycle methods that get invoked in the React component.</p>



<p>We shall be updating the above code for state objects and trying to understand the life cycle methods,</p>



<p></p>


<div class="wp-block-syntaxhighlighter-code "><pre class="brush: plain; title: ; notranslate">
class App extends React.Component {

  constructor(props){
    super(props)
    this.state = { date:new Date()}
 }
  render() {
    return (
      &amp;lt;div className=&quot;App&quot;&gt;
        &amp;lt;header className=&quot;App-header&quot;&gt;
          &amp;lt;img src={logo} className=&quot;App-logo&quot; alt=&quot;logo&quot; /&gt;
          &amp;lt;p&gt;
            Hello, World!  Current Time : { this.state.date.toLocaleDateString() }
        &amp;lt;/p&gt;
        &amp;lt;/header&gt;
      &amp;lt;/div&gt;
    );
  }
}

</pre></div>


<p></p>



<p>In the above code below is the sequence of methods that get called,</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2020/05/React-undersatnding-lifecycle-methods-best-practices-1024x584.jpg" alt="React Components Lifecycle with State and Props - Best Practices" class="wp-image-10984" width="523" height="297" srcset="https://thecodebuzz.com/wp-content/uploads/2020/05/React-undersatnding-lifecycle-methods-best-practices-1024x584.jpg 1024w, https://thecodebuzz.com/wp-content/uploads/2020/05/React-undersatnding-lifecycle-methods-best-practices-300x171.jpg 300w, https://thecodebuzz.com/wp-content/uploads/2020/05/React-undersatnding-lifecycle-methods-best-practices-768x438.jpg 768w, https://thecodebuzz.com/wp-content/uploads/2020/05/React-undersatnding-lifecycle-methods-best-practices-785x448.jpg 785w" sizes="auto, (max-width: 523px) 100vw, 523px" /></figure>



<p></p>



<p></p>



<p>If you have any useful Life Cycle methods like <em><strong>componentDidmount </strong></em>or <strong><em>componentDidUpdate </em></strong>or <em><strong>componentWillunMount </strong></em>then such methods will get executed too right after render and just before UI is displayed.</p>



<p></p>



<figure class="wp-block-image size-large"><img loading="lazy" decoding="async" width="387" height="730" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/React-lifecycle-methods-workflow.jpg" alt="React Components Lifecycle with State and Props - Best Practices" class="wp-image-11040" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/React-lifecycle-methods-workflow.jpg 387w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-lifecycle-methods-workflow-159x300.jpg 159w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-lifecycle-methods-workflow-276x520.jpg 276w" sizes="auto, (max-width: 387px) 100vw, 387px" /></figure>



<p></p>



<p></p>



<p>As shown above in the diagram, please see a brief description of each stage</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-constructor">Constructor </h2>



<p></p>



<p>Constructors get called first when the Component life cycle starts.</p>



<p></p>



<p>It is the best place to initialize the data.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-render">Render</h2>



<p></p>



<p>Right after the Constructor Render method gets called.</p>



<p></p>



<p>Its sole responsibility is to return JSX only. </p>



<p></p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow"><p>Please note that <strong><em>Render </em></strong>should not do anything apart from rendering the <em>JSX</em>. Please do not write any business logic or fetch any details or update state in Render method</p></blockquote>



<p></p>



<h2 class="wp-block-heading" id="aioseo-componentdidmount"><strong>componentDidmount</strong></h2>



<p></p>



<p><strong>componentDidmount</strong> method gets called once for the given components. <strong>componentDidmount</strong> method is a good location to do initial data loading.</p>



<p>It&#8217;s recommended to use componentDidmount for initial data loading. You may use it to call Example: <strong><em>Fetch </em></strong>GET methods or for calling a custom function with business logic.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-componentdidupdate"><strong>componentDidUpdate</strong></h2>



<p></p>



<p><em><strong>componentDidUpdate </strong></em>is a good location to do data loading when a data update happens on the component. Please make sure to use conditional updates to the selected resources (to avoid any recursive calls).</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-componentwillunmount"><em><strong>componentWillunMount</strong></em></h2>



<p></p>



<p>Use <em><strong>componentWillunMount</strong></em> for performing clean-up activities.</p>



<p>If using any external libraries like vendor components etc. in your application, it&#8217;s your responsibility to unload/unmount it after its usage.</p>



<p></p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-best-practices-for-state-in-react-components">Best Practices for State in React components </h2>



<p></p>



<p>Lets now learn a few Best Practices for the state in React components.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-do-not-modify-state-object-directly">Do not modify the State object directly</h2>



<p></p>



<p>The example below state object <strong><em>date </em></strong>is modified using the direct assignment as below which is wrong.</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/State-in-component-in-React-Best-practices-Do-not-assign-state-directly.jpg" alt="React Components Lifecycle with State and Props - Best Practices" class="wp-image-10985" width="527" height="90" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/State-in-component-in-React-Best-practices-Do-not-assign-state-directly.jpg 721w, https://thecodebuzz.com/wp-content/uploads/2020/06/State-in-component-in-React-Best-practices-Do-not-assign-state-directly-300x52.jpg 300w" sizes="auto, (max-width: 527px) 100vw, 527px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-always-use-setstate-to-modify-state-object">Always use SetState to modify the State object</h2>



<p></p>



<p>You must use <em><strong>setState </strong></em>to modify, update modify the State objects.</p>



<p>In the example below date, State object is modified to use a new <em><strong>Date </strong></em>object using <strong><em>setState </em></strong>function.</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/State-in-component-in-React-Best-practices-Use-SetState-to-assign-state-object.jpg" alt="React Components Lifecycle with State and Props - Best Practices" class="wp-image-10986" width="527" height="90" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/State-in-component-in-React-Best-practices-Use-SetState-to-assign-state-object.jpg 703w, https://thecodebuzz.com/wp-content/uploads/2020/06/State-in-component-in-React-Best-practices-Use-SetState-to-assign-state-object-300x52.jpg 300w" sizes="auto, (max-width: 527px) 100vw, 527px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-asynchronous-state-update-by-react">Asynchronous State update by React</h2>



<p></p>



<p>Considering performance React may update multiple setState calls into a single update. </p>



<p></p>



<p>Do not directly rely on this state object as the code may fail to get the latest state objects when accessed asynchronously.</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/React-Asynchronous-State-update-by-React-Best-practices.jpg" alt="React Components Lifecycle with State and Props - Best Practices" class="wp-image-10988" width="517" height="119" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/React-Asynchronous-State-update-by-React-Best-practices.jpg 642w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-Asynchronous-State-update-by-React-Best-practices-300x70.jpg 300w" sizes="auto, (max-width: 517px) 100vw, 517px" /></figure>



<p></p>



<p>Instead, use <em><strong>setState() </strong></em>accepting a function as shown below rather than using actual objects.</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/React-Asynchronous-State-update-by-React-setState-accepting-a-functions.jpg" alt="React Components Lifecycle with State and Props - Best Practices" class="wp-image-10989" width="515" height="127" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/React-Asynchronous-State-update-by-React-setState-accepting-a-functions.jpg 742w, https://thecodebuzz.com/wp-content/uploads/2020/06/React-Asynchronous-State-update-by-React-setState-accepting-a-functions-300x74.jpg 300w" sizes="auto, (max-width: 515px) 100vw, 515px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-each-state-object-managed-independently">Each State object is managed independently</h2>



<p></p>



<p>Each State object is independent of the other but when called with the <strong><em>setState </em></strong>React merges those objects to the current state.</p>



<p></p>



<p>In the below example State objects like <strong><em>date</em></strong>, <em><strong>amount, </strong></em>and <strong><em>accounts </em></strong>can be independently updated and retrieved as required.</p>



<p></p>



<figure class="wp-block-image size-large is-resized"><img loading="lazy" decoding="async" src="https://www.thecodebuzz.com/wp-content/uploads/2020/06/Each-state-object-declared-is-indepdent-of-each-other-but-merged-together.jpg" alt="React Components Lifecycle with State and Props - Best Practices" class="wp-image-10990" width="481" height="222" srcset="https://thecodebuzz.com/wp-content/uploads/2020/06/Each-state-object-declared-is-indepdent-of-each-other-but-merged-together.jpg 698w, https://thecodebuzz.com/wp-content/uploads/2020/06/Each-state-object-declared-is-indepdent-of-each-other-but-merged-together-300x139.jpg 300w" sizes="auto, (max-width: 481px) 100vw, 481px" /></figure>



<p></p>



<h2 class="wp-block-heading" id="aioseo-state-objects-data-is-owned-by-the-component-only">State objects data is owned by the Component only</h2>



<p></p>



<p>State objects are owned by a component only and can never be treated as State objects outside of that component. However one can send State objects to other child components as props. Such data flow if needed can should be uni-directional i.e should always flow from the origin to its child component.</p>



<p></p>



<h2 class="wp-block-heading" id="aioseo-understanding-props-systems-and-component-communication">Understanding Props Systems and Component Communication</h2>



<p></p>



<p>Please refer below article for more details on the Props system,</p>



<p></p>



<ul class="wp-block-list"><li><a href="https://www.thecodebuzz.com/react-pass-data-parent-child-component-redux-props-hooks/" target="_blank" rel="noopener" title="React – Pass Data From One Component to Other Component">Understanding React Component Communication and Props system</a></li></ul>



<p></p>



<p><strong>Other references</strong>:</p>



<p></p>



<ul class="wp-block-list"><li><a href="https://www.thecodebuzz.com/getting-started-with-react/" target="_blank" rel="noreferrer noopener" title="Getting Started with React">Getting Started with React </a></li></ul>



<p></p>



<h2 class="wp-block-heading" id="aioseo-summary">Summary </h2>



<p></p>



<p>In React Programming class-based components provide a lot of benefits and it is a preferred way of creating and initializing the components. Today in this article we learned how to create a class-based component and then we understood how to leverage State and Props in React components and also learned a few best practices for managing them in the application.</p>



<p></p>



<hr>



<p class=""></p>



<p class="has-background" style="background-color:#b6d9ac;font-size:18px"><br>Please <strong><em>bookmark </em></strong>this page and <em><strong>share </strong></em>it with your friends.                                                    Please <a href="https://www.thecodebuzz.com/subscription/" target="_blank" rel="noreferrer noopener"><em><mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-luminous-vivid-orange-color"><strong>Subscribe</strong> </mark></em></a>to the blog to receive notifications on freshly published (2025) best practices and guidelines for software design and development.</p>




<br>



<hr>



<p class=""></p>



<p></p><p>The post <a href="https://thecodebuzz.com/react-components-with-state-and-props-best-practices/">React Components Lifecycle with State and Props – Best Practices</a> first appeared on <a href="https://thecodebuzz.com">TheCodeBuzz</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://thecodebuzz.com/react-components-with-state-and-props-best-practices/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
