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
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 🧵👇🏻
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
So let's start by understanding the syntax first
3/15
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 }
{ 5 / 15 }
For example, let me modify the code little bit so that you can understand it better
{ 6 / 15 }
{ 7 / 15 }
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 }
If we return a function within the method, this function perform basically a clean up of what we did last time.
{ 9 / 15 }
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
Let's build something useful using it. We will be using useEffect for fetching some COVID data
{ 11 / 15 }
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 }
- 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 }
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
🧵👇
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
Six tools that will change the way your write code: 🧵
1️⃣ HarperDB
Deciding between SQL and NoSQL? Use HarperDB as it provides you the functionality of both.
Access data using API endpoint.
🔗 https://t.co/qf5CalexfA
2️⃣ Visual Go
Visualizing data structures and algorithms
🔗 https://t.co/b8YeMYdOaf
3️⃣ Extends Class
Syntax validators, code formatter, testers, HTTP clients, mock server, and much more.
🔗 https://t.co/GlKjoBnzqg
4️⃣ Codeshare
Share Code in Real-time with Developers. An online editor for teaching and interviewing.
🔗 https://t.co/2m1Ne3KJRK
1️⃣ HarperDB
Deciding between SQL and NoSQL? Use HarperDB as it provides you the functionality of both.
Access data using API endpoint.
🔗 https://t.co/qf5CalexfA
2️⃣ Visual Go
Visualizing data structures and algorithms
🔗 https://t.co/b8YeMYdOaf
3️⃣ Extends Class
Syntax validators, code formatter, testers, HTTP clients, mock server, and much more.
🔗 https://t.co/GlKjoBnzqg
4️⃣ Codeshare
Share Code in Real-time with Developers. An online editor for teaching and interviewing.
🔗 https://t.co/2m1Ne3KJRK
7 GitHub repositories will make you a standout developer from 99% of people:
1. Memphis
An open-source real-time data processing platform.
• Message broker
• Schema Management
• Zero ops
• Cloud-native
• Rapid development
• Significantly lower development time
🔗 https://t.co/w5L2aCM3do
2. The art of command line
Master the command line, in one page.
🔗 https://t.co/uF2RlcUIxb
3. Public APIs
A collective list of free APIs for use in software and web development
🔗 https://t.co/vDRQKBf15V
4. Hover
A collection of CSS3 powered hover effects to be applied to links, buttons, logos, SVG, featured images and so on.
🔗 https://t.co/bIGEEPcMkJ
1. Memphis
An open-source real-time data processing platform.
• Message broker
• Schema Management
• Zero ops
• Cloud-native
• Rapid development
• Significantly lower development time
🔗 https://t.co/w5L2aCM3do
2. The art of command line
Master the command line, in one page.
🔗 https://t.co/uF2RlcUIxb
3. Public APIs
A collective list of free APIs for use in software and web development
🔗 https://t.co/vDRQKBf15V
4. Hover
A collection of CSS3 powered hover effects to be applied to links, buttons, logos, SVG, featured images and so on.
🔗 https://t.co/bIGEEPcMkJ
More from Webdev
23 FREE SVG Icon Libraries For Front-End Developers
Mega Thread 🧵👇
1️⃣ Ionicons: The premium icon pack for Ionic Framework
Ionicons is an open-sourced and MIT-licensed icon pack.
2️⃣ 700+ CSS Icons, Customizable, Retina Ready & API
Pure CSS icons library, Customizable & Retina-Ready built 100% in pure CSS, SVG, SVG Sprite, styled components, Figma, and Adobe XD. Easy integration: Embed, NPM & API
3️⃣ SVG Repo - Free SVG Vectors and Icons
Free Vectors and Icons in SVG format. Download free mono or multi-color vectors for commercial use. Search in 300.000+ Free SVG Vectors and Icons.
https://t.co/2Ht2sI23TE
4️⃣ Feather – Simply beautiful open source icons
Feather is a collection of simply beautiful open source icons. Each icon is designed on a 24x24 grid with an emphasis on simplicity, consistency, and readability.
Mega Thread 🧵👇
1️⃣ Ionicons: The premium icon pack for Ionic Framework
Ionicons is an open-sourced and MIT-licensed icon pack.
2️⃣ 700+ CSS Icons, Customizable, Retina Ready & API
Pure CSS icons library, Customizable & Retina-Ready built 100% in pure CSS, SVG, SVG Sprite, styled components, Figma, and Adobe XD. Easy integration: Embed, NPM & API
3️⃣ SVG Repo - Free SVG Vectors and Icons
Free Vectors and Icons in SVG format. Download free mono or multi-color vectors for commercial use. Search in 300.000+ Free SVG Vectors and Icons.
https://t.co/2Ht2sI23TE
4️⃣ Feather – Simply beautiful open source icons
Feather is a collection of simply beautiful open source icons. Each icon is designed on a 24x24 grid with an emphasis on simplicity, consistency, and readability.
Use these 5 websites to learn web development for free in 2023 (take a look):
1. Codedamn
All-in-one platform for learning web development:
• Hands-on practice and projects bundled
• Video content along with interactive labs, quizzes, and articles
• Every course comes with a certificate
• code brushes AI in playgrounds
🔗 https://t.co/mXmUtJZnOU
2. Codepip
Codepip is the platform for your favorite web development games. Gain an edge in your following interview or project.
🔗 https://t.co/MjHNsLvkiL
3. RapidAPI Comics
Learn API development with RapidAPI Comics (sketch notes and fun analogies).
🔗 https://t.co/KONorFomgu
4. JS Visualizer 9000
Learn JavaScript by visualizing your code.
🔗 https://t.co/9NId4GjGAq
1. Codedamn
All-in-one platform for learning web development:
• Hands-on practice and projects bundled
• Video content along with interactive labs, quizzes, and articles
• Every course comes with a certificate
• code brushes AI in playgrounds
🔗 https://t.co/mXmUtJZnOU
2. Codepip
Codepip is the platform for your favorite web development games. Gain an edge in your following interview or project.
🔗 https://t.co/MjHNsLvkiL
3. RapidAPI Comics
Learn API development with RapidAPI Comics (sketch notes and fun analogies).
🔗 https://t.co/KONorFomgu
4. JS Visualizer 9000
Learn JavaScript by visualizing your code.
🔗 https://t.co/9NId4GjGAq
You May Also Like
🌿𝑻𝒉𝒆 𝒔𝒕𝒐𝒓𝒚 𝒐𝒇 𝒂 𝑺𝒕𝒂𝒓 : 𝑫𝒉𝒓𝒖𝒗𝒂 & 𝑽𝒊𝒔𝒉𝒏𝒖
Once upon a time there was a Raja named Uttānapāda born of Svayambhuva Manu,1st man on earth.He had 2 beautiful wives - Suniti & Suruchi & two sons were born of them Dhruva & Uttama respectively.
#talesofkrishna https://t.co/E85MTPkF9W
Now Suniti was the daughter of a tribal chief while Suruchi was the daughter of a rich king. Hence Suruchi was always favored the most by Raja while Suniti was ignored. But while Suniti was gentle & kind hearted by nature Suruchi was venomous inside.
#KrishnaLeela
The story is of a time when ideally the eldest son of the king becomes the heir to the throne. Hence the sinhasan of the Raja belonged to Dhruva.This is why Suruchi who was the 2nd wife nourished poison in her heart for Dhruva as she knew her son will never get the throne.
One day when Dhruva was just 5 years old he went on to sit on his father's lap. Suruchi, the jealous queen, got enraged and shoved him away from Raja as she never wanted Raja to shower Dhruva with his fatherly affection.
Dhruva protested questioning his step mother "why can't i sit on my own father's lap?" A furious Suruchi berated him saying "only God can allow him that privilege. Go ask him"
Once upon a time there was a Raja named Uttānapāda born of Svayambhuva Manu,1st man on earth.He had 2 beautiful wives - Suniti & Suruchi & two sons were born of them Dhruva & Uttama respectively.
#talesofkrishna https://t.co/E85MTPkF9W
Prabhu says i reside in the heart of my bhakt.
— Right Singh (@rightwingchora) December 21, 2020
Guess the event. pic.twitter.com/yFUmbfe5KL
Now Suniti was the daughter of a tribal chief while Suruchi was the daughter of a rich king. Hence Suruchi was always favored the most by Raja while Suniti was ignored. But while Suniti was gentle & kind hearted by nature Suruchi was venomous inside.
#KrishnaLeela
The story is of a time when ideally the eldest son of the king becomes the heir to the throne. Hence the sinhasan of the Raja belonged to Dhruva.This is why Suruchi who was the 2nd wife nourished poison in her heart for Dhruva as she knew her son will never get the throne.
One day when Dhruva was just 5 years old he went on to sit on his father's lap. Suruchi, the jealous queen, got enraged and shoved him away from Raja as she never wanted Raja to shower Dhruva with his fatherly affection.
Dhruva protested questioning his step mother "why can't i sit on my own father's lap?" A furious Suruchi berated him saying "only God can allow him that privilege. Go ask him"