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

65 JavaScript resources that can help you

Mega Thread ๐Ÿงต ๐Ÿ‘‡๐Ÿป

๐Ÿ”น Websites

1. Java5cript
https://t.co/2gdB6LdfQ8

2. JavaScript Tutorial
https://t.co/YyH7YEZtOL

3. JavaScript 30
https://t.co/X2e6T9qFW5

4. JavaScript Info
https://t.co/GLcMZmyfC9

5. JavaScript by

๐Ÿ”น Learn through visualizing

6. JS Visualizer
https://t.co/DKXfCA5bnv

7. UI dev visualizer
https://t.co/IsnNdAGANq

8. JS

๐Ÿ”น Documents

9. MDN
https://t.co/XkMlZQOF0h

10. W3 Schools
https://t.co/aJB14ha9KT

11. DevDocs
https://t.co/sVwCCrYKwA

12. JavaScript Info
https://t.co/VJaTfVOIa8

13. JavaScript

๐Ÿ”น GitHub repo

14. Modern JavaScript tutorials
https://t.co/vDgTJKbz74

15. Awesome JS Learning
https://t.co/CUCBjfJEcY

16. JS by example
https://t.co/VLT6dRwLSp

17. 33 JS concepts
https://t.co/F5KDOSaACD

18. 30 seconds of
If you start with the right course then the process becomes a little easier

I found some amazing YouTube videos and courses that will help you start your Web Development journey

๐Ÿงต๐Ÿ‘‡๐Ÿป

HTML and CSS

- A great way to arouse your web dev journey with Gary's (@designcoursecom) course on YouTube

๐Ÿ”—
https://t.co/xjm7nfV2L2


JavaScript

- Although it's impossible to learn JavaScript in 2 hours but JavaScript mastery is one the best YouTube chanel for JavaScript. This 2 hours long crash course will help you start your journey and gives you quick overview.

๐Ÿ”— https://t.co/1zcSeu4zKE


Git and GitHub

Git is an essential tool. And after learning JavaScript, I think one should go for Git and GitHub. Check out this free great course on Udemy

๐Ÿ”— https://t.co/E14cibOLXb


React

What you'll learn
- what problems React can solve
- how React solves those problems under the hood
- what JSX is and how it translates to regular JavaScript function calls and objects
- manage state with hooks
- build forms

๐Ÿ”— https://t.co/3z22aeVQFc

You May Also Like

Still wondering about this ๐Ÿค”


save as q