Everything you need to know about useState hook of React

A beginner's guide

Thread🧵👇

Hey👋

Hooks are powerful but confusing. Don't worry, I'll try to explain each hook in the easiest way in this thread series of React hooks

Let's start with useState, the most useful and simple hook in my opinion
Working with React hooks, first thing you need to do is to import the particular hook

It's quite easy👇

📌 import { useState } from "react";
useState hook takes a parameter as initial value of state and return an array having two values

- The first value is the current state
- The second value is the function that allow us to change our state

Let me show you the return value by printing it out on console👇
Now we know what useState hook return, it's time to destruct our value

const [currentState, setCurrentState] = useState(0);

🔹 currentState is the value of our state
🔹 setCurrentState is the function using which we can change our state value
Let's build a simple counter so that we can understand it effectively.
Though, useState has a very powerful use from this small counters to handling the large forms

Basic boilerplate code for counter👇
Now we want to increase our counter by one by user clicks on the "PLUS" button and decrease the value by one when user clicks on the "MINUS" button

Here value/counter is nothing but our current state which we want to change accordingly
Here setCurrentState function comes into play

- We will write a handlePlusButton function in order to increase the counter by 1 every time user click plus button

This is pretty easy just call the setCurrentState function and increase counter value just like this👇
As now you can see, everytime I click the "PLUS" button my counter increases by one (see attached video)

Here we are changing our counter(state) using setCurrentState(setState) function
The other way of updating our state is passing a function inside setCurrentState function.

The function that we pass inside setCurrentState will take one param which is nothing but the previous value of counter

Something like this👇
Similarly we can write function for "MINUS" button
Though there is a problem with updating our state like this 👇

setCurrentState(currentState + 1);

If you call setCurrentState function two times like this, it will still increase our counter by 1 (see attached image)
There is an other way to passing our initial state inside useState hook. Like this👇

📌useState(() => 0);

This prevent running our useState hook every single time we render our component. Hence by passing the value like this can speed up our app performance
I think that's pretty much it for useState hook . I hope you get some idea and basic overview.

Feel free to drop your doubts and suggestion❤️

Next I'll catch you with the useEffect thread

More from Pratham

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

You May Also Like

Fake chats claiming to be from the Irish African community are being disseminated by the far right in order to suggest that violence is imminent from #BLM supporters. This is straight out of the QAnon and Proud Boys playbook. Spread the word. Protest safely. #georgenkencho


There is co-ordination across the far right in Ireland now to stir both left and right in the hopes of creating a race war. Think critically! Fascists see the tragic killing of #georgenkencho, the grief of his community and pending investigation as a flashpoint for action.


Across Telegram, Twitter and Facebook disinformation is being peddled on the back of these tragic events. From false photographs to the tactics ofwhite supremacy, the far right is clumsily trying to drive hate against minority groups and figureheads.


Declan Ganley’s Burkean group and the incel wing of National Party (Gearóid Murphy, Mick O’Keeffe & Co.) as well as all the usuals are concerted in their efforts to demonstrate their white supremacist cred. The quiet parts are today being said out loud.


The best thing you can do is challenge disinformation and report posts where engagement isn’t appropriate. Many of these are blatantly racist posts designed to drive recruitment to NP and other Nationalist groups. By all means protest but stay safe.
महाभारत की कहानी कौन नहीं जानता।लेकिन क्या आपको पता है कि महाभारत के ज्यादातर पात्र किसी न किसी श्राप में फंसे थे।अगर ये श्राप न होते तो कदाचित महाभारत की कहानी कुछ और होती।हिन्दु पौराणिक ग्रंथों में विभिन्न श्रापों का वर्णन मिलता है व हर श्राप के पीछे कोई कहानी अवश्य होती है।


आइए आज जानते हैं महाभारत कथा में वर्णित कुछ श्रापों के बारे में।

1) राजा पाण्डु को ऋषि किन्दम का श्राप

एकबार महाराज पाण्डु शिकार खेलने वन गए।झाडियों के पीछे कुछ हिल रहा था। मृग है सोचकर राजा ने बाण चलाया जो जाकर ऋषि किन्दम और उनकी पत्नी को लगा।वे दोनो रति-क्रीड़ा में लिप्त थे।

जब राजा ने उन्हें देखा तो बहुत दुखी हुए कि ये मुझसे क्या पाप हो गया।बहुत क्षमा याचना के बाद भी किन्दम ऋषि ने पाण्डु को श्राप दे दिया कि जब भी वो किसी स्त्री को काम भावना से स्पर्श करेंगे उसी क्षण उनकी मृत्यु हो जाएगी।पश्चाताप करने, वे सिंहासन पे अन्धे राजा धृतराष्ट्र को बैठाकर...


..स्वयं अपनी रानियों कुंती व माद्री के साथ वन चले गए।पांडवों का जन्म भी कुंती को ऋषि दुर्वासा द्वारा दिए गए मंत्र से हुआ था जिसमे किसी भी देव का स्मरण कर उस देव से कुंती,पुत्र प्राप्त कर सकती थी।एक बार माद्री पे मोहित हो जब पांडु ने उसे स्पर्श किया,उसी क्षण पांडु की मृत्यु होगयी।


2) उर्वशी का अर्जुन को श्राप

महाभारत युद्ध से पहले जब अर्जुन दिव्यास्त्र प्राप्त करने स्वर्ग गए तो वहां उर्वशी नाम की अप्सरा उन पर मोहित हो गयी। अर्जुन ने जब उन्हें अपनी माता के समान बताया तो यह सुनकर उर्वशी क्रोधित हो गयी और अर्जुन को श्राप दे डाला कि तुम नपुंसक की भांति...