<?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: Devyank Nagpal</title>
    <description>The latest articles on DEV Community by Devyank Nagpal (@devyank_nagpal_c746402a7c).</description>
    <link>https://dev.to/devyank_nagpal_c746402a7c</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%2F645758%2F1c4daf9a-d55c-4d32-ac80-8473dad0e312.png</url>
      <title>DEV Community: Devyank Nagpal</title>
      <link>https://dev.to/devyank_nagpal_c746402a7c</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/devyank_nagpal_c746402a7c"/>
    <language>en</language>
    <item>
      <title>REDUX</title>
      <dc:creator>Devyank Nagpal</dc:creator>
      <pubDate>Wed, 19 Jan 2022 19:53:31 +0000</pubDate>
      <link>https://dev.to/devyank_nagpal_c746402a7c/redux-3deb</link>
      <guid>https://dev.to/devyank_nagpal_c746402a7c/redux-3deb</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;A predictable state application for JavaScript application&lt;br&gt;
In Simple words-it provide one single place to store all the states and that place is known as store&lt;br&gt;
Store : It is a single state object responsible for entire state of your application .This mean if you have react app with some components and each have its own state ,then the entire react state will be managed by the store. That means if we have to make changes in any of the state we can do it through the store only.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Data in redux flow unidirectional&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%2F8w6c64zrryxnlvmiw6kv.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%2F8w6c64zrryxnlvmiw6kv.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h1&gt;
  
  
  Main Topics in Redux
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Actions&lt;/strong&gt;&lt;br&gt;
Actions are basically JavaScript object .They just tell us what type of event is to be performed but they don’t tell us how to perform that particular event.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reducers&lt;/strong&gt;&lt;br&gt;
These are basically the functions which perform the operation according to the type of action ,&lt;br&gt;
it takes state and action as argument and return the updated state.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Store&lt;/strong&gt;&lt;br&gt;
The redux store brings together state , reducers and actions . This basically keeps the react-redux family together 💙&lt;/p&gt;

&lt;p&gt;Syntax to declare store-&lt;code&gt;const store=Redux.createStore(reducer)&lt;/code&gt;;&lt;/p&gt;

