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

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
6 beginner friendly websites that will boost your HTML and CSS learning process 🚀

🧵👇🏻

1️⃣ Learn HTML

- The easiest way to learn HTML & CSS. Learn HTML provides an interactive tutorial that explains how to build HTML & CSS websites step by step.

🔗
https://t.co/W1XytKL1MI


2️⃣ HTML best practice

- A very well written document on HTML best practice

🔗 https://t.co/6PTcY1U5Cw


3️⃣ Learn to Code HTML & CSS

- Learn to Code HTML & CSS is a simple and comprehensive guide dedicated to helping beginners learn HTML and CSS

🔗 https://t.co/LwY9E2qUNS


4️⃣ Hex Invaders

- Hex Invaders is a fun way to learn and understand what hex codes are and how they work

🔗 https://t.co/EpQyhlN7pv
5 best color palette for every web developer and designer 🎨

🧵👇


1. Color hunt

- Color Hunt is a free and open platform for color inspiration with thousands of trendy hand- picked color palettes

🖇️
https://t.co/xR0FVTjK0L


2. Adobe color wheel

Explore and create accessible color palettes using color wheel, in variety of color variations and contrast levels. It will tell you automatically if two colors are not accessible

🖇️ https://t.co/ndsgBauTEc


3. Palette Ninja

Palette ninja is an online color scheme generator that allows you to create harmonious color schemes in seconds

🖇️ https://t.co/cuzP21tn2Z


4. My color space

Here you can find the perfect matching color scheme for your next project! Generate nice color palettes, color gradients and much more! Your space for everything that has to do with color

🖇️ https://t.co/kmjrXOceAj
6 websites that are so fantastic you'll wonder how they even exist (for developers):

1. ScribeHow (@ScribeHow)

Turn any process into a step-by-step guide, instantly.

• Click record
• Do the thing you want to show someone (like how to reset a password)
• Get a beautiful auto-generated guide

No manual screenshots.

🔗
https://t.co/8EnpUIvIeR


2. SourceAI

Explain what you want in plain English, and AI will generate a code snippet for you.

🔗 https://t.co/KFLbBBjllm


3. Blackbox

Turn any questions into a code snippet or extract a code snippet from a video.

🔗 https://t.co/6Z4fM5ieWn


4. Figstack

One-stop for developers.

• Understand code in any programming language
• Translate programming languages
• Automate documentation for your functions
• Get the time complexity of your program

🔗 https://t.co/koY4cmciR3
Top 5 free APIs for web developers

🧵👇

1️⃣ Custom Search API

- Create your own custom search engine. You can search for image, videos, and news.

🔗
https://t.co/nTvv3c3W06


2️⃣ Movie Database API

- Access movie and TV information similar to that of IMDB.

🔗

3️⃣ Dad Jokes API

- Get random endless dad jokes

🔗

4️⃣ COVID-19 Data API

- Every 15 minutes updated statistic about Coronavirus. Latest stats by country, are collected from several reliable sources.

🔗

You May Also Like

MDZS is laden with buddhist references. As a South Asian person, and history buff, it is so interesting to see how Buddhism, which originated from India, migrated, flourished & changed in the context of China. Here's some research (🙏🏼 @starkjeon for CN insight + citations)

1. LWJ’s sword Bichen ‘is likely an abbreviation for the term 躲避红尘 (duǒ bì hóng chén), which can be translated as such: 躲避: shunning or hiding away from 红尘 (worldly affairs; which is a buddhist teaching.) (
https://t.co/zF65W3roJe) (abbrev. TWX)

2. Sandu (三 毒), Jiang Cheng’s sword, refers to the three poisons (triviṣa) in Buddhism; desire (kāma-taṇhā), delusion (bhava-taṇhā) and hatred (vibhava-taṇhā).

These 3 poisons represent the roots of craving (tanha) and are the cause of Dukkha (suffering, pain) and thus result in rebirth.

Interesting that MXTX used this name for one of the characters who suffers, arguably, the worst of these three emotions.

3. The Qian kun purse “乾坤袋 (qián kūn dài) – can be called “Heaven and Earth” Pouch. In Buddhism, Maitreya (मैत्रेय) owns this to store items. It was believed that there was a mythical space inside the bag that could absorb the world.” (TWX)