<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ramachandra Petla</title>
    <description>The latest articles on DEV Community by Ramachandra Petla (@ramachandrapetla).</description>
    <link>https://dev.to/ramachandrapetla</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1012748%2F90feb580-713f-47f7-b728-ad8443825ce5.png</url>
      <title>DEV Community: Ramachandra Petla</title>
      <link>https://dev.to/ramachandrapetla</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ramachandrapetla"/>
    <language>en</language>
    <item>
      <title>Micro Frontends</title>
      <dc:creator>Ramachandra Petla</dc:creator>
      <pubDate>Thu, 10 Oct 2024 16:26:01 +0000</pubDate>
      <link>https://dev.to/ramachandrapetla/micro-frontends-398n</link>
      <guid>https://dev.to/ramachandrapetla/micro-frontends-398n</guid>
      <description>&lt;p&gt;We’ve all enjoyed building creative front-end applications using our favorite library, &lt;code&gt;React&lt;/code&gt;. But did you know that we can break down large, complex applications into smaller, more manageable pieces? And, to make it even better, you can use different frameworks for each of these pieces!&lt;/p&gt;

&lt;p&gt;I recently dove into this development model, and even though it's been around for some time, I’m amazed I hadn’t fully explored it until now.&lt;/p&gt;

&lt;p&gt;If you’re like me and hadn’t come across this before, let me break it down for you. You’ve probably heard of microservices, right? If not, here’s a quick explanation. In the old days, we used to build applications as a single monolithic unit. These were often massive and hard to grasp, especially if you were new to the codebase—what we typically refer to as "legacy" applications. Some clever folks thought, "Why not split these big applications into smaller, independent units to make them easier to manage?" And thus, microservices were born. Each small unit provides a set of services, but the core functionality of the original application remains the same, just divided into manageable chunks.&lt;/p&gt;

&lt;p&gt;Now, if you’ve followed so far, you might be thinking, "So this new model must be similar to microservices." And you'd be right! The concept is indeed similar, but there’s a key difference in the implementation. While backend services can be deployed and run independently (though they can still communicate with each other), the frontend approach requires a slightly different strategy.&lt;/p&gt;

&lt;p&gt;Now, let’s dive into the concept of Micro Frontend Architecture (MFE).&lt;/p&gt;

&lt;p&gt;Just like microservices in the backend, micro frontends break up the frontend monolith into smaller, independently developed, tested, and deployed units. Each of these units represents a different part of the application—like a specific feature or section. This allows different teams to work on different parts of the application, using the tools, frameworks, and libraries that best suit their needs.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why Micro Frontends?
&lt;/h2&gt;

&lt;p&gt;In traditional frontend development, a single team or multiple teams work on one large codebase. Over time, this can get messy—long build times, complex dependency management, and hard-to-track bugs. With micro frontends, you’re essentially breaking this down, so each team manages a smaller scope. This approach provides a few key benefits:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Team Autonomy:&lt;/strong&gt; Different teams can independently develop, test, and deploy their micro frontends. This allows them to use different frameworks (yes, you can have React for one module, Angular for another, and maybe even Vue.js elsewhere).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Scalability:&lt;/strong&gt; With teams working independently, they can scale their development efforts without being blocked by other teams working on the same codebase. This accelerates development speed and reduces bottlenecks.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Easier Maintenance:&lt;/strong&gt; Since the application is broken into smaller pieces, each micro frontend can evolve separately. This leads to easier maintenance and debugging, as you no longer need to sift through a gigantic codebase to find a problem.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Incremental Upgrades:&lt;/strong&gt; One of the biggest pains in frontend development is upgrading frameworks or libraries. With micro frontends, you can upgrade one part of the application without affecting the rest, making transitions smoother.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How Micro Frontends Work
&lt;/h2&gt;

&lt;p&gt;The core idea is to split the frontend application by features, and then stitch them together at runtime. This can be done in several ways, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Iframe-based Approach:&lt;/strong&gt; While not common anymore, this involves embedding different micro frontends using iframes. It’s easy to implement but can lead to poor user experiences due to performance and styling issues.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;JavaScript Bundles:&lt;/strong&gt; Different micro frontends are deployed as independent JavaScript bundles, and these are combined on the client side. This is a more modern and flexible approach.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Web Components:&lt;/strong&gt; Using web standards like custom elements, shadow DOM, and HTML templates, micro frontends can be built and deployed as reusable components that work seamlessly across frameworks.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;-&lt;br&gt;
&lt;strong&gt;Server-side Composition:&lt;/strong&gt; In this method, the micro frontends are composed server-side, and the user receives a fully-rendered page. This is great for performance but might be more complex to implement.&lt;/p&gt;

