<?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: prsna1230</title>
    <description>The latest articles on DEV Community by prsna1230 (@prsna1230).</description>
    <link>https://dev.to/prsna1230</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%2F1071188%2F52303d02-5989-4ad7-8cc2-e4ca99db9371.jpeg</url>
      <title>DEV Community: prsna1230</title>
      <link>https://dev.to/prsna1230</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prsna1230"/>
    <language>en</language>
    <item>
      <title>React with TypeScript</title>
      <dc:creator>prsna1230</dc:creator>
      <pubDate>Sun, 27 Aug 2023 15:56:34 +0000</pubDate>
      <link>https://dev.to/prsna1230/react-with-typescript-3600</link>
      <guid>https://dev.to/prsna1230/react-with-typescript-3600</guid>
      <description>&lt;p&gt;Hey Guys! Imagine you're building a house. In the world of web development, that house is your application, and React is the tool you use to construct it. Now, think of JavaScript and TypeScript as the building materials. One's like clay, versatile and well-known, while the other's akin to a modern alloy, offering precision and strength. In this journey, we'll explore how these materials shape your creation, and why sometimes, the choice between them can make all the difference. &lt;br&gt;
Anyway the end result is going to be a beautiful house(outer appearance) but the materials which we are using going to impact lot, like lifespan, comfort etc. In this blog we are going to see the difference of using React with JS and (Typescript)TS.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difference 1&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;// ParentComponent.js
import React from 'react';
import ChildComponent from './ChildComponent';

