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 amazing GitHub repositories for every self taught developer
A Thread π§΅
1οΈβ£ Computer Science
π Path to a free self-taught education in Computer Science!
π https://t.co/67jB5zqIes
2οΈβ£ Free Certifications
Curated list of free courses & certifications
π https://t.co/4XtlAVlovs
3οΈβ£ Free Programming Books
π Freely available programming books
π https://t.co/eOWLCtwtIV
4οΈβ£ JavaScript Question
A long list of (advanced) JavaScript questions, and their explanations β¨
π https://t.co/UvEl1Esvh3
A Thread π§΅
1οΈβ£ Computer Science
π Path to a free self-taught education in Computer Science!
π https://t.co/67jB5zqIes
2οΈβ£ Free Certifications
Curated list of free courses & certifications
π https://t.co/4XtlAVlovs
3οΈβ£ Free Programming Books
π Freely available programming books
π https://t.co/eOWLCtwtIV
4οΈβ£ JavaScript Question
A long list of (advanced) JavaScript questions, and their explanations β¨
π https://t.co/UvEl1Esvh3
I've brought you some amazing GitHub repositories of web development tips and tricks π
THREADπ§΅π½
1οΈβ£ JS Tips
- A huge list of 73 great tips and tricks of JavaScript
π https://t.co/r0J9vW8WrH
2οΈβ£ CSS Protips
- A collection of tips to help take your CSS skills pro. Definitely check it out
π https://t.co/5haB2xTWuz
3οΈβ£ JS Tips and Tricks
- Some advanced tips and tricks of JavaScript that can help you to take your skills onto next level
π https://t.co/NvfoANwweV
4οΈβ£ Git Tips
- Git is an essential tool for very programmer. in this repo you'll find the most commonly used git tips and tricks
π https://t.co/34qvOhYCZE
THREADπ§΅π½
1οΈβ£ JS Tips
- A huge list of 73 great tips and tricks of JavaScript
π https://t.co/r0J9vW8WrH
2οΈβ£ CSS Protips
- A collection of tips to help take your CSS skills pro. Definitely check it out
π https://t.co/5haB2xTWuz
3οΈβ£ JS Tips and Tricks
- Some advanced tips and tricks of JavaScript that can help you to take your skills onto next level
π https://t.co/NvfoANwweV
4οΈβ£ Git Tips
- Git is an essential tool for very programmer. in this repo you'll find the most commonly used git tips and tricks
π https://t.co/34qvOhYCZE
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
APIs in general are so powerful.
Best 5 public APIs you can use to build your next project:
1. Number Verification API
A RESTful JSON API for national and international phone number validation.
π https://t.co/fzBmCMFdIj
2. OpenAI API
ChatGPT is an outstanding tool. Build your own API applications with OpenAI API.
π https://t.co/TVnTciMpML
3. Currency Data API
Currency Data API provides a simple REST API with real-time and historical exchange rates for 168 world currencies
π https://t.co/TRj35IUUec
4. Weather API
Real-Time & historical world weather data API.
Retrieve instant, accurate weather information for
any location in the world in lightweight JSON format.
π https://t.co/DCY8kXqVIK
Best 5 public APIs you can use to build your next project:
1. Number Verification API
A RESTful JSON API for national and international phone number validation.
π https://t.co/fzBmCMFdIj
2. OpenAI API
ChatGPT is an outstanding tool. Build your own API applications with OpenAI API.
π https://t.co/TVnTciMpML
3. Currency Data API
Currency Data API provides a simple REST API with real-time and historical exchange rates for 168 world currencies
π https://t.co/TRj35IUUec
4. Weather API
Real-Time & historical world weather data API.
Retrieve instant, accurate weather information for
any location in the world in lightweight JSON format.
π https://t.co/DCY8kXqVIK