<?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: Hemant Kumar</title>
    <description>The latest articles on DEV Community by Hemant Kumar (@hemant1101).</description>
    <link>https://dev.to/hemant1101</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F741653%2F606ce654-f89b-4060-afa0-dfc0bf757899.png</url>
      <title>DEV Community: Hemant Kumar</title>
      <link>https://dev.to/hemant1101</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/hemant1101"/>
    <language>en</language>
    <item>
      <title>Achieving Reusability With React Composition</title>
      <dc:creator>Hemant Kumar</dc:creator>
      <pubDate>Tue, 17 Jan 2023 07:43:29 +0000</pubDate>
      <link>https://dev.to/hemant1101/achieving-reusability-with-react-composition-1eo8</link>
      <guid>https://dev.to/hemant1101/achieving-reusability-with-react-composition-1eo8</guid>
      <description>&lt;p&gt;React Composition is a development pattern based on React's original component model where we build components from other components using explicit defined props or the implicit children prop.&lt;/p&gt;

&lt;p&gt;In terms of refactoring, React composition is a pattern that can be used to break a complex component down to smaller components, and then composing those smaller components to structure and complete your application.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Accordion Component&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";

const Accordion = () =&amp;gt; {
  const [expanded, setExpanded] = useState(false);

  const toggleExpanded = () =&amp;gt; {
    setExpanded((prevExpanded) =&amp;gt; !prevExpanded);
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;button onClick={toggleExpanded}&amp;gt;
        Header &amp;lt;span&amp;gt;{expanded ? "-" : "+"}&amp;lt;/span&amp;gt;
      &amp;lt;/button&amp;gt;
      {expanded &amp;amp;&amp;amp; &amp;lt;div&amp;gt;Content&amp;lt;/div&amp;gt;}
    &amp;lt;/div&amp;gt;
  );
};

export default Accordion;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; import React from "react";
import Accordion from "./components/Accordion";

const App = () =&amp;gt; {
  return &amp;lt;Accordion /&amp;gt;;
};

export default App; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Editable Component&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React, { useState } from "react";

const Editable = () =&amp;gt; {
  const [editable, setEditable] = useState(false);
  const [inputValue, setInputValue] = useState("Title");

  const toggleEditable = () =&amp;gt; {
    setEditable((prevEditable) =&amp;gt; !prevEditable);
  };

  const handleInputChange = (e) =&amp;gt; {
    setInputValue(e.target.value);
  };

  return (
    &amp;lt;div&amp;gt;
      {editable ? (
        &amp;lt;label htmlFor="title"&amp;gt;
          Title:
          &amp;lt;input
            type="text"
            id="title"
            value={inputValue}
            onChange={handleInputChange}
          /&amp;gt;
        &amp;lt;/label&amp;gt;
      ) : (
        &amp;lt;&amp;gt;Title: {inputValue}&amp;lt;/&amp;gt;
      )}
      &amp;lt;button onClick={toggleEditable}&amp;gt;{editable ? "Cancel" : "Edit"}&amp;lt;/button&amp;gt;
    &amp;lt;/div&amp;gt;
  );
};

export default Editable;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import React from "react";
import Editable from "./components/Editable";

const App = () =&amp;gt; {
  return &amp;lt;Editable /&amp;gt;;
};

export default App;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's explore one similarity between these two components.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Problem:&lt;/strong&gt; Notice how both the Accordion component and the Editable component share the same functionality, where both are dependent on a boolean and a function to update that boolean — in other words, a toggle functionality.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt; We can use a custom hook that will allow us to reuse this toggle logic in both components, and in any new component added in the future.&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>What is the difference between state and props in React?</title>
      <dc:creator>Hemant Kumar</dc:creator>
      <pubDate>Tue, 17 Jan 2023 07:28:08 +0000</pubDate>
      <link>https://dev.to/hemant1101/what-is-the-difference-between-state-and-props-in-react-4g0o</link>
      <guid>https://dev.to/hemant1101/what-is-the-difference-between-state-and-props-in-react-4g0o</guid>
      <description>&lt;p&gt;Props and state are related. The state of one component will often become the props of a child component. Props are passed to the child within the render method of the parent as the second argument to React.createElement() or, if you're using JSX, the more familiar tag attributes.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;MyChild name={this.state.childsName} /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The parent's state value of childsName becomes the child's this.props.name. From the child's perspective, the name prop is immutable. If it needs to be changed, the parent should just change its internal state:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;this.setState({ childsName: 'New name' });&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;and React will propagate it to the child for you. A natural follow-on question is: what if the child needs to change its name prop? This is usually done through child events and parent callbacks. The child might expose an event called, for example, onNameChanged. The parent would then subscribe to the event by passing a callback handler.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&amp;lt;MyChild name={this.state.childsName} onNameChanged={this.handleName} /&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The child would pass its requested new name as an argument to the event callback by calling, e.g., this.props.onNameChanged('New name'), and the parent would use the name in the event handler to update its state.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;handleName: function(newName) {&lt;br&gt;
   this.setState({ childsName: newName });&lt;br&gt;
}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;props vs state&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;tl;dr If a Component needs to alter one of its attributes at some point in time, that attribute should be part of its state, otherwise it should just be a prop for that Component.&lt;br&gt;
**&lt;br&gt;
props**&lt;br&gt;
Props (short for properties) are a Component's configuration. They are received from above and immutable as far as the Component receiving them is concerned. A Component cannot change its props, but it is responsible for putting together the props of its child Components. Props do not have to just be data -- callback functions may be passed in as props.&lt;br&gt;
**&lt;br&gt;
state**&lt;br&gt;
The state is a data structure that starts with a default value when a Component mounts. It may be mutated across time, mostly as a result of user events.&lt;/p&gt;