&lt;h2&gt;
  
  
  Key Considerations
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Communication Between Micro Frontends:&lt;/strong&gt; Though each micro frontend is independent, they often need to communicate. This can be managed through events, a shared state, or API calls.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Shared Dependencies:&lt;/strong&gt; While it’s tempting for each micro frontend to include its own libraries, it’s important to manage shared dependencies to avoid bloating the final bundle size.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Routing:&lt;/strong&gt; You’ll need a strategy for routing between different micro frontends. This can be done through a central router that loads the appropriate frontend based on the URL.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Micro frontends bring the same benefits of modularity and scalability that we love in microservices to the frontend world. By splitting large applications into smaller, independently managed pieces, we can reduce complexity, boost team productivity, and deliver more flexible, maintainable code.&lt;/p&gt;

&lt;p&gt;If you’re dealing with a large, unwieldy frontend, maybe it’s time to explore the world of micro frontends!&lt;/p&gt;

</description>
      <category>react</category>
      <category>remoteui</category>
      <category>microfrontend</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Beginners guide to ReactJS</title>
      <dc:creator>Ramachandra Petla</dc:creator>
      <pubDate>Thu, 13 Jul 2023 19:05:33 +0000</pubDate>
      <link>https://dev.to/ramachandrapetla/beginners-guide-to-reactjs-456h</link>
      <guid>https://dev.to/ramachandrapetla/beginners-guide-to-reactjs-456h</guid>
      <description>&lt;p&gt;React is client side JavaScript Library that allows us to create Dynamic user interfaces, Single Page Applications (SPA) following the component structure, thereby making the life of developers easy.&lt;/p&gt;

&lt;p&gt;This article is for React beginners who is looking to get started with React. In this article you can learn everything you needed to create a web application using ReactJS. &lt;/p&gt;

&lt;p&gt;Before moving forward with our little project, lets understand some important concepts of React&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What are Components&lt;/li&gt;
&lt;li&gt;Writing Markup with JSX&lt;/li&gt;
&lt;li&gt;Props and State&lt;/li&gt;
&lt;li&gt;Handling Events&lt;/li&gt;
&lt;li&gt;React Hooks&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What are Components ?
&lt;/h2&gt;

&lt;p&gt;We can create Components in two ways. Functional components and Class components&lt;/p&gt;

&lt;h3&gt;
  
  
  Class component
&lt;/h3&gt;

&lt;p&gt;If you want to create a Class component you need to extend the Component class of React, and your class component should have render() method and it is mandatory method. All the JSX syntax is returned inside the render method.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;render()&lt;/code&gt; methods needs to be pure. i.e. you should not update the component state inside it. Because when JSX tries to render the component it looks for components state and props and renders it again if any changes occurred. If you update component state inside render, JSX will recognize it and render it again and again, which is not acceptable.&lt;/p&gt;

&lt;p&gt;If you are wondering about state and props, don’t worry. We are going to learn them below &lt;/p&gt;

&lt;p&gt;To create a class component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Welcome&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Component&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello, &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Each component has several life cycle methods. These methods can be override with custom code to run at particular times in the process.&lt;/p&gt;

&lt;p&gt;Every React Component (Class / Functional) has 3 lifecycle phases: Mounting, Updating, Unmounting.&lt;/p&gt;

&lt;p&gt;A component &lt;code&gt;mounts&lt;/code&gt; when it renders for the first time&lt;/p&gt;

&lt;p&gt;From the second time, Component &lt;code&gt;updates&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;When the component is removed from the DOM, it is said to be &lt;code&gt;unmounted&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Class components has LifeCycle methods that run at each phase.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Mounting Lifecycle methods:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;componentWillMount()&lt;/code&gt;, &lt;code&gt;render()&lt;/code&gt;, and &lt;code&gt;componentDidMount()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;code&gt;componentWillMount()&lt;/code&gt; will be called first followed by the &lt;code&gt;render()&lt;/code&gt; method and finally the &lt;code&gt;componentDidMount()&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Updating Lifecycle method:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;shouldComponentUpdate()&lt;/code&gt; it is called before &lt;code&gt;render()&lt;/code&gt; is called. Component only renders if &lt;code&gt;shouldComponentUpdate()&lt;/code&gt; returns &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Unmounting LifeCycle method:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;componentWillUnmount()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We are not going much deeper into the class components.&lt;/p&gt;

