<?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: Piyush Jaiswal</title>
    <description>The latest articles on DEV Community by Piyush Jaiswal (@piyushjaiswal1610).</description>
    <link>https://dev.to/piyushjaiswal1610</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%2F1020983%2F235140e3-767a-43ee-9bc2-f62432e8527d.jpeg</url>
      <title>DEV Community: Piyush Jaiswal</title>
      <link>https://dev.to/piyushjaiswal1610</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/piyushjaiswal1610"/>
    <language>en</language>
    <item>
      <title>Intoduction to Redux</title>
      <dc:creator>Piyush Jaiswal</dc:creator>
      <pubDate>Tue, 20 Jun 2023 14:27:43 +0000</pubDate>
      <link>https://dev.to/piyushjaiswal1610/mastering-react-redux-part-1-what-is-redux-and-why-it-exists-1437</link>
      <guid>https://dev.to/piyushjaiswal1610/mastering-react-redux-part-1-what-is-redux-and-why-it-exists-1437</guid>
      <description>&lt;p&gt;Hey coders! I hope you are doing well and building projects. As promised in the previous article, this is part 1 of our comprehensive guide on React Redux.&lt;/p&gt;

&lt;p&gt;In this article, we are going to understand What is redux? Why do we need redux? What are other available state management mechanisms? And what packages does the redux library have to offer?&lt;br&gt;
So let’s dig into it ….&lt;/p&gt;
&lt;h3&gt;
  
  
  Why do we need to manage the state?
&lt;/h3&gt;

&lt;p&gt;We know React offers certain hooks to locally manage the state in a component. For example, while creating forms we can manage the state of the form with the help of the useState() hook and accordingly perform functions.&lt;/p&gt;

&lt;p&gt;But sometimes we need to render UI or perform some actions based on the state which is used or declared in some other component.&lt;/p&gt;

&lt;p&gt;In other words, we need to manage a global state such that, every component has access to it and can use or update the state. And that updation of state by one component should immediately reflect in each and every component.&lt;/p&gt;

&lt;p&gt;That’s where global state management comes into the picture. We’ll look into state management in detail in the next article.&lt;/p&gt;
&lt;h2&gt;
  
  
  State Management without Redux
&lt;/h2&gt;

&lt;p&gt;1 &lt;strong&gt;useState()&lt;/strong&gt;&lt;br&gt;
   Although this is the best way to manage the state locally. But if some other component tries to access this state, we are helpless.&lt;br&gt;
&lt;/p&gt;

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

