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 great background websites for every web developer and designer

πŸ§΅πŸ‘‡


1. Magic pattern

Beautiful pure CSS background patterns that you can actually use in your projects. They are highly customizable as well.

πŸ–‡οΈ
https://t.co/ZqczrPfkdZ


2. Cool backgrounds

Collection of cool backgrounds that you can add to blogs, websites, or as desktop and phone wallpapers

πŸ–‡οΈ https://t.co/df7VSb9t8H


3. Gradienta

Use pure CSS gradient backgrounds for your next website or app, as a JPG image or CSS code, no attribute required

πŸ–‡οΈ https://t.co/R58zrwpZCH


4 Mesh gradient

Creat aurora UI like gradients using few simple clicks

πŸ–‡οΈ https://t.co/vOHHDASr1V
7 amazing websites that will help you improve your CSS skills

πŸ§΅πŸ‘‡

1️⃣ Learn to code HTML and CSS

- Learn how to build beautiful and intuitive websites by way of clear and organized lessons

πŸ–‡οΈ
https://t.co/bl2rx2q8vk


2️⃣ CSS reference

- CSS Reference is a free visual guide to CSS. It features the most popular properties, and explains them with illustrated and animated content

πŸ–‡οΈ https://t.co/XAlj33g1Zk


3️⃣ CSS tutorial

- These tutorials on web design & CSS makes learning this stuff fun and easy

πŸ–‡οΈ https://t.co/y9vAj6fMGk


4️⃣ CSS Layouts

- This site teaches the CSS fundamentals that are used in any website's layout.

πŸ–‡οΈ https://t.co/n4EGLScpDF
Use these 6 AI websites to finish hours of work in minutes (take a look):

1. Sheet+

Become a spreadsheet master with Sheet+ AI-powered tools.

β€’ Convert text to formula
β€’ Convert formula to explanation
β€’ Debug formulas
β€’ Generate VBA code
β€’ Get instant answers to any spreadsheet question

πŸ”—
https://t.co/fWsAeKa6ko


2. DeepCode

DeepCode's AI-based code review notifies you about critical bugs in real time and shows you how to fix them.

πŸ”— https://t.co/dPTPq9syoy


3. Akkio

Grow your business with data-driven decisions.

Go from data to AI in 10 minutes β€” no code or data science skills required.

πŸ”— https://t.co/I29FIHDIs0


4. Autocode

Instant endpoints for webhooks, bots, and APIs.

Build and host Node.js endpoints in seconds.

Connect to your favorite APIs like Airtable, Discord, Notion, Slack, Webflow, and more.

πŸ”— https://t.co/uQZB0NCHJT
Five great icons websites for every web developer and designer ⏺️

A Thread πŸ§΅πŸ‘‡πŸ»


1️⃣ Icons 8

- Icons8 is just more than icons. You can download illustrations, vector images, music and much more

πŸ–‡οΈ
https://t.co/inHse0QXBh


2️⃣ Icon Monstr

- Black and white themed minimal icons which looks super great. You can also cuatomize the thickness

πŸ–‡οΈ https://t.co/dbQrJi0IHf


3️⃣ Icon Icons

- Over one thousand free icons which you can download as icons or images

πŸ–‡οΈ https://t.co/h5NlH5fGGr


4️⃣ Icons finder

Filter through the world’s largest marketplace for icons with flexibility and ease. Made up by submissions from top designers around the world, and curated by the team

πŸ–‡οΈ https://t.co/sqiZ6H9sR7

You May Also Like

I think a plausible explanation is that whatever Corbyn says or does, his critics will denounce - no matter how much hypocrisy it necessitates.


Corbyn opposes the exploitation of foreign sweatshop-workers - Labour MPs complain he's like Nigel

He speaks up in defence of migrants - Labour MPs whinge that he's not listening to the public's very real concerns about immigration:

He's wrong to prioritise Labour Party members over the public:

He's wrong to prioritise the public over Labour Party