function ParentComponent() {
  const message = 'Hello from Parent';

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Parent Component&amp;lt;/h1&amp;gt;
      &amp;lt;ChildComponent message={message} /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default ParentComponent;

&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;// ChildComponent.js
import React from 'react';

function ChildComponent(props) {
  return &amp;lt;p&amp;gt;Received message: {props.message}&amp;lt;/p&amp;gt;;
}

export default ChildComponent;

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

&lt;/div&gt;



&lt;p&gt;In this example, the ParentComponent sends a prop called message to the ChildComponent. The ChildComponent receives the prop and displays it in a paragraph element. &lt;/p&gt;

&lt;p&gt;Let's see the same implementation in typescript&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ParentComponent.tsx
import React from 'react';
import ChildComponent from './ChildComponent';

function ParentComponent() {
  const message: string = 'Hello from Parent';

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Parent Component&amp;lt;/h1&amp;gt;
      &amp;lt;ChildComponent message={message} /&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

export default ParentComponent;
&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;// ChildComponent.tsx
import React from 'react';

interface ChildProps {
  message: string;
}

function ChildComponent({message}: ChildProps) {
  return &amp;lt;p&amp;gt;Received message: {message}&amp;lt;/p&amp;gt;;
}

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

&lt;/div&gt;



&lt;p&gt;In the TypeScript version, we've defined an interface ChildProps for the props that the ChildComponent expects. The message prop is specified as a string type in the interface. This adds type safety and ensures that the component receives the correct prop type.&lt;/p&gt;

&lt;p&gt;The rest of the code remains similar to the JavaScript version, with the main difference being the use of TypeScript types and interfaces to define prop types.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difference 2&lt;/strong&gt;&lt;br&gt;
In the world of JavaScript, you can call a component's file index.js or index.jsx, and they mean the same thing. But, if you're using TypeScript, remember to name it as index.tsx. If you forget, React won't recognize it as a component; it will think it's just a TypeScript file.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difference 3&lt;/strong&gt;&lt;br&gt;
Let's explore a simple sample project in React with TypeScript to grasp how to utilise state effectively.&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';

function App() {
  const [inputValue, setInputValue] = useState&amp;lt;string&amp;gt;('');
  const [namesList, setNamesList] = useState&amp;lt;string[]&amp;gt;([]);

  const handleAddClick = () =&amp;gt; {
    if (inputValue.trim() !== '') {
      setNamesList([...namesList, inputValue]);
      setInputValue('');
    }
  };

  return (
    &amp;lt;div&amp;gt;
      &amp;lt;h1&amp;gt;Input and Display Example&amp;lt;/h1&amp;gt;
      &amp;lt;input
        type="text"
        value={inputValue}
        onChange={(e)=&amp;gt;setInputValue(e.target.value)}
        placeholder="Enter a name"
      /&amp;gt;
      &amp;lt;button onClick={handleAddClick}&amp;gt;Add&amp;lt;/button&amp;gt;
      &amp;lt;div&amp;gt;
        &amp;lt;h2&amp;gt;Names List&amp;lt;/h2&amp;gt;
        &amp;lt;ul&amp;gt;
          {namesList.map((name, index) =&amp;gt; (
            &amp;lt;li key={index}&amp;gt;{name}&amp;lt;/li&amp;gt;
          ))}
        &amp;lt;/ul&amp;gt;
      &amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;
  );
}

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

&lt;/div&gt;



&lt;p&gt;This code creates a simple web application that allows users to input names. When a name is entered and the "Add" button is clicked, the name is added to a list of names. The list of names is then displayed on the web page. &lt;/p&gt;

&lt;p&gt;Happy Learning!&lt;/p&gt;

</description>
      <category>reactjsdevelopment</category>
      <category>typescript</category>
      <category>beginners</category>
      <category>webdev</category>
    </item>
    <item>
      <title>Unraveling JavaScript's Immutable Secrets: The Dance Between Primitives and Objects</title>
      <dc:creator>prsna1230</dc:creator>
      <pubDate>Fri, 26 May 2023 10:45:16 +0000</pubDate>
      <link>https://dev.to/prsna1230/unraveling-javascripts-immutable-secrets-the-dance-between-primitives-and-objects-321i</link>
      <guid>https://dev.to/prsna1230/unraveling-javascripts-immutable-secrets-the-dance-between-primitives-and-objects-321i</guid>
      <description>&lt;p&gt;Hi, Do you know that Javascript is one of the most popular programming language. In the mean time it is one of the most hated programming language too. Do you know why? Because javascript contains lot of weird stuffs with amazing capability in performing operation. On this line, Today let we explore one of the most missed concept to learn as beginners.&lt;/p&gt;

&lt;p&gt;Let we start with an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = 10;
let b = a;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the previous code, I just assigned value &lt;code&gt;10&lt;/code&gt; to a variable &lt;code&gt;a&lt;/code&gt;, and I am reassigning the &lt;code&gt;a&lt;/code&gt; to &lt;code&gt;b&lt;/code&gt;. you may thinking what is the issue in it.? let me give a another code snippet.&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%2F16qptn4xykl1xu9geodm.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%2F16qptn4xykl1xu9geodm.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;here I assigning an &lt;code&gt;obj1&lt;/code&gt; to &lt;code&gt;obj2&lt;/code&gt;. Very similar to previous code. But there is a difference. let we see the difference&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let a = 10;
let b = a;
b = 20;
console.log(a) // 10
console.log(b) // 20
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the code, I modified the value of &lt;code&gt;b&lt;/code&gt; as &lt;code&gt;20&lt;/code&gt; but it doesn't affect the value of the &lt;code&gt;a&lt;/code&gt;. as you see on the console.log of &lt;code&gt;a&lt;/code&gt; gives still &lt;code&gt;10&lt;/code&gt; but b is 20. you may think it is simple. Yes it is.! but behind this, there lies a concept called &lt;strong&gt;mutability&lt;/strong&gt; . &lt;br&gt;
*&lt;em&gt;Generally Primitives are mutable in JS. *&lt;/em&gt;&lt;br&gt;
oh! OK. what is primitives.? &lt;br&gt;
In JS Primitives are numbers, string, boolean. whereas Non Primitives are Array, objects.&lt;/p&gt;

&lt;p&gt;Let we see the difference between them:&lt;br&gt;
If we declare a variable with string, number, or boolean it will store its data. whenever we try to modify that data it will not overwrite rather it will create a new copy in different memory address.&lt;/p&gt;

&lt;p&gt;but, When a variable is assigned with non-primitives like array or object, that variable holds reference to that non-primitive ,instead of that as value. &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%2Fce71pn4qtmz68wsqttgc.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%2Fce71pn4qtmz68wsqttgc.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you noticed I changed the obj2 but it is affecting the obj1 as well, the reason is non primitive variable stores address not data.&lt;/p&gt;

&lt;p&gt;Hope This is helpful.&lt;br&gt;
Cheers,&lt;br&gt;
Happy Learning.!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
