Beginner's guide to the most widely used hooks of React

The useState and useEffect hook

A long thread๐Ÿงต๐Ÿ‘‡๐Ÿป
Start with the useState hook

- You will use it almost 100% in your component. The useState hook is not so tough to understand but there is some hiddent concepts, Let's start ๐Ÿ‘‡๐Ÿป

{ 2 / 27 }
Working with React hooks, first thing you need to do is to import the particular hook

It's quite easy๐Ÿ‘‡

๐Ÿ“Œ import { useState } from "react";

{ 3 / 27 }
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 the 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

{ 5 / 27 }
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๐Ÿ‘‡

{ 6 / 27 }
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

{ 7 / 27 }
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๐Ÿ‘‡

{ 8 / 27 }
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

{ 9 / 27 }
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๐Ÿ‘‡

{ 10 / 27 }
Similarly we can write function for "MINUS" button

{ 11 / 27 }
Let's see what 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)

{ 12 / 27 }
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

{ 13 / 27 }
Alright that was pretty much it for useState hook. Let's move onto useEffect hook now ๐Ÿ˜

{ 14 / 27 }
If you're familiar with class components then you might know that we have various lifecycle methods but in functional components, we don't have any lifecycle methods.

Instead we have a powerful hook called useEffect๐Ÿ’ช

{ 15 / 27 }
By using useEffect, you tell React that your component needs to do something after render. React will remember the function you passed (we'll refer to it as our โ€œeffectโ€), and call it later after performing the DOM updates

So let's start by understanding the syntax first๐Ÿ‘‡
useEffect take two parameters, first is a function and second is an array.

The function inside the useEffect will run every single time component re-render.

Consider this piece of code and check the output in next tweet

{ 17 / 27 }
As you can see in the output the function is executed every single time my component re-render๐Ÿ‘‡

{ 18 / 27 }
But let's say if I add some dependency in the array and pass the array as second parameter then useEffect will only run when the value of dependency array change.ย 

For example let me modify the code little bit so that you can understand it better

{ 19 / 27 }
As you can see when I click on the re-render button, our useEffect run this is because I have passed render state inside dependency array๐Ÿ‘‡

{ 20 / 27 }
๐Ÿ“Œ Here's an important thing to note is that if you pass an empty array then it will only run on once.ย 
No matter how many times you render your component, the useEffect will run only once because the value of empty array never going to changeย 

{ 21 / 27 }
In useEffect we can also perform clean up

If we return a function within the method, this function perform basically a clean up of what we did last time.

{ 22 / 27 }
For example, consider this piece of code

useEffect(() => {

ย  ย  console.log({ render });

ย  ย  return () => {
ย  ย  ย  console.log("I'm cleanup function");
ย  ย  };
ย  }, [render]);

Everytime I click the button, first our useEffect perform clean up then run the effect function

23/27
So far we have discussed the basics of useEffect.ย 

Let's build something useful using it. We will be using useEffect for fetching some COVID data

{ 24 / 27 }
We will print total number of confirmed COVID cases of a specific country enter by user in the input field.

On the basis of the value entered by user we will store that in "country" and change that value in our API link

{ 25 / 27 }
- Make an input fieldย ย 
- on form submit, store the input value in "country"ย 

Print the confirmed cases on screen as simple as that๐Ÿคฉ

check the entire code๐Ÿ‘‡

{ 26 / 27 }
Great! that's pretty much it for this thread.ย 

I hope you get a basic overview of both the hooks. Did I miss something? Feel free to add your suggestions down below๐Ÿ˜‰

Peace out โค๏ธ

โ€ข โ€ข โ€ข

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

24 Mar
If you know CSS then you can use these amazing generators and save your time

Thread ๐Ÿงต๐Ÿ‘‡๐Ÿป
1๏ธโƒฃ CSS Box Shadow Generator

- Generate CSS3 Box Shadow code for your Div, Frame, Buttons or any other HTML element with Outline, and Inset (inner) type shadow effects

๐Ÿ”— cssboxshadow.com Image
2๏ธโƒฃ Glassmorphism

- Generate glassmorphic design easily

๐Ÿ”— glassgenerator.netlify.app Image
Read 6 tweets
24 Mar
Well all that we have covered so far we can do that using flexbox also.

Let's understand the 2 dimensions of grid layout

{ 14 / 21 } Image
We can change the position of a particular items in accordance of row and column

For example, I want my first item taking up entire row, that is from first column to last column

grid-column: 1 / 4;

{ 15 / 21 } ImageImage
Alright moving forward, The next property we have is grid-template-areas which specifies the areas within the grid layout.

Each row is defined by apostrophes (' ')

Sounds confusing? Let see this in action

{ 16 / 21 }
Read 8 tweets
24 Mar
A complete beginner's guide to CSS Grid layout ๐Ÿ‘‡๐Ÿป

Thread๐Ÿงต Image
Grid is used for making complex web design layouts more easily as it's not so hard to master

Using Flex you can make only 1D layout but Grid gives you full power of creating 2D layout

Let's start

{ 2 / 21 }
First things first, start with giving the display property "grid" to the container element or parent element

{ 3 / 21 } Image
Read 13 tweets
23 Mar
JavaScript is the most important language for web development. Let's see how you can start learning it

A THREAD๐Ÿงต
In this thread, we will cover some quick points so that you will be able make functional websites within 10 - 15 days.

However JavaScript is much more than this. But this thread will give you a quick overview

{ 2 / 18 }
First things first, whenever I'm up to learning new technology or language, I always start with a crash course. By doing so I get a quick overview of things in almost no time

{ 3 / 18 }
Read 18 tweets
22 Mar
These 10 web development code snippet websites can save you a lot of time

A thread ๐Ÿงต
1๏ธโƒฃ Code My UI

- Handpicked collection of Web Design & UI Inspiration with Code Snippets.

๐Ÿ”— codemyui.com
2๏ธโƒฃ Creatives Feed

- CreativesFeed works to share the best free code snippets and web development resources online.

๐Ÿ”— creativesfeed.com/topics/code-snโ€ฆ
Read 11 tweets
21 Mar
Let's create some easy and complex shapes using pure CSS ๐ŸŽจ

A Thread ๐Ÿงต
1. Circle

- Pretty simple, we just need to make a square and apply the border-radius 50% in order to give it a circular shape
2. Semi-circle

- Create a rectangle
- Apply border radius top left and top right same as height of the rectangle
Read 16 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!