Everything you need to know about useState hook of React

A beginner's guide

Thread๐Ÿงต๐Ÿ‘‡ Image
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๐Ÿ‘‡ ImageImage
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 Image
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๐Ÿ‘‡ Image
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๐Ÿ‘‡ Image
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๐Ÿ‘‡ Image
Similarly we can write function for "MINUS" button Image
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) ImageImage
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 Image
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

โ€ข โ€ข โ€ข

Missing some Tweet in this thread? You can try to force a refresh
ใ€€

Keep Current with Pratham ๐Ÿ‘จโ€๐Ÿ’ป๐Ÿš€

Pratham ๐Ÿ‘จโ€๐Ÿ’ป๐Ÿš€ Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

More from @Prathkum

31 Jan
Positioning in CSS allows you to display your element wherever you want on the screen

But when I was learning it, I found it little bit confusing๐Ÿ˜…

So in this thread I'll try to explain it in easiest manner with practical implementation. Let's start

THREAD๐Ÿงต๐Ÿ‘‡ Image
There are 5 values that you can pass in position property

- static
- relative
- absolute
- fixed
- sticky

In this thread we will be focusing on relative and absolute positioning as both are widely used
Let's start with understanding what document flow is?

๐Ÿ“Œ Elements are displayed on the screen as they written in the HTML document

Consider the following piece of code:

H1, P, H3 and div are displayed on the screen in exact order as they written in the HTML file. ImageImage
Read 14 tweets
27 Jan
Complete Beginner's guide to React in 2021โš›๏ธ

A Long Thread๐Ÿงต
If you just started or planning to getting started with React, this thread might help you.

In this thread I'll try to give you a quick overview to the world of React.

So if you find that sound interesting, prepare a coffee and give this thread a read๐Ÿ˜‰
Before you go further into the React, make sure to check these things

โœ… Basic knowledge of HTML and CSS
โœ… JavaScript fundamentals and ES6 features

I would like to suggest you build a decent hold on JavaScript concepts because that is the backbone of React
Read 35 tweets
26 Jan
50 Resources for learning React in 2021โš›๏ธ

Thread๐Ÿงต๐Ÿ‘‡
๐Ÿ“Œ DOCUMENTATION

1. React official docs
reactjs.org/docs/getting-sโ€ฆ

2. MDN docs
developer.mozilla.org/en-US/docs/Leaโ€ฆ

3. W3 Schools
w3schools.com/react/
Read 18 tweets
24 Jan
63 RESOURCES OF CSS๐ŸŒˆ๐Ÿ‘‡

Thread๐Ÿงต
๐Ÿ“Œ COLORS

1. Color Hunt
colorhunt.co

2. Coolors
coolors.co

3. HTML color codes
htmlcolorcodes.com

4. UI Gradients
uigradients.com/#Bupe

5. Gradient generator
cssgradient.io
6. Encycolorpedia
encycolorpedia.com

7. WebFx
webfx.com/web-design/colโ€ฆ

8. Radial Gradient
css-gradient.com

9. COLORS
clrs.cc

10. Flat UI colors 2
flatuicolors.com
Read 14 tweets
23 Jan
Let's create this LOADING animation in less than 15 minutes๐ŸŒˆ

THREAD๐Ÿงต
The HTML structure is pretty straight forward

- Create a container div

- Create 7 different span elements for each letter in the word "LOADING"
- Centralize everything, I have used positioning in order to center my elements but you can use any method you want
Read 8 tweets
23 Jan
50 resources for learning JavaScript in 2021๐Ÿ‘‡

Thread๐Ÿงต
In this tweet you will find resources in various form

- Documentation
- Courses
- Websites
- YouTube Videos
- Books
- GitHub Repos
- Interactive learning
- Projects
๐Ÿ“Œ DOCUMENTATION

1. MDN
developer.mozilla.org/en-US/docs/Webโ€ฆ

2. W3 Schools
w3schools.com/js/

3. DevDocs
devdocs.io/javascript/

4. JavaScript Info
javascript.info

5. JavaScript Garden
bonsaiden.github.io/JavaScript-Garโ€ฆ
Read 13 tweets

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Too expensive? Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal Become our Patreon

Thank you for your support!

Follow Us on Twitter!