Pratham Profile picture
26 May, 25 tweets, 6 min read
Introduction to React Hooks ↪️

Hooks let you manage state and other React feature without writing the class component. It was first introduced in React 16.8

Let us try to understand it in more detail 🧵👇🏻
Starting with the discussion on some key point about React hooks 👇🏻
📌 What exactly hooks are?

- We can consider hooks as a JavaScript functions which takes some parameter and return something accordingly. But they servers some complex functionality like managining state and other React features
📌 Hooks are backwards-compatible

Backwards compatiblity is the characterstic of a technology that allows for for interoperability with an older legacy system. Hence hooks don’t contain any breaking changes.
📌 Did Hooks make any changes?

React concepts are same irrelevant of class component or functional component. Hooks can make the code shorter and they provide a quick and efficient way to combine state, props. lifecycle etc...
📌 Some points to consider while working with hooks

- Don’t call Hooks inside loops, conditions, or nested functions
- Call hooks from React function components
📌 Let's see what a hook looks like

Introduction to widely used React hook - useState 🔽
While working with any React hooks, first thing we need to do is to import it.
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 👇🏻
You can see the syntax of hooks are quite similar than typical function in React. We just need to pass the paramaters according to the acceptance of a particular hook
Moving forward, lets destruct the return value

const [currentState, setCurrentState] = useState(0);

- currentState is the value of our current state
- setCurrentState is the function using which we can change current state value
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 🔽
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
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 🔽
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
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:
Similarly we can write functionalty for "MINUS" button
Though there is problem in updating the state using this method

setCurrentState(currentState + 1);

If you call setCurrentState function two times like this, it will still increase our counter by 1 (see attached image)
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
This is how useState hook works.

We can also create our custom hooks. Building our own Hooks lets us extract component logic into reusable functions.

Lets try to mimic useState hook 👇🏻
Here inside the useStateDuplicate hook, I just wrote the useState by doing so now our useStateDuplicate hook provide the functionality of useState hook
Be sure to add "use" keywords as prefix in the name of your custom hook. Without the use keyword, React wouldn't able to perform automatically error checking for you.
Hooks are broadly divided into two catrgories.

1. Basic hooks
- useState
- useEffect
- useContext

2. Additional hooks
- useReducer
- useCallback
- useMemo
- useRef
- useImperativeHandle
- useLayoutEffect
- useDebugValue
I have already covered the detailed explanation of useEffect hook in one of my threads

I think that's the basic intro of hooks. If you like it, share it with your connections, it means a lot to me ❤️

I'll catch you in my next thread on useReducder hook, until then 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

27 May
Introduction to CSS transitions ⏲️

CSS transition add some smoothness in animation. By using it, you can change styling over a given duration instead of instantly

Let's discuss this in a little more detail 🧵👇🏻 Image
CSS transition and CSS animation are two closely related modules of CSS but they are different

For example, CSS animation runs automatically but the transition need to be triggered like hover
Transition is a like a two point operation. For example, suppose you're changing the height to an element when user hover it from 100px to 200px

In this case point A is 100px and point B is 200px
Read 16 tweets
27 May
Announcement 🚨

You all have been waiting for this for so long. I just published my eBook containing all my CSS cheat sheets.

Sounds good? Purchase it right now for $1 only 💸

🔗 gum.co/css-cheat Image
People are suggesting me that $ 1 is too low 😅

I was going to keep its price $0. Thank god i didn't do that. Still, I love helping others as much as I can. If you can't spend $1 due to some reasons, DM me and I'll send you a free copy ❤️
Unbelievable for me 🚀 Image
Read 5 tweets
27 May
These 5 great tools can save you from writing CSS code if you're a front-end developer

🧵👇🏻
1️⃣ CSS Accordion Slider Generator

- Create fully responsive, css only accordion sliders

🔗 accordionslider.com Image
2️⃣ Animate.css

- Animate is a CSS library for basic CSS animation. Its a browser friendly with a lot of pre built animations

🔗 animate.style
Read 6 tweets
26 May
Cool things about HTML in this single thread 🧵👇🏻
2. Types of list marks Image
Read 11 tweets
26 May
5 great code snippet websites for every web developer and designer

🧵👇🏻
1️⃣ 30 Seconds of Code

- Short code snippets for all your development needs

🔗 30secondsofcode.org
2️⃣ Free Frontend

- Free hand-picked HTML, CSS and JavaScript (jQuery, React, Vue) code examples, tutorials and articles

🔗 freefrontend.com
Read 6 tweets
25 May
5 GitHub repositories for you if you're learning HTML, CSS and JavaScript

🧵👇🏻
1️⃣ JavaScript questions

- A long list of (advanced) JavaScript questions, and their explanations

🔗 github.com/lydiahallie/ja…
2️⃣ Microsoft web dev course

- This is a super cool GitHub repo by Microsoft for learning web development for beginners

🔗 github.com/microsoft/Web-…
Read 6 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!

:(