&lt;p&gt;With the advent of the Functional components, The use of Class components is minimized. In this article we are going to use Functional Components only.&lt;/p&gt;

&lt;h3&gt;
  
  
  Functional Component:
&lt;/h3&gt;

&lt;p&gt;With &lt;code&gt;function Welcome() { }&lt;/code&gt; you define a JavaScript function with the name &lt;code&gt;Welcome&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This function should have return statement&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Functional Components are same are JavaScript Functions, except that their name should start with a capital letter, or they don’t work. &lt;code&gt;function welcome() { }&lt;/code&gt; is wrong. &lt;code&gt;function Welcome() { }&lt;/code&gt; is right&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Sample Functional Component:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
   &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
         &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Hello, &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
     &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;Welcome&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In Functional Components we cannot use &lt;code&gt;LifeCycleMethods&lt;/code&gt;, instead we have &lt;code&gt;Hooks&lt;/code&gt; which are introduced with React 16.8&lt;/p&gt;

&lt;p&gt;We can use functional components same as the Class components.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is JSX?
&lt;/h2&gt;

&lt;p&gt;JSX is a syntax extension to javascript. Whatever you have seen in the previous section in the return statement is the JSX syntax.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;const element = &amp;lt;h1&amp;gt;Hello, world!&amp;lt;/h1&amp;gt;;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Consider the statement above. It is neither HTML nor String. It is called JSX. JSX is used to create UI templates by utilizing the full power of JavaScript&lt;/p&gt;

&lt;p&gt;In JSX syntax, we can write HTML elements and use React components with In.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;h1&amp;gt;&lt;/span&gt;Intro Heading&lt;span class="nt"&gt;&amp;lt;/h1&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;IntroSection&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is what a component looks like. We can reuse this component inside other components along with HTML elements and JSX compiler with understands it and converts it into html.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;IntroSection&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Name:&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Email:&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Address:&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;address&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;IntroSection&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;When writing JSX in React, there's one caveat: &lt;strong&gt;&lt;em&gt;you must return one parent item. Not more than one&lt;/em&gt;&lt;/strong&gt;. For example, this is not possible&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;These components throws and error&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;FuncComp&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Parent 1&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Parent 2&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Parent 3&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Instead you need to wrap these inside another parent or use special JSX fragment &lt;code&gt;&amp;lt;&amp;gt; &amp;lt;/&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;it is same as &lt;code&gt;&amp;lt;React.Fragment&amp;gt; &amp;lt;/React.Fragment&amp;gt;&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="c1"&gt;//This is going to work&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;FuncComp&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Parent 1&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Parent 2&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Parent 3&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    )
}

//This could work as well

function FuncComp() &lt;span class="si"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Parent 1&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Parent 2&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Parent 3&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="si"&gt;}&lt;/span&gt;

function FuncComp() &lt;span class="si"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Fragment&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Parent 1&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Parent 2&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Parent 3&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nc"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Fragment&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="si"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Props and State
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Props:
&lt;/h3&gt;

&lt;p&gt;React component uses props to communicate with each other. Every parent component can pass information to their child components in the form of props. You can think of props just like HTML attributes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;StudentGroup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;StudentGroup&lt;/code&gt; component has a child component &lt;code&gt;Student&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the above snippet, &lt;code&gt;StudentGroup&lt;/code&gt; haven’t passed any props to the &lt;code&gt;Student&lt;/code&gt; component&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;StudentGroup&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Student&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;studentName&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;age&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;studentAge&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;email&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;studentEmail&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Student component can read the information from the parent component as follows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Name: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Age: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Email: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// OR (use Object Destructing)&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Name: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Age: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Email: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;email&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  State:
&lt;/h3&gt;