export default function counter(){

//local state management
const [data, setData] = useState("")

return(
&amp;lt;div&amp;gt;
&amp;lt;input type="text" value={data} onChange={(e)=&amp;gt;setData(e.target.value)} /&amp;gt;
&amp;lt;/div&amp;gt;
)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;2 &lt;strong&gt;ContextAPI&lt;/strong&gt;&lt;br&gt;
  ContextAPI is not a state management library, instead, it &lt;br&gt;
  is a mechanism based upon props drilling You can refer to &lt;br&gt;
  this &lt;a href="https://dev.to/piyushjaiswal1610/react-hooks-simplified-295"&gt;article&lt;/a&gt; to understand hooks, and here is &lt;br&gt;
  the &lt;a href="https://github.com/piyush-jaiswal-projects/react-hooks/blob/master/src/hooks/ContextAPI.js"&gt;implementation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;As you can see, it takes a certain line of code to prop drill. And it’s a good approach if we have to manage a bunch of components or our codebase is small. &lt;/p&gt;

&lt;p&gt;Now, imagine if we have a very huge code base and 100s of components, then we have to repeat this process for every component of each component tree. And this is pretty much hectic and repeated logic which is not desirable&lt;/p&gt;

&lt;p&gt;3 &lt;strong&gt;useReducer&lt;/strong&gt;&lt;br&gt;
You can see the useReducer implementation over here. UseReducer hook came into existence after a few years of the introduction of the redux library. &lt;/p&gt;

&lt;p&gt;useReducer is inspired by redux and works on the same pattern as redux but the problem remains untouched. useReducer() is more suitable for smaller-scale applications or local component state management. It follows the same pattern as Redux but on a smaller scale.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Redux? And how it manages the state?
&lt;/h2&gt;

&lt;p&gt;As per official Redux documentation,&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Redux is a pattern and library for managing and updating application state, using events called "actions". It serves as a centralized store for the state that needs to be used across your entire application, with rules ensuring that the state can only be updated in a predictable fashion.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Redux implements state management with the help of One Way Data Flow and Flux Pattern which we will cover in the next article i.e. Part 2 of this series.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Redux library offers?
&lt;/h2&gt;

&lt;p&gt;Redux in itself offers multiple packages. These are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;react-redux package to build react apps. It offers some hooks to read and update the state which we will see in upcoming articles.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;redux core package. This package consists of all the libraries, features, etc. It is used to write redux logic from scratch.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;redux toolkit. It is the most recommended way to implement redux logic. write redux logic from scratch consists of repeated boilerplate code. Hence to manage the state more efficiently redux toolkit is recommended.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But in order to build our understanding regarding redux, we must learn to write redux logic from scratch and then we can easily move on to redux toolkit&lt;/p&gt;

&lt;h2&gt;
  
  
  When and When Not to Use Redux?
&lt;/h2&gt;

&lt;p&gt;So basically we have 3 options available for managing the state:&lt;/p&gt;

&lt;p&gt;1 &lt;strong&gt;Context API&lt;/strong&gt;&lt;br&gt;
Use Context API when you have a small to medium-sized application with a few levels of nesting, and you need to share the state between components that are not directly connected in the component tree.&lt;/p&gt;

&lt;p&gt;It's useful for managing global or application-level states, such as user authentication, theme preferences, or language settings.&lt;/p&gt;

&lt;p&gt;Context API is built into React and doesn't require any additional libraries, making it lightweight and easy to use.&lt;/p&gt;

&lt;p&gt;However, it might not be the best choice for complex state management scenarios or applications with a large number of components, as it can lead to prop drilling and performance issues.&lt;/p&gt;

&lt;p&gt;2 &lt;strong&gt;useReducer&lt;/strong&gt;&lt;br&gt;
Use the useReducer hook when you have a local state that requires more complex state transitions, such as managing the state of a form, implementing undo/redo functionality, or handling a component with multiple states and actions.&lt;/p&gt;

&lt;p&gt;It provides a way to handle state updates through a reducer function, similar to how Redux works but on a smaller scale.&lt;br&gt;
useReducer is part of React's core library and can be used without any additional dependencies.&lt;/p&gt;

&lt;p&gt;While it is more powerful than a simple component-level state, it may become harder to manage for larger applications with more complex state interactions.&lt;/p&gt;

&lt;p&gt;3 &lt;strong&gt;Redux&lt;/strong&gt;&lt;br&gt;
Use Redux when you have a large application with complex state management needs, including advanced features like time travel debugging, middleware, and a centralized store. &lt;br&gt;
It's suitable for applications with many interconnected components and where the managing state becomes challenging using the Context API or useReducer alone.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Tu sum up, For small to medium-sized projects, Context API and useReducer might be sufficient. For larger and more complex applications, Redux could provide the necessary tools and scalability.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This sums up our first article of the series Mastering Redux. I hope you enjoyed learning about what is redux, why it exists, how it works, and when to use it.&lt;/p&gt;

&lt;p&gt;In the next article, we will learn about State Management in detail and see why immutability is an important concept in the working of redux.&lt;/p&gt;

&lt;p&gt;If you like my work, please follow me to never miss out on any of my well-crafted articles.&lt;br&gt;
You can also follow me on &lt;a href="https://github.com/piyush-jaiswal-projects"&gt;GitHub&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/piyushjaiswal1610/"&gt;LinkedIn&lt;/a&gt;. I will meet you in the next one.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>redux</category>
      <category>react</category>
      <category>webdev</category>
    </item>
    <item>
      <title>React Hooks Simplified</title>
      <dc:creator>Piyush Jaiswal</dc:creator>
      <pubDate>Fri, 09 Jun 2023 12:32:45 +0000</pubDate>
      <link>https://dev.to/piyushjaiswal1610/react-hooks-simplified-295</link>
      <guid>https://dev.to/piyushjaiswal1610/react-hooks-simplified-295</guid>
      <description>&lt;p&gt;If you have ever tried learning React, your brain might get stuck on React Hooks. But not anymore, I have explained important built-in react hooks in a very simplified manner with easy implementation. So let's dive deep into it…&lt;/p&gt;

&lt;p&gt;In very simplified terms, Hooks are reusable logic or javascript functions similar to the reusable components we build in React.&lt;/p&gt;

&lt;p&gt;Hooks are identified by their name which starts with “use”. For example, useState(use logic to maintain state), useEffect(use logic to perform some actions as an effect when something(s) changes), etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  useState
&lt;/h2&gt;

&lt;p&gt;In simple terms, useState is used to define variables whose values can be changed dynamically, and at the same time, those changes get reflected in your UI components.&lt;/p&gt;

&lt;p&gt;Normal variables do change dynamically but their state doesn’t get updated in your UI because UI is not automatically aware of the fact that the state is changed.&lt;/p&gt;

&lt;p&gt;Therefore, libraries such as react follow a unidirectional data flow, where changes to the data trigger updates to the UI. This means that you need to explicitly update the UI to reflect the changes in the variables.&lt;br&gt;
&lt;strong&gt;Example: Simple Up and Down Counter&lt;/strong&gt;&lt;br&gt;
Explanation: &lt;br&gt;
Whenever you click on the + or - button, you are trying to update the state of the variable dynamically with the help of the update function provided by useState hook. So whenever the state changes, the hook explicitly forces the updation of the component by re-rendering.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function UseStateHook() {
    const [data, setData] = useState({
        count: 0,
        id: 'X'
    });
&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;    const updateCount = (operator) =&amp;gt; {
        operator === '+'
            ?
            setData((prev) =&amp;gt; {
                return {
                    ...prev,
                    count: prev.count + 1
                }
            })
            :
            setData((prev) =&amp;gt; {
                return {
                    ...prev,
                    count: prev.count - 1
                }
            });

    }

    return (
        &amp;lt;div className="App"&amp;gt;
            &amp;lt;h2&amp;gt;Use State Hook&amp;lt;/h2&amp;gt;
            &amp;lt;br /&amp;gt;
            &amp;lt;button onClick={() =&amp;gt; { updateCount('-') }}&amp;gt;-&amp;lt;/button&amp;gt;
            &amp;lt;span&amp;gt;{data.count}&amp;lt;/span&amp;gt;
            &amp;lt;button onClick={() =&amp;gt; { updateCount('+') }}&amp;gt;+&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
    );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  useEffect
&lt;/h2&gt;

&lt;p&gt;Many times you want to perform some action, when some state or variable changes.&lt;br&gt;
This can be achieved by useEffect&lt;br&gt;
It triggers a function, whenever the state of dependencies passed by you changes.&lt;br&gt;
3 cases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;To run the function on every render&lt;br&gt;
&lt;/p&gt;

&lt;pre class="highlight plaintext"&gt;&lt;code&gt;```
useEffect(()=&amp;gt;{
//perform some action on every render
});
```
&lt;/code&gt;&lt;/pre&gt;




&lt;/li&gt;
&lt;li&gt;&lt;p&gt;To run the function only once&lt;br&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(()=&amp;gt;{
//perform some action only once
}, []);
//empty dependency array
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ol&gt;
&lt;li&gt;To run the function only when the passed dependencies gets changed.
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(()=&amp;gt;{
//perform some action only when dependency variables change
}, [dependencyVarOne, dependencyVarTwo]);
//non-empty dependency array
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;u&gt;Cleanup function&lt;/u&gt;: useEffect is generally used for side effect functions like subscriptions, data fetching, etc. So when the component gets unmounted it’s required to release or clean up the resources&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(()=&amp;gt;{
//side effect logic

return ()=&amp;gt;{
//cleanup logic
}
}, [/* dependencies */ ] );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Example:&lt;/strong&gt; When we change width of browser, as an effect the current width of browser gets displayed&lt;br&gt;
&lt;/p&gt;

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

function UseEffectHook() {
    const [width, setWidth] = useState(window.innerWidth);

    useEffect(() =&amp;gt; {
        window.addEventListener('resize', () =&amp;gt; {
            setWidth(window.innerWidth);
        })

        //cleanup
        return () =&amp;gt; {
            window.removeEventListener('resize', () =&amp;gt; {
                setWidth(0);
            });
        }
    }, [])
&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;    return (
        &amp;lt;&amp;gt;
            &amp;lt;h2&amp;gt;useEffect Hook&amp;lt;/h2&amp;gt;
            &amp;lt;br /&amp;gt;
            &amp;lt;p&amp;gt;
                Window Width:
                &amp;lt;span&amp;gt;
                    {width}
                &amp;lt;/span&amp;gt;
            &amp;lt;/p&amp;gt;
        &amp;lt;/&amp;gt;
    );
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  useMemo
&lt;/h2&gt;

&lt;p&gt;It is used to improve the performance of your React app.&lt;/p&gt;

&lt;p&gt;Sometimes, you have to perform functions such as sorting data, fetching a large amount of data, or some other kind of expensive functions&lt;br&gt;
In that case, you don’t want your app to perform that function again and again until it is really required to recalculate as it makes your app slow&lt;/p&gt;

&lt;p&gt;Here, useMemo hooks come into the picture, it caches the result of that expensive function and calls the function again only when dependencies change. Otherwise, it returns the previous cached result.&lt;br&gt;
&lt;strong&gt;Example&lt;/strong&gt;: In the following example, on initial render, both memoized and un-memoized function gets called.&lt;br&gt;
But when we click on the update button it should only count up the value by one but it is also calling unmemoized function as seen on the console&lt;br&gt;
But memoized function gets called only when the input location or vacancy gets updated&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const hotelData = useMemo(() =&amp;gt; {
        console.log("Memoized Function Call");
        //calling an API to get data
        return `Hotel Data ${userInput.location}  and ${userInput.vaccancy}`;
    }, [userInput.location, userInput.vaccancy])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Complete code for this example can be found &lt;a href="https://github.com/piyush-jaiswal-projects/react-hooks/blob/master/src/hooks/UseMemoHook.js"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  useCallback
&lt;/h2&gt;

&lt;p&gt;It is also a performance improvement hook&lt;br&gt;
It caches the entire function between re-renders but, it doesn’t call the function it only returns it.&lt;br&gt;
In some cases, your function call might get changed based on some variables otherwise function remains the same&lt;br&gt;
So for that purpose, useCallback can be used&lt;br&gt;
&lt;strong&gt;Example&lt;/strong&gt;: in this example, we have used useEffect() hook to compare whether our function gets changed. As you click on the update button, the state changes and component gets re-render but the function inside useCallback does not change&lt;br&gt;
However, if you change the dependency variable, the function inside the callback gets changed&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let func = useCallback((orderdetails) =&amp;gt; {
        //api call to post data or any other function for ${value}
        console.log(`Order Placed ${value}`);
        return value;
    }, [value])

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

&lt;/div&gt;



&lt;p&gt;Complete code for this example can be found &lt;a href="https://github.com/piyush-jaiswal-projects/react-hooks/blob/master/src/hooks/UseCallbackHook.js"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  useRef
&lt;/h2&gt;

&lt;p&gt;Suppose you want to keep a value or function for future reference and that value or function doesn’t affect the rendering of your component, then useRef() can be used to reference that value or function between re-renders.&lt;/p&gt;

&lt;p&gt;And later or you can use that, maybe for comparing, etc&lt;br&gt;
For example, we can make use of useRef() to compare functions as we have done in the example of callback&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;:, as you update the state using the update button nothing happens, but when you change the input value&lt;br&gt;
Two values get logged&lt;br&gt;
One is the previous value of a function and its result&lt;br&gt;
And then you get a new function and its result&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const initialFunc = useRef(func);

    useEffect(() =&amp;gt; {
        if (initialFunc.current !== func) {
            console.log("Function gets changed");
            initialFunc.current();
            func();
        }
    },[func])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Complete code for this example can be found &lt;a href="https://github.com/piyush-jaiswal-projects/react-hooks/blob/master/src/hooks/UseRefHook.js"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  forwardRef
&lt;/h2&gt;

&lt;p&gt;Sometimes you may want that your parent component can modify DOM or perform some action in the child component to which the parent doesn’t have direct access.&lt;br&gt;
In that case, you can make use of the useRef() hook to hold that state and forwardRef() to expose that part to your parent&lt;br&gt;
Example manipulating a child component from the parent component&lt;br&gt;
In this example, we are changing the type of input of an input tag from the parent element&lt;br&gt;
Initially, when you type in the input tag it takes normal text input but when you click on the button it converts to password type&lt;br&gt;
&lt;/p&gt;

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

// This example is referred from Official React Docs
// Create child component here or import it
const Child = forwardRef((props, ref) =&amp;gt; {
    return &amp;lt;input placeholder={props.placeholder} ref={ref} /&amp;gt;;
});

function ForwardRef() {
    const mainRef = useRef(null);

    function handleManipulation() {
        mainRef.current.setAttribute('type', 'password');
    }

    return (
        &amp;lt;div&amp;gt;
            &amp;lt;h2&amp;gt;Forward Ref: Manipulating Child Component From Parent&amp;lt;/h2&amp;gt;
            &amp;lt;br /&amp;gt;
            &amp;lt;Child placeholder="Enter Any Value..." ref={mainRef} /&amp;gt;
            &amp;lt;br /&amp;gt;
            {/* BUTTON OF PARENT TO MANIPULATE CHILD */}
            &amp;lt;button onClick={handleManipulation}&amp;gt;Convert Input type to Password&amp;lt;/button&amp;gt;
        &amp;lt;/div&amp;gt;
    );
}

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  useReducer
&lt;/h2&gt;

&lt;p&gt;This hook is used for state management. It allows you to define the initial state, define a function that will take the current state, and action performed as an argument, and contains the logic to update the state. It also has a dispatch function to help you update your state.&lt;/p&gt;

&lt;p&gt;DISPATCH FUNCTION It is used to update the state to a different value and triggers the re-render and specify the action that occurred. Dispatch function updates the state only for the next render, and you’ll get the old value before the re-render completes.&lt;/p&gt;

&lt;p&gt;REDUCER FUNCTION This function takes state and action as an argument. And the logic to update the state based upon action is present inside it. Action argument contains the action performed as defined by the dispatch function and state contains the current state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;: Let’s say a user logged in to our web app. He is not a subscriber hence the initial state is user type general but when he successfully subscribes, the state gets updated to the subscribed user, and accordingly UI changes.&lt;br&gt;
&lt;/p&gt;

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

function reducer(state, action) {
    if (action.type === "Subscribed") {
        return {usertype: "subscribed"}
    }
    else if (action.type === "Unsubscribed") {
        return {usertype: "unsubscribed"}
    }
    else if (action.type === "New") {
        return {usertype: "general"}
    }
    throw Error('Unknown Action');
}
&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;function UseReducerHook() {
    const [state, dispatch] = useReducer(reducer, { usertype: "general" });

    function handleClick(e) {
        switch (e.target.id) {
            case "Subscribe":
                dispatch({type: "Subscribed"})
                break;
            case "Unsubscribe":
                dispatch({type: "Unsubscribed"})
                break;
            default:
                dispatch({type: "New"})
                break;
        }
    }
return(
&amp;lt;div&amp;gt;
//code
&amp;lt;/div&amp;gt;
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Complete code can be found &lt;a href="https://github.com/piyush-jaiswal-projects/react-hooks/blob/master/src/hooks/UseReducerHook.js"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Context API
&lt;/h2&gt;

&lt;p&gt;It is another way of state management. It can be better understood by following explanation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;: It’s implementation can be understood by creating a dark and light mode functionality&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createContext, useState} from "react";
import Heading from "../component/heading";

const Theme = createContext(null);

function ContextAPI() {
    const [state, setState] = useState({
        theme: "light",
        text: "Welcome to light"
    })

    function handleTheme(e) {
        e.target.id === "Light"
            ?
            setState({
                "theme": "light",
                "text": "Welcome to light"
            })
            :
            setState({
                "theme": "dark",
                "text": "Welcome to darkness"
            })

    }

    return (
        &amp;lt;Theme.Provider value={{
            theme: state.theme,
            text: state.text
        }} &amp;gt;
            &amp;lt;div className={state.theme === 'dark' ? "dark" : "light"}&amp;gt;
            &amp;lt;Heading /&amp;gt;
            &amp;lt;button id="Light" onClick={handleTheme}&amp;gt;Light Mode&amp;lt;/button&amp;gt;
            &amp;lt;button id="Dark" onClick={handleTheme}&amp;gt;Dark Mode&amp;lt;/button&amp;gt;
            &amp;lt;/div&amp;gt;
        &amp;lt;/Theme.Provider&amp;gt;
    );
}

export {ContextAPI, Theme};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Code of child component Heading is :&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { useContext } from "react";
import { Theme } from "../hooks/ContextAPI";

function Heading() {
    const contentTheme = useContext(Theme);
    return &amp;lt;h1 className={contentTheme.theme === "dark" ? "dark" : "light"}&amp;gt;{contentTheme.text}&amp;lt;/h1&amp;gt;
}

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

&lt;/div&gt;



&lt;p&gt;So that's all from my side. I hope this article helped you to clear your understanding a bit. You can found the entire code over &lt;a href="https://github.com/piyush-jaiswal-projects/react-hooks"&gt;here&lt;/a&gt;&lt;br&gt;
I'll try to come up with more articles with improved explanation and examples.&lt;/p&gt;

</description>
      <category>react</category>
      <category>frontend</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Various Environments in Software Development</title>
      <dc:creator>Piyush Jaiswal</dc:creator>
      <pubDate>Sat, 25 Feb 2023 07:58:51 +0000</pubDate>
      <link>https://dev.to/piyushjaiswal1610/various-environments-in-software-development-449p</link>
      <guid>https://dev.to/piyushjaiswal1610/various-environments-in-software-development-449p</guid>
      <description>&lt;p&gt;The journey of a software, starts from the core idea and goes on to the deployment of the final product. &lt;/p&gt;

&lt;p&gt;As our app, website, web app, or any tech product starts receiving huge traffic, it becomes important to provide the reliable services. &lt;br&gt;
Any error or bug in the code will lead to servers crashing down, which may eventually lead to huge losses in terms of both finances and number of users. &lt;br&gt;
Thus, the deployment of a new code, or changes in existing code base must go through a set of procedures to ensure smooth and bug free experience. Therefore, we need different environments, and each has its own role to play.&lt;/p&gt;

&lt;h2&gt;
  
  
  There are basically 4 environments or stages:
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Development Environment&lt;/li&gt;
&lt;li&gt;Testing Environment&lt;/li&gt;
&lt;li&gt;Staging Environment&lt;/li&gt;
&lt;li&gt;Production Environment&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Development Environment
&lt;/h2&gt;

&lt;p&gt;If you have ever written code, then there is a high chance that you were working in Development environment. &lt;br&gt;
In this environment, it's basically about writing code to build your websites, apps, web-apps, extensions, and everything else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Testing Environment
&lt;/h2&gt;

&lt;p&gt;Now once you are done building your product, as a developer, you start writing testing scripts. &lt;br&gt;
In Test Scripts you write test cases, to test and to find bug in your own code so that you can correct them.&lt;br&gt;
Developers usually do minimal testing in this stage.&lt;/p&gt;

&lt;h2&gt;
  
  
  Staging Environment
&lt;/h2&gt;

&lt;p&gt;Once developers are done with developing and testing at their level, codebase is sent for testing in Staging Environment. &lt;br&gt;
At this stage, rigorous testing is done. &lt;br&gt;
There are basically two types of tetsing:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Automatic Testing&lt;/li&gt;
&lt;li&gt;Manual Testing&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For example your code is staged for first time, then you'll be having Stage 1 -&amp;gt; Version 1.0&lt;br&gt;
Now tester will come up with some improvements. Developer will implement those improvements and thus now you are in Stage 2 and product is in Version 2.0&lt;/p&gt;

&lt;p&gt;This stage continues in loop until a satisfactory Version is developed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Production Environment
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Anything Which goes into production, has to be defect free.&lt;/strong&gt;&lt;br&gt;
This is the simple rule that must be followed.&lt;/p&gt;

&lt;p&gt;Once previous stages are complete, code is deployed and is available for customers to use.&lt;/p&gt;

&lt;p&gt;So this is a small and crisp information on different environments. Hope you found it useful and interesting. If you do, please follow for more upcoming articles.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>architecture</category>
      <category>node</category>
      <category>devops</category>
    </item>
  </channel>
</rss>