&lt;p&gt;To declare store in redux we make use of&lt;code&gt;createStore&lt;/code&gt; method it takes reducer as an argument&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%2Fqnxy4og4rteero0mub74.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%2Fqnxy4og4rteero0mub74.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In my opinion learning is better when we learn something by working on it . So let my dive you through the counter project with the help of which your understanding of redux will hopefully get better.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Firstly will be creating our Actions&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;export const Incnumber=()=&amp;gt;{
return {
type:'increment'
}
}
export const Decnumber=()=&amp;gt;{
return {
type:'decrement'
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So as you can see here I have created two function for incrementing(&lt;code&gt;Incnumber&lt;/code&gt;) and decrementing(&lt;code&gt;Decnumber&lt;/code&gt;) the value. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Now moving to the next step creating the reducer file
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const initialstate=0;
const changethenumber=
(state=initialstate,action)=&amp;gt;
switch(action.type){
case 'increment':
return state+1;
case 'decrement':
return state-1;
default : 
return state;
}
}
export default changethenumber;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So as we can see here reducer function taking argument state and action then according to the type of action  changing the state.&lt;/p&gt;

&lt;p&gt;Generally in complex application there are many reducer so to combine the all reducers into single reducer to send them to the &lt;code&gt;createStore&lt;/code&gt; we make use of   &lt;code&gt;combineReducers&lt;/code&gt;.In order to do this will have to import&lt;code&gt;combineReducers&lt;/code&gt; from redux.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import changethenumber from './Updown'
import { combineReducers } from "redux";
const rootreducers=combineReducers({changethenumber})
export default rootreducers;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As we are set with Actions and Reducers its time to pass our reducer to store&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { createStore } from "redux";
import rootreducers from "./reducers";
const store=createStore(rootreducers, 
window.__REDUX_DEVTOOLS_EXTENSION__ &amp;amp;&amp;amp; window.__REDUX_DEVTOOLS_EXTENSION__());
export default store;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here first we are importing &lt;code&gt;createStore&lt;/code&gt; from redux  as we know it is used to define store and takes reducer as an argument.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;After setting our action ,reducer and store its time to provide our store to app&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So, in this step will make use of  component it males redux store available for any nested component.&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 from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import store from './store';
import {Provider} from 'react-redux'

ReactDOM.render(
&amp;lt;React.StrictMode&amp;gt;
&amp;lt;Provider store={store}&amp;gt;
&amp;lt;App /&amp;gt;
&amp;lt;/Provider&amp;gt;
&amp;lt;/React.StrictMode&amp;gt;,
document.getElementById('root'));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;as we have  provided our store to app now we can access any state from the app.js itself .Once we are done with this, with the help of redux dev tool we can check the action ,state of our app in the console . As shown in the given image its showing our &lt;code&gt;changethenumber&lt;/code&gt; reducer.&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%2F6oyaxbrdd7xefhc1zeup.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%2F6oyaxbrdd7xefhc1zeup.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Here comes the use of &lt;code&gt;useSelector&lt;/code&gt; and &lt;code&gt;useDispath&lt;/code&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;useSelector&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;It takes state as an argument and return the data from  state &lt;/p&gt;
&lt;h3&gt;
  
  
  &lt;code&gt;useDispatch&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;It plays the important role it takes action object as an argument ,whenever we pass an action object the dispatch calls our reducer and  passes in the current state to make changes in that state.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import './App.css';
import { useDispatch,useSelector } from 'react-redux';
import {Incnumber,Decnumber} from './actions/index'
function App() {
const mystate=useSelector((state)=&amp;gt;state.changethenumber)
const dispatch=useDispatch();
return 
(
&amp;lt;div className="container"&amp;gt;&amp;lt;h1&amp;gt;Increament/Decreament Counter&amp;lt;/h1&amp;gt;
&amp;lt;h4&amp;gt;Using React and Redux&amp;lt;/h4&amp;gt;
&amp;lt;div className="quantity"&amp;gt;
&amp;lt;a 
className='quantity_minus' 
title='Decreament'
 onClick={()=&amp;gt;dispatch(Decnumber()) }&amp;gt;
&amp;lt;span&amp;gt;-&amp;lt;/span&amp;gt;
&amp;lt;/a&amp;gt;
&amp;lt;input name='quantity' className='quantity_input' type="text" value={mystate} /&amp;gt;
&amp;lt;a 
onClick={()=&amp;gt;dispatch(Incnumber()) } 
className='quantity_plus'
 title='Increament'&amp;gt;
&amp;lt;span&amp;gt;+&amp;lt;/span&amp;gt;
&amp;lt;/a&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;As we can clearly observe in the app.js file that with the help of &lt;code&gt;useSelector&lt;/code&gt; we have passed our &lt;/p&gt;

&lt;p&gt;&lt;code&gt;changethenumber&lt;/code&gt;  reducer and with the help of dispatch we have passed the action object.&lt;/p&gt;

&lt;p&gt;💙&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Context Hook</title>
      <dc:creator>Devyank Nagpal</dc:creator>
      <pubDate>Sun, 16 Jan 2022 07:22:06 +0000</pubDate>
      <link>https://dev.to/devyank_nagpal_c746402a7c/context-hook-47cp</link>
      <guid>https://dev.to/devyank_nagpal_c746402a7c/context-hook-47cp</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;It helps in providing data to components.&lt;br&gt;
Generally in react what happens that whenever we our working on a small project the data is passed from parent to child , where we have one or two component we can easily make use of props to pass the data.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But when it comes to big project when there are no of components and we have to pass the data which will be used by almost every component or we don’t have to pass data in hierarchy then passing data through the props become little bit unmanageable so in order to prevent this context provides a way to pass the data to different components&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Ie6yOFHU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/71ncsoonurgdcz3evequ.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Ie6yOFHU--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/71ncsoonurgdcz3evequ.png" alt="Image description" width="695" height="329"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;context is majorly used when the data is global let me take you through an example suppose you are calling an Api  and different components are accessing different data of the Api then you just can’t manually pass the data through props you need a proper system to manage your data flow so that it can be easily accessed by every component.&lt;/p&gt;
&lt;h3&gt;
  
  
  When To use Context?
&lt;/h3&gt;

&lt;p&gt;the image shown below is enough to tell you when to make use of it&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2iBJw70z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avcl968omros3za6llza.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2iBJw70z--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/avcl968omros3za6llza.png" alt="Image description" width="695" height="469"&gt;&lt;/a&gt;&lt;br&gt;
let me dive you through a project with which your understanding towards it will get better and will also be introducing you to the &lt;code&gt;createcontext&lt;/code&gt; and &lt;code&gt;context.provider&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;import React, { createContext, useContext, useEffect, useState } from 'react'
// import { useNavigate } from 'react-router-dom';


const Food=createContext();

const key="d90a7bf3";
const API="b1d3f3b888a6b98ec1aa24c610480f2f";
const FoodContext = ({children}) =&amp;gt; {

    const [recipes, setrecipe] = useState([]);
    const [search, setsearch] = useState("");
    const [query, setquery] = useState('');
// const navigate=useNavigate();

    useEffect(()=&amp;gt;{
        getrecipes();
      },[query]);

      const updatesearch=e=&amp;gt;{
        setsearch(e.target.value);

      }
      const getrecipes=async()=&amp;gt;{
      const response =await fetch(`https://api.edamam.com/search?q=${query}&amp;amp;app_id=${key}&amp;amp;app_key=${API}`);
      const data=await response.json();
      setrecipe(data.hits);
      }

    return (
        &amp;lt;Food.Provider value={{setquery,recipes,search,setsearch,updatesearch}}&amp;gt;
            {children}
        &amp;lt;/Food.Provider&amp;gt;
    )
}

export default FoodContext

export const FoodState=()=&amp;gt;{
    return useContext(Food);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is our foodcontext.js  &lt;/p&gt;

&lt;p&gt;&lt;code&gt;const Food=createContext();&lt;/code&gt; talking about this here we are doing nothing just calling api &lt;code&gt;createContext&lt;/code&gt; from react.&lt;/p&gt;

&lt;p&gt;Moving further as you can see we are making an api call and accessing the data from the api which is to be accessed by different components.&lt;/p&gt;

&lt;p&gt;Here comes the &lt;code&gt;Food.Provider&lt;/code&gt; basically in context with the help of it we pass the data to the descendants . It takes value as an prop where we refer to the data which we have to actually pass to the components.&lt;/p&gt;

&lt;p&gt;Now comes the &lt;code&gt;useContext&lt;/code&gt; so up till now we have done everything we have call the api  and passed the data but the most important thing left that we also have to use that data here &lt;code&gt;useContext&lt;/code&gt; plays a significant role it helps in using the data which is passed through the context. &lt;/p&gt;

&lt;p&gt;Once we are done with all this we have to put our app.js inside the &lt;code&gt;FoodContext&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;import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import FoodContext from './FoodContext';
// import reportWebVitals from './reportWebVitals';

ReactDOM.render(
  &amp;lt;React.StrictMode&amp;gt;
    &amp;lt;FoodContext&amp;gt;
    &amp;lt;App /&amp;gt;
    &amp;lt;/FoodContext&amp;gt;
  &amp;lt;/React.StrictMode&amp;gt;,
  document.getElementById('root')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When we are done with all this we are finally left with using the data in a particular component&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 from 'react'
import { useNavigate } from 'react-router-dom';
// import { Link } from 'react-router-dom';
import { FoodState } from '../FoodContext'
// import * as React from 'react';

const Header = () =&amp;gt; {
    const navigate=useNavigate();

    const {setquery,setsearch,search,updatesearch}=FoodState();

    const getsearch=(e)=&amp;gt;{
        e.preventDefault();
        setquery(search);
       navigate("/food");
        setsearch('');
      }

    return (
        &amp;lt;div className="navbar"&amp;gt;
&amp;lt;div className="text"&amp;gt;
    Nagpal's 
&amp;lt;/div&amp;gt;
&amp;lt;div className="search"&amp;gt;

        &amp;lt;form  onSubmit={getsearch}  className="searchfrom"&amp;gt;
        &amp;lt;input className="searchbar" type={search} onChange={updatesearch} /&amp;gt;
        &amp;lt;button   className="searchbutton"&amp;gt;Search&amp;lt;/button&amp;gt;

      &amp;lt;/form&amp;gt;
&amp;lt;/div&amp;gt;
        &amp;lt;/div&amp;gt;

    )
}

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

&lt;/div&gt;



&lt;p&gt;So as you can see here we are accessing the data from the context  by &lt;code&gt;destructuring&lt;/code&gt; the data from &lt;code&gt;FoodState()&lt;/code&gt;.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>webdev</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
