,

Crash Course on React Hooks: Exploring useMemo, useCallback, and More

Posted by



React Hooks Crash Course (useMemo, useCallback and more)

React Hooks has revolutionized the way we write React components. With the introduction of functional components, we no longer need to rely on class components for state management. React Hooks provide a simple and elegant solution to managing state and side effects without the need for classes.

In this crash course, we will dive into some of the most commonly used React Hooks – useMemo, useCallback, and more – and explore how they can enhance the performance and efficiency of our React applications.

1. useMemo: useMemo is a hook that allows us to optimize the performance of our components by memoizing a value. useMemo takes a function and an array of dependencies as arguments, and returns a memoized value. The memoized value will only be recomputed when the dependencies change.

Example:
“`
const memoizedValue = useMemo(() => computeExpensiveValue(a, b), [a, b]);
“`

2. useCallback: useCallback is another hook that helps optimize the performance of our components. It is used to memoize a function, similar to useMemo. The difference is that useMemo memoizes a value, whereas useCallback memoizes a function. This is useful when we want to pass down a function to child components and prevent unnecessary re-renders.

Example:
“`
const memoizedCallback = useCallback(() => {
doSomething(a, b);
}, [a, b]);
“`

3. useState: useState is the most basic and often the first hook we encounter. It allows us to add state to our functional components. useState returns an array with two elements – the current state value and a function to update the state. We can destructure this array into separate variables for easier access.

Example:
“`
const [count, setCount] = useState(0);
“`

4. useEffect: useEffect is a powerful hook that allows us to perform side effects in our components. It is similar to the lifecycle methods in class components, such as componentDidMount and componentDidUpdate. We can use useEffect to fetch data, subscribe to events, or perform any other side effect. useEffect takes a function and an array of dependencies as arguments. The function will be called after each render, and the dependencies determine when the effect should be re-run.

Example:
“`
useEffect(() => {
// perform side effect
return () => {
// clean up side effect
};
}, [dependencies]);
“`

These are just a few of the many React Hooks available. React Hooks provide a clean and concise way to handle state and side effects in our React components. By using useMemo and useCallback, we can optimize our components’ performance, while useState and useEffect allow us to manage state and perform side effects easily.

In conclusion, React Hooks have transformed the way we write React components, making them more efficient and easier to understand. Whether you are a React beginner or an experienced developer, learning and utilizing React Hooks can greatly benefit your development workflow. So go ahead and experiment with these hooks in your next React project and see the difference they can make!

0 0 votes
Article Rating
20 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ashutosh Tiwari
7 months ago

hey Ed thanks for this amazing video, can you please use all of these hooks in a project and show us a glimpse of why it is used and where to use the specific hook. Except (useState, useRef, useEffect, useEffectLayout)

Yajirushi K
7 months ago

About the useTransition I din't knew it works like that, what I used every time for real time filtering with fetch was a fetch api with abort signal, this makes things easier 👌

Baxtiyor Sulaymonov | AT
7 months ago

You made me think about svelte )

sreeharsha d
7 months ago

hi
when ever i use hooks in my own custom library i getting the error saying cannot read the null with the hook i used

Aryan Kalra
7 months ago

When you are trying to understand useMemo and useCallback by yourself 30:25

Kocikoro
7 months ago

Great video as always! Thank you Ed!

Memaan  Collection
7 months ago

@developedbyed Can you make a video on React Error Handling? like there's error boundry which react provide idk why these component are class component and theirs also react-error-boundry npm which actually provide you with hooks….. please NATURE DO SO ACTION MAKE ed SEE MY COMMENT & MAKE VIDEO ON IT

王俊
7 months ago

import React, { useEffect, useState } from 'react'

const Test = () => {

const [count, setCount] = useState(0);

useEffect(() => {
const vv = setInterval(() => {
setCount(prev => prev + 1)
}, 1000)
}, []);

return (
<div onClick={() => { setCount(prev => prev + 1) }}>{count}</div>
)
}

export default Test

i have no problem“ why?

Joshua Modiba
7 months ago

🤦oh man i liked the components by what's up with these hooks limitations and easy to make bugs

Nova No Skillz
7 months ago

your useMemo example was the first time ive seen someone make sense of usememo. even my senior dev has had a hard time explaining it. all he ever says is use useMemo to memoize your function hahaha wtf!!! thanks for this video.

iedi3
7 months ago

It’s funny the last part, Svelte. I’m learning React, due to popularity, while already using Vue. But I would’ve wanted to try Svelte… but there’s only an amount of time you can spend learning new things, while also having commercial skills, I’m also learning node/express, again, I wish I would’ve learned Nest…😂

Seven77tw
7 months ago

i love you

hiberniankingy
7 months ago

A thumbs up for questioning why we haven't moved over to Svelte lol

Dario Macedo
7 months ago

hey Ed some arduino or ESP32 projects with react interaction would be amazing!

Master Miscellaneous
7 months ago

Thanks for this video and for walking through all these. I especially like the idea of using Zustand. Will you ever make a video tutorial on this state management library?

Pedro Gomes
7 months ago

this was great thank you !

Atiurrahaman
7 months ago

I've gone through many tutorials but this one cleared my mind with all the unused material and filled it with required data only. Thanks for this precious tutorial my gorgeous friend.🎉❤

Thuan Pham Minh
7 months ago

how to re-render without setState on function => never can do that

ylliee
7 months ago

Timestamps
0:43 useState
4:26 useEffect
12:46 useContext
15:48 useReducer
20:09 useRef
21:35 useMemo
23:16 useCallback & useMemo
28:02 useTransition

sunbertyv
7 months ago

I have this problem, i downloaded node, and installed react with all packages, but then when i do localhost3000 i don't see my page, what can i do