<?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: krishnaPrasad2141</title>
    <description>The latest articles on DEV Community by krishnaPrasad2141 (@krishnaprasad2141).</description>
    <link>https://dev.to/krishnaprasad2141</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%2F178896%2F9d014a88-1f38-4085-a2b7-c0b42ff51467.png</url>
      <title>DEV Community: krishnaPrasad2141</title>
      <link>https://dev.to/krishnaprasad2141</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/krishnaprasad2141"/>
    <language>en</language>
    <item>
      <title>react-hooks: manage global state - no need of redux</title>
      <dc:creator>krishnaPrasad2141</dc:creator>
      <pubDate>Tue, 11 Jun 2019 07:11:33 +0000</pubDate>
      <link>https://dev.to/krishnaprasad2141/react-hooks-manage-global-state-no-need-of-redux-2072</link>
      <guid>https://dev.to/krishnaprasad2141/react-hooks-manage-global-state-no-need-of-redux-2072</guid>
      <description>&lt;p&gt;I know You Want to manage global state and using  redux is so 2018.&lt;br&gt;
Remember redux is great but if you don't have a requirenment don't use it at all.&lt;br&gt;
So is there is any way to manage global state without the use of redux.&lt;/p&gt;

&lt;p&gt;yes and the answer is "context api" and "useReducer".&lt;/p&gt;

&lt;p&gt;In this tutorial you will learn each and every thing you need to manage state globally in series of step.&lt;/p&gt;

&lt;p&gt;So lets begin.&lt;/p&gt;

&lt;p&gt;In App.tsx&lt;/p&gt;

&lt;p&gt;step 1. &lt;br&gt;
import  React , {useReducer} from “react”;&lt;br&gt;
Great Job!&lt;/p&gt;

&lt;p&gt;step 2. &lt;br&gt;
In your App.tsx call useReducer(countReducer , initialCount) with reducer and initial Count.&lt;/p&gt;

&lt;p&gt;oh! no! what the heck is reducer, from where will you get it.&lt;/p&gt;

&lt;p&gt;dont't worry reducer is just a function that takes two arguments (state , action)&lt;/p&gt;

&lt;p&gt;step 1.5.&lt;/p&gt;

&lt;p&gt;So let us create a reducer first okay &lt;/p&gt;

&lt;p&gt;const countReducer = (state, action) =&amp;gt; {&lt;br&gt;
  switch (action.type) {&lt;br&gt;
    case "increment":&lt;br&gt;
      return state + 1;&lt;br&gt;
      break;&lt;br&gt;
    case "decrement":&lt;br&gt;
      return state - 1;&lt;br&gt;
      break;&lt;br&gt;
    case "reset":&lt;br&gt;
      return 0;&lt;br&gt;
      break;&lt;br&gt;
    default:&lt;br&gt;
      return 0;&lt;br&gt;
      break;&lt;br&gt;
  }&lt;br&gt;
};&lt;br&gt;
const initialCount = 0;&lt;/p&gt;

&lt;p&gt;omg you have just created reducer and initial state &lt;br&gt;
isn't it fun&lt;/p&gt;

&lt;p&gt;step 2 again.&lt;/p&gt;

&lt;p&gt;call useReducer with countReducer and initialCount.&lt;br&gt;
and you will get current state and a dispatch method which will call reducer we create above.&lt;/p&gt;

&lt;p&gt;const [countState, countDispatch] = useReducer(countReducer , initialCount);&lt;br&gt;
useReducer take two arguments &lt;/p&gt;

&lt;p&gt;you might have one question why you use that ugly array kind of thing.&lt;br&gt;
believe me it is just syntaxtic sugar When i saw this i have the same feeling.&lt;/p&gt;

&lt;p&gt;so basically it is array destructing.&lt;br&gt;
It is not array just two normal variables&lt;/p&gt;

&lt;p&gt;instead of writing &lt;br&gt;
const countState = blablabla and &lt;br&gt;
const  countDispatch = blablabla&lt;/p&gt;

&lt;p&gt;i wrote const [countState, countDispatch] = blablabla&lt;/p&gt;

&lt;p&gt;it is just neat and clean&lt;/p&gt;

&lt;p&gt;useReducer returns an array of two elements &lt;br&gt;
state  --- represent current state&lt;br&gt;
dispatch  method -- to dispatch actions in reducer&lt;/p&gt;

&lt;p&gt;step 2 complete take some rest.&lt;/p&gt;

&lt;p&gt;step 3 create context.&lt;br&gt;
for this first import createContext from react&lt;/p&gt;

&lt;p&gt;import React, { useReducer, createContext } from "react";&lt;br&gt;
and create context.&lt;/p&gt;

&lt;p&gt;export const CounterContext = createContext(null);&lt;/p&gt;

&lt;p&gt;We can pass dispatch method from one component to another through context.Provider and update global state .&lt;/p&gt;


&lt;p&gt;In first file&lt;br&gt;&lt;br&gt;
import React, { useReducer, createContext } from "react";&lt;br&gt;&lt;br&gt;
const initialState = 0;&lt;br&gt;&lt;br&gt;
export const CounterContext = createContext(null);&lt;br&gt;&lt;br&gt;
const App: React.FC = () =&amp;gt; {&lt;br&gt;&lt;br&gt;
 const [count, dispatch] = useReducer(countReducer, initialState);&lt;br&gt;&lt;br&gt;
 return (&lt;br&gt;&lt;br&gt;
   &lt;/p&gt;
&lt;br&gt;&lt;br&gt;
     &lt;br&gt;&lt;br&gt;
       &lt;br&gt;&lt;br&gt;
     &lt;br&gt;&lt;br&gt;
     Count: {count}&lt;br&gt;&lt;br&gt;
);&lt;br&gt;&lt;br&gt;
};&lt;br&gt;&lt;br&gt;
export default App;&lt;br&gt;&lt;br&gt;
In second file&lt;br&gt;&lt;br&gt;
import React, { useContext } from "react";&lt;br&gt;&lt;br&gt;
import { CounterContext } from "./App";&lt;br&gt;&lt;br&gt;
export const Counter = () =&amp;gt; {&lt;br&gt;&lt;br&gt;
 const context = useContext(CounterContext);&lt;br&gt;&lt;br&gt;
 return (&lt;br&gt;&lt;br&gt;
   &lt;br&gt;&lt;br&gt;
     &lt;h1&gt;{context.count}&lt;/h1&gt;
&lt;br&gt;&lt;br&gt;
      context.dispatch({ type: "increment" })}&amp;gt;+&lt;br&gt;&lt;br&gt;
      context.dispatch({ type: "decrement" })}&amp;gt;-&lt;br&gt;&lt;br&gt;
      context.dispatch({ type: "reset" })}&amp;gt;0&lt;br&gt;&lt;br&gt;
     &lt;h2&gt;-----------------&lt;/h2&gt;
&lt;br&gt;&lt;br&gt;
   &lt;br&gt;&lt;br&gt;
 );&lt;br&gt;&lt;br&gt;
};

</description>
      <category>reacthooks</category>
      <category>redux</category>
      <category>state</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
