Pratham Profile picture
May 18, 2021 15 tweets 5 min read Read on X
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

codesandbox.io/s/covid-tracke…

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

• • •

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

Apr 7
APIs are the backbone of development.

Let me show you how to build your first basic REST API in four simple steps.
Step 1: Install Node.js

I'm assuming you have Node.js installed on your machine. If you haven't installed it, click on the following link and install it simply.

nodejs.org/en
Step 2: Initialize the project

Let's start; create an empty directory and initialize your project by running the following command. Image
Read 17 tweets
Apr 4
5 GitHub repositories will make your life 90% easier:
1. StackQL

StackQL allows you to query multiple cloud and SaaS providers (Google, AWS, Azure, Okta, GitHub, etc.) using SQL.

🔗 github.com/stackql/stackql
Image
2. 50 Projects 50 Days

50+ mini web projects using HTML, CSS & JavaScript.

🔗 github.com/bradtraversy/5…
Image
Read 6 tweets
Feb 2
Convert your Python application into a website in 2 minutes.

Let me show you how:
There's no easy way than this to build a data, AI-based web applications.

I came across Taipy.

It is an open-source Python library for building production-ready front-end & back-end in no time.
Let's see in five simple steps how to build a movie recommendation system using Taipy.
Read 11 tweets
Jan 23
6 free Books to learn web development:
1. The Magic of CSS

The content in this eBook is for all-level developers. Learn about layout, box-model, positioning etc. with sample code snippets.

adamschwartz.co/magic-of-css/
Image
2. DOM Enlightenment

"DOM Enlightenment" is a free online book that explores DOM in-depth, providing a clear and detailed understanding of how it works.

domenlightenment.com
Image
Read 7 tweets
Jan 8
5 GitHub repositories that will make your life 90% easier if you write code:
1. Refine

A React Framework for building internal tools, admin panels, dashboards & B2B apps with unmatched flexibility.

Speed up your development timeline.

🔗 github.com/refinedev/refi…
Image
2. Keystone

The most powerful headless CMS for Node.js — built with GraphQL and React

🔗 github.com/keystonejs/key…
Image
Read 6 tweets
Jan 2
6 GitHub repositories will make you feel like an expert developer:
1. BCMS

BCMS is an open-source Headless CMS.

Design your content structure without leaving the browser.

🔗 github.com/bcms/cms
2. llmware

With the help of llmware, you can build your own LLM-based application with little coding and data.

It easily links to all popular models like Hugging Face and OpenAI and automatically captures, tracks, and evaluates AI output results.

🔗 github.com/llmware-ai/llm…
Image
Read 7 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

Don't want to be a Premium member but still want to support us?

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

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us!

:(