React Hooks are the functions which "hook into" React state and lifecycle features from function components. Hooks allows you to manipulate state and other React feature without writing a class. Let's talk about widely used hook

useEffect hook at a glance ๐Ÿงต๐Ÿ‘‡๐Ÿป

useEffect hook is the heart of React functional components

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
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

3/15
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

{ 4 / 15 }
As you can see in the output the function is executed every single time my component re-render

{ 5 / 15 }
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

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

{ 7 / 15 }
๐Ÿšจ 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

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

{ 9 / 15 }
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

10/15
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

{ 11 / 15 }
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

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

{ 13 / 15 }
Check out the source code on this link in more accessible form

https://t.co/r9G6i7EAIW

{ 14 / 15 }
Awesome! I think we have covered everything related to useEffect hook. It is little tough so try to play around with the code. I hope you like this thread โค๏ธ

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
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.

๐Ÿ”—
12 websites that will help you learn web development faster (completely 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

More from Webdev

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
12 websites that will help you learn web development faster (completely 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

Ivor Cummins has been wrong (or lying) almost entirely throughout this pandemic and got paid handsomly for it.

He has been wrong (or lying) so often that it will be nearly impossible for me to track every grift, lie, deceit, manipulation he has pulled. I will use...


... other sources who have been trying to shine on light on this grifter (as I have tried to do, time and again:


Example #1: "Still not seeing Sweden signal versus Denmark really"... There it was (Images attached).
19 to 80 is an over 300% difference.

Tweet: https://t.co/36FnYnsRT9


Example #2 - "Yes, I'm comparing the Noridcs / No, you cannot compare the Nordics."

I wonder why...

Tweets: https://t.co/XLfoX4rpck / https://t.co/vjE1ctLU5x


Example #3 - "I'm only looking at what makes the data fit in my favour" a.k.a moving the goalposts.

Tweets: https://t.co/vcDpTu3qyj / https://t.co/CA3N6hC2Lq