&lt;p&gt;A Component manages its own state internally. Besides setting an initial state, it has no business fiddling with the state of its children. You might conceptualize state as private to that component.&lt;/p&gt;

&lt;p&gt;Changing props and state&lt;br&gt;
                                                   props   state&lt;br&gt;
    Can get initial value from parent Component?    Yes     Yes&lt;br&gt;
    Can be changed by parent Component?             Yes     No&lt;br&gt;
    Can set default values inside Component?*       Yes     Yes&lt;br&gt;
    Can change inside Component?                    No      Yes&lt;br&gt;
    Can set initial value for child Components?     Yes     Yes&lt;br&gt;
    Can change in child Components?                 Yes     No&lt;/p&gt;

&lt;p&gt;Note that both props and state initial values received from parents override default values defined inside a Component.&lt;br&gt;
Should this Component have state?&lt;br&gt;
State is optional. Since state increases complexity and reduces predictability, a Component without state is preferable. Even though you clearly can't do without state in an interactive app, you should avoid having too many Stateful Components.&lt;/p&gt;

&lt;p&gt;Component types&lt;br&gt;
Stateless Component Only props, no state. There's not much going on besides the render() function. Their logic revolves around the props they receive. This makes them very easy to follow, and to test.&lt;/p&gt;

&lt;p&gt;Stateful Component Both props and state. These are used when your component must retain some state. This is a good place for client-server communication (XHR, web sockets, etc.), processing data and responding to user events. These sort of logistics should be encapsulated in a moderate number of Stateful Components, while all visualization and formatting logic should move downstream into many Stateless Components.&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>community</category>
      <category>discord</category>
      <category>reddit</category>
    </item>
    <item>
      <title>Introduction to React ⚛️</title>
      <dc:creator>Hemant Kumar</dc:creator>
      <pubDate>Sat, 27 Nov 2021 16:42:46 +0000</pubDate>
      <link>https://dev.to/hemant1101/introduction-to-react-4nmm</link>
      <guid>https://dev.to/hemant1101/introduction-to-react-4nmm</guid>
      <description>&lt;p&gt;What is React.js?&lt;/p&gt;

&lt;p&gt;React… let’s say you are browsing the internet and the content on a page requires some changes according to your inputs, time, or any other factor.. JavaScript seems to be inefficient and complex for handling those changes, hence a library to “react” to those factors and update the content of a page is called React.js ⚛️.&lt;/p&gt;

&lt;p&gt;React is component based, meaning it can be used to build simple reusable components of the UI (example: a button) and use it everywhere in your app.&lt;br&gt;
React is state based, implying the app’s state is handled and accordingly the render takes place, this helps to write better and predictable code.&lt;br&gt;
To phrase it properly, React is a free and open-source front-end JavaScript library for building user interfaces or UI components.&lt;/p&gt;

&lt;p&gt;History (A small flashback) 📜:-&lt;/p&gt;

&lt;p&gt;In 2013, Facebook struggled while handling Facebook Ads and was looking for a way to make small components that build up to make bigger and multifunctional apps. Hence, some basic prototype versions of react were made such as Xhp, and FaxJS. Soon after acquiring Instagram, Facebook built the first stable version of React and deployed it in 2014 as a small part of its User Interface while also open-sourcing it. Many had hopes but were skeptical with thoughts that were along the lines of “HTML in JavaScript? Why?”&lt;/p&gt;

&lt;p&gt;But what is App? App is just a function. A JavaScript Function. Yes, React has brought a component based UI system to its bare minimum. I may sound weird, but trust me, it took decades to get here. and React is the finest in history. At least according to most of the web developers.&lt;/p&gt;

&lt;p&gt;How to code the “hello world” react App?&lt;br&gt;
Once you have Node.js and NPM installed go ahead and run the following command in your terminal:&lt;/p&gt;

&lt;p&gt;npx create-react-app my-app // Sets up a basic react project&lt;/p&gt;

&lt;p&gt;Open the folder “my-app” in any of your code editor. This is how the files should look like:&lt;/p&gt;

&lt;p&gt;src —Folder where the main core react files will be coded and stored.&lt;/p&gt;

&lt;p&gt;public —Folder where you can place your assets and the index.html file.&lt;/p&gt;

&lt;p&gt;Please feel free to ignore the following files as we won’t be testing our app or report vitals for time being :)&lt;/p&gt;

&lt;p&gt;In every react based project, there will be an index.html containing a div or a section tag with an id of “root”. This div is where we would render or paint our complete react app.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Starting GUI with a Swing..</title>
      <dc:creator>Hemant Kumar</dc:creator>
      <pubDate>Sat, 27 Nov 2021 16:09:09 +0000</pubDate>
      <link>https://dev.to/hemant1101/starting-gui-with-a-swing-2a9i</link>
      <guid>https://dev.to/hemant1101/starting-gui-with-a-swing-2a9i</guid>
      <description>&lt;p&gt;Java is a powerful programming language that is right at home in standalone desktop applications and complex enterprise-level web applications. It is a popular programming language for computer science students because it incorporates just about every key concept of modern object-oriented programming yet remains relatively easy to learn.&lt;/p&gt;

&lt;p&gt;The Swing toolkit offers developers a platform-independent, customizable, configurable, and lightweight solution that can easily be incorporated into practically any Java program. The toolkit offers basic components like buttons and labels as well as advanced features including trees and tables. The entire toolkit is written in Java and is part of the Java Foundation Classes (JFC). The JFC is a collection of packages designed to create fully featured desktop applications. Other components of the JFC include AWT, Accessibility, Java 2-D, and Drag and Drop.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
