React Hooks are so powerful and especially some additional hooks.

Do you know there is an alternative to useState called useReducer hook? It is used to manage more complex states.

Let's look into detail 🧵👇🏻

Usually, the useState hook is used to tackle states in React where you can pass the initial state and React preserves state for you between re-renders

What if you need to manage more complex states? Here useReducers comes into play
Similar to useState, the useReducer hook also returns a pair of values. Let's see what these two things are
The first value is `undefined` which is the current state (it is undefined because we didn't pass any initial state in the useReducer hook)

The second value is a dispatch function using which we can update our state.
The useReducer hook accepts 3 parameter

1. reducer
2. Initial state
3. init
Now we know that useReducer takes three parameters and return two values

Here is the complete syntax 👇🏻
Let's try to see this into action.

I just implemented a basic code here. Basically, I am calling dispatch function on button click which will eventually call reducer function
So far we haven't seen how the state can be managed using this.

Before that one thing to note here is that useReducer is for handling more complex states hence it is recommended to passing the current state as an object
Now we can simply update our state by passing the state parameter in the reducer function.

Something like this 👇🏻
Now every time I click on the button my state will be incremented by 1 because the reducer function returning the new state by adding one to the previous state.

Play around with it here: https://t.co/6BlaBeul1U
The reducer function accepts the second argument as well known as "action" using which we can tell the reducer function that what operations need to perform on the state.
Generally, we can pass different actions through dispatch to reducer via an object.
This is how you can pass different types of operations in the reducer function using the action parameter.
What if I want to pass some values through dispatch to the reducer function?

Here is "payload" comes into play. It is used to pass the value which represents the payloads of the action.

For example, suppose I want to pass "temp" variable 👇🏻

https://t.co/6BlaBeul1U
This may sound a little confusing for the first time. Don't worry I created a notes app for you using the useReducer hook.

Check out the code and try to play around with it for better understanding.

https://t.co/TCPEn0W3Pz
I have a video tutorial on YouTube explaining about useReducer in more bit details.

Check it out: https://t.co/nt4hZzInIR
That's pretty much it for this thread. If you like this thread, share it with your connections it means a lot to me 💖

Peace out 😉

More from Pratham

5 amazing websites that will blow the mind of a developer. Definitely check them out

🧵👇🏻

1️⃣ Animation generator

- Dead simple visual tools to help you generate CSS for your projects.

🔗
https://t.co/IFmIEgDiVY


2️⃣ Neural Network Visualizer

- Deep playground is an interactive visualization of neural networks, written in TypeScript using d3.js.

🔗 https://t.co/mTAlFbJsOW


3️⃣ Blockchain Demo

- A visual demo of blockchain technology

🔗 https://t.co/I1RwxYcM1Z


4️⃣ Developer Roadmaps

- Step by step guides and paths to learn different tools or technologies

🔗 https://t.co/VSNPdG8jQR
9 websites that will help you learn web development faster (they are free) 🧵

1. How HTTP Works

Everything you need to know about HTTP based system.

🔗
https://t.co/gVZS4RzS1a


2. 30 Days of Node

Learn Node step by step with interactive examples and code snippet in 30 days.

🔗 https://t.co/9nbtMiNB1C


3. How DNS Works

Learn what happens when you type a website address in your browser

🔗 https://t.co/SqMRNnDbc3


4. Git

Check out this excellent free website to learn git visually.

🔗 https://t.co/rQJMISBDfS
Six tools that will change the way your write code: 🧵

1️⃣ HarperDB

Deciding between SQL and NoSQL? Use HarperDB as it provides you the functionality of both.

Access data using API endpoint.

🔗
https://t.co/qf5CalexfA


2️⃣ Visual Go

Visualizing data structures and algorithms

🔗 https://t.co/b8YeMYdOaf


3️⃣ Extends Class

Syntax validators, code formatter, testers, HTTP clients, mock server, and much more.

🔗 https://t.co/GlKjoBnzqg


4️⃣ Codeshare

Share Code in Real-time with Developers. An online editor for teaching and interviewing.

🔗 https://t.co/2m1Ne3KJRK

You May Also Like

राम-रावण युद्ध समाप्त हो चुका था। जगत को त्रास देने वाला रावण अपने कुटुम्ब सहित नष्ट हो चुका था।श्रीराम का राज्याभिषेक हुआ और अयोध्या नरेश श्री राम के नेतृत्व में चारों दिशाओं में शन्ति थी।
अंगद को विदा करते समय राम रो पड़े थे ।हनुमान को विदा करने की शक्ति तो राम में थी ही नहीं ।


माता सीता भी हनुमान को पुत्रवत मानती थी। अत: हनुमान अयोध्या में ही रह गए ।राम दिनभर दरबार में, शासन व्यवस्था में व्यस्त रहते थे। संध्या को जब शासकीय कार्यों में छूट मिलती तो गुरु और माताओं का कुशल-मंगल पूछ अपने कक्ष में जाते थे। परंतु हनुमान जी हमेशा उनके पीछे-पीछे ही रहते थे ।


उनकी उपस्थिति में ही सारा परिवार बहुत देर तक जी भर बातें करता ।फिर भरत को ध्यान आया कि भैया-भाभी को भी एकांत मिलना चाहिए ।उर्मिला को देख भी उनके मन में हूक उठती थी कि इस पतिव्रता को भी अपने पति का सानिध्य चाहिए ।

एक दिन भरत ने हनुमान जी से कहा,"हे पवनपुत्र! सीता भाभी को राम भैया के साथ एकांत में रहने का भी अधिकार प्राप्त है ।क्या आपको उनके माथे पर सिन्दूर नहीं दिखता?इसलिए संध्या पश्चात आप राम भैया को कृप्या अकेला छोड़ दिया करें "।
ये सुनकर हनुमान आश्चर्यचकित रह गए और सीता माता के पास गए ।


माता से हनुमान ने पूछा,"माता आप अपने माथे पर सिन्दूर क्यों लगाती हैं।" यह सुनकर सीता माता बोलीं,"स्त्री अपने माथे पर सिन्दूर लगाती है तो उसके पति की आयु में वृद्धि होती है और वह स्वस्थ रहते हैं "। फिर हनुमान जी प्रभु राम के पास गए ।