&lt;p&gt;When you create components, It need to preserve some component specific information based on the user interactions. For suppose, you created a counter as a component that increases its value on click of button. The current value of the counter is the information that we are going to store in the state, because we need to constantly update this value on every interaction with the button. Whenever the user clicks on the button, the UI should updated instantly with the new value. This is where the state comes into picture. State is specific to component. Every instance of component has its own state.&lt;/p&gt;

&lt;p&gt;The state of component can be managed by its own, or it can be managed by its parent component. If it is managed by parent component, with change in state is affected by all the component instances. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fui75pluv2otl86gcwcxb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fui75pluv2otl86gcwcxb.png" alt="state by component" width="800" height="469"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;State managed by Component&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcmjq5kdbc58kp2mel802.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcmjq5kdbc58kp2mel802.png" alt="state by parent" width="800" height="469"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;State managed by Components Parent&lt;/p&gt;

&lt;p&gt;For functional component, state is created using &lt;code&gt;useState&lt;/code&gt; hook&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;
&lt;span class="c1"&gt;//State managed by Component&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;useStae&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;increaseCounter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Counter: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;increaseCounter&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Increase Counter&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;//State managed by parent&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;Counter&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&lt;span class="nx"&gt;increaseCounter&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Counter: &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;onClick&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;increaseCounter&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Increase Counter&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ParentComp&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;increaseCounter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;setCounter&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;counter&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;Counter&lt;/span&gt; &lt;span class="na"&gt;increaseCounter&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;increaseCounter&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Hooks
&lt;/h2&gt;

&lt;p&gt;React provides a bunch of standard in-built hooks:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;useState&lt;/code&gt;: To manage states. Returns a stateful value and an updater function to update it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useEffect&lt;/code&gt;: To manage side-effects like API calls, subscriptions, timers, mutations, and more.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useContext&lt;/code&gt;: To return the current value for a context.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useReducer&lt;/code&gt;: A &lt;code&gt;useState&lt;/code&gt; alternative to help with complex state management.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useCallback&lt;/code&gt;: It returns a memorized version of a callback to help a child component not re-render unnecessarily.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useMemo&lt;/code&gt;: It returns a memoized value that helps in performance optimizations.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useRef&lt;/code&gt;: It returns a ref object with a &lt;code&gt;.current&lt;/code&gt; property. The ref object is mutable. It is mainly used to access a child component imperatively.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useLayoutEffect&lt;/code&gt;: It fires at the end of all DOM mutations. It's best to use &lt;code&gt;useEffect&lt;/code&gt; as much as possible over this one as the &lt;code&gt;useLayoutEffect&lt;/code&gt; fires synchronously.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;useDebugValue&lt;/code&gt;: Helps to display a label in React DevTools for custom hooks.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To Create a React application, you need to have &lt;code&gt;npm&lt;/code&gt; installed on your machine&lt;/p&gt;

&lt;p&gt;Open terminal on mac or CMD on windows and enter the below commands&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npx create-react-app react-example
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is going to create a new react project with name ‘react-example’ in the directory where you executed that command&lt;/p&gt;

&lt;p&gt;This will the basic directory structure you will get if you create app using &lt;code&gt;create-react-app&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv0b4qa4fspg6sd117fts.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv0b4qa4fspg6sd117fts.png" alt="Directory Structure" width="564" height="734"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;index.js&lt;/code&gt; is going to be the starting point of the application&lt;/p&gt;

&lt;p&gt;&lt;code&gt;App.js&lt;/code&gt; is the First component that is wrapped inside &lt;code&gt;index.js&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You can start creating new components and wrap it inside &lt;code&gt;App&lt;/code&gt; component&lt;/p&gt;

&lt;p&gt;In our next article, we can create a simple website using React. Until then keep messing up with React&lt;/p&gt;

</description>
      <category>react</category>
      <category>beginners</category>
      <category>website</category>
    </item>
    <item>
      <title>Spring Security: Configuring UserDetailsService (Part 1)</title>
      <dc:creator>Ramachandra Petla</dc:creator>
      <pubDate>Tue, 20 Jun 2023 02:02:03 +0000</pubDate>
      <link>https://dev.to/ramachandrapetla/spring-security-authentication-authorization-part-1-1n48</link>
      <guid>https://dev.to/ramachandrapetla/spring-security-authentication-authorization-part-1-1n48</guid>
      <description>&lt;p&gt;When we create an API for providing services or an web application, it is crucial to provide authentication and authorization to the project. In this post I am going to tell how to use Spring Security module to make the necessary security configurations.&lt;/p&gt;

&lt;p&gt;To start, I am going to create a RestController with two endpoints "/", "/welcome" and try to run the application and see what happens.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@RestController
public class HomeController {   
   @GetMapping("/")
   public String sayHello() {
      return "Welcome to Spring Security!";
   }
   @GetMapping("/welcome")
   public String sayHello() {
      return "Welcome to Spring Security(Protected)!";
   }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the result !&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh1uc6hc44msrgca3yzwf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh1uc6hc44msrgca3yzwf.png" alt="base endpoint without spring security module" width="740" height="288"&gt;&lt;/a&gt;&lt;br&gt;
Lets see what happens if we add spring security to the project dependency&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;dependency&amp;gt;             
   &amp;lt;groupId&amp;gt;org.springframework.boot&amp;lt;/groupId&amp;gt;
   &amp;lt;artifactId&amp;gt;spring-boot-starter-security&amp;lt;/artifactId&amp;gt;
&amp;lt;/dependency&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now if we run the application and try to hit and endpoint you will be redirected to a login page which will look like below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frw72cva7g9zmuotg8r11.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frw72cva7g9zmuotg8r11.png" alt="Endpoint after adding spring security" width="800" height="341"&gt;&lt;/a&gt;&lt;br&gt;
Well, I don't remember adding a &lt;code&gt;/login&lt;/code&gt; endpoint nor configured any users. But how the login page rendered and why?&lt;br&gt;
Spring security is enabled just by adding the dependency to the classpath. By default all the routes/endpoints needs authentication to access. But what credentials are used to login? I don't remember configuring any credentials!. It is because if we didn't configure it spring will provide a default random credentials at run time&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9kbs8ciuszdmwwn6iis6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9kbs8ciuszdmwwn6iis6.png" alt="auto generated password" width="800" height="71"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Security Configuration
&lt;/h2&gt;

&lt;p&gt;Spring MVC sends all incoming HTTP requests into a single servlet named &lt;code&gt;DispatcherServlet&lt;/code&gt; after passing through set of filters. The DispatcherServlet is responsible to send the HTTP request to the corresponding Controller.&lt;br&gt;
When we add Spring Security to the classpath a set of Security Filters are added to the FilterChain. These filters intercept every HTTP request before they come to the Controllers. This is where Spring security authenticates the HTTP requests and decides either to forward or reject the request.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgm31etu2di2e5d8xq5bc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgm31etu2di2e5d8xq5bc.png" alt="Spring Security Architecture" width="800" height="367"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;By default, every HTTP request is authenticated. But, we can override this behaviour by making our own security configuration&lt;/p&gt;

&lt;p&gt;In order to do that, we need to provide our custom &lt;code&gt;SecurityFilterChain&lt;/code&gt; bean&lt;/p&gt;

&lt;p&gt;Default behaviour is due to the bean&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Bean
@Order(SecurityProperties.BASIC_AUTH_ORDER)
SecurityFilterChain defaultSecurityFilterChain(HttpSecurity http) throws Exception {
    http.authorizeHttpRequests().anyRequests().authenticated();
    http.formLogin();
    http.httpBasic();
    return http.build();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, we can have our own bean in a brand new configuration class like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Configuration
class ApiSecurityConfiguration {    
   @Bean
   SecurityFilterChain customSecurityFilterChain(HttpSecurity http) throws Exception {
      http.authorizeHttpRequests((requests)-&amp;gt; requests
           .requestMatchers("/welcome").authenticated()              
               .anyRequests().authenticated());
       .formLogin();
       .httpBasic(Customizer.withDefaults());
      return http.build();
   }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The above configuration makes &lt;code&gt;/welcome&lt;/code&gt; endpoint is authenticated and any endpoint other than that are publicly accessible&lt;/p&gt;

&lt;p&gt;But we still have not set up our own set of users.&lt;br&gt;
Before moving forward with Users setup, we need to understand the how the request is processed.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7dyakllklvvh32s12y3n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7dyakllklvvh32s12y3n.png" alt="UserDetailsService" width="800" height="388"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;UserDetailsService&lt;/code&gt; (Interface) &lt;code&gt;-&amp;gt; loadUserByUserName(String username)&lt;/code&gt; &lt;br&gt;
This is the Core Interface which loads user specific data&lt;/p&gt;

&lt;p&gt;&lt;code&gt;UserDetailsManager&lt;/code&gt;(Interface extends UserDetailsService) Extension of userDetailsService which allows to create, update, delete UserDetails (see method signatures below)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;createUser(UserDetails user)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;updateUser(UserDetails user)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;deleteUser(String username user)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;changePassword(String oldPwd, String newPwd)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;userExists(String username)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Sample Implementation classes ( for &lt;code&gt;UserDetailsManager&lt;/code&gt; interface ) provided by Spring Security are &lt;code&gt;InMemoryUserDetailsManager&lt;/code&gt;, &lt;code&gt;JdbcUserDetailsManager&lt;/code&gt;, &lt;code&gt;LdapUserDetailsManager&lt;/code&gt; &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;All the above interfaces and classes use an interface &lt;code&gt;UserDetails&lt;/code&gt; which provides core user information&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For proving user details for authetication, we need to implement &lt;code&gt;UserDetailsService&lt;/code&gt; interface and provide it as a spring bean. We can either use the default implementations provided by spring or we can have our own custom implementation. &lt;br&gt;
If we are building enterprise application, it is recommended to have your own custom implementation. But for simplicity, we will use the default implementations provided &lt;br&gt;
In this post, we will see how we can configure &lt;code&gt;InMemoryUserDetailsManager&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Bean
UserDetailsService userDetailsService() {
   UserDetails admin = User.withDefaultPasswordEncoder()                         
                           .username("admin")                                
                           .password("12345")                                
                           .authorities("admin")                                 
                           .build();    
   UserDetails user = User.withDefaultPasswordEncoder()
               .username("user")
               .password("54321")
               .authorities("read")
               .build();
   return new InMemoryUserDetailsManager(user, admin);
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, we are using &lt;code&gt;DefaultPasswordEncoder()&lt;/code&gt;. But, in general we need to provide a &lt;code&gt;PasswordEncoder&lt;/code&gt; bean. Spring provides us with many password encoders&lt;br&gt;
For Example,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Bean
PasswordEncoder passwordEncoder() {
   return new BCryptyPasswordEncoder()
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, with these configurations, we can access the protected routes using the provided users in InMemory.&lt;br&gt;
We can also get users from database using default &lt;code&gt;JdbcUserDetailsManager&lt;/code&gt; bean offered by spring Security&lt;br&gt;
In order to use this we need to have a datasource&lt;br&gt;
We are going to use mysql for this application. Datasource configurations are done in &lt;code&gt;applications.properties&lt;/code&gt; file or we have &lt;code&gt;application.yml&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;spring:
  datasource:
    platform: mysql
    url: jdbc:mysql://localhost:5432/mydb
    username: foo
    password: bar
    jpa:
       show-sql: true
       properties:
          hibernate:
        format_sql: true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By Default &lt;code&gt;JdbcUserDetailsManager&lt;/code&gt; default implementation, assumes we follow a database  schema which has &lt;code&gt;users(id, username, password, enabled)&lt;/code&gt; and &lt;code&gt;authorities(id, username, authority)&lt;/code&gt; tables&lt;br&gt;
So, if we are using &lt;code&gt;JdbcUserDetailsManager&lt;/code&gt; implementation we need to follow the same ddl schema&lt;br&gt;
Replace &lt;code&gt;InMemoryUserDetailsManager&lt;/code&gt; bean with &lt;code&gt;JdbcUserDetailsManager&lt;/code&gt; bean (Make sure you pass DataSoucrce object)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Bean
UserDetailsService userDetailsService(DataSource dataSource) {
   return new JdbcUserDetailsManager(dataSource)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Well, we have configured our application to use InMemory UserDetails and configured to fetch user details from database as well using default implementations.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/LrRtwbgTe6g"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;But this is not enough for enterprise application, since we might not follow the same naming convention in the database based on client recommendation. In that case we cannot use &lt;code&gt;JdbcUserDetailsManager&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;But that is a topic for another day.&lt;br&gt;
I will write more about it in my next post. &lt;/p&gt;

</description>
      <category>springboot</category>
      <category>springsecurity</category>
      <category>java</category>
      <category>api</category>
    </item>
  </channel>
</rss>
