You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: react/more_react_concepts/refs_and_memoization.md
+12-3Lines changed: 12 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,8 +6,9 @@ How can we do DOM manipulations that we usually do in vanilla JavaScript? Is it
6
6
7
7
This section contains a general overview of topics that you will learn in this lesson.
8
8
9
-
- Explore `useRef` hook and its use cases.
10
-
- Explain memoization and how `useCallback` and `useMemo` can be used.
9
+
-`useRef` hook and its use cases.
10
+
- Memoization and how `useCallback` and `useMemo` can be used.
11
+
- Automatic memoization with React Compiler.
11
12
12
13
### The useRef hook
13
14
@@ -292,9 +293,15 @@ Yay, there's only one arrow function, and it's simpler to read. There's nothing
292
293
293
294
Which one should we use, then? Use `useMemo` for *any* value types, and use `useCallback` specifically for functions. At the end of the day, they both do similar things with a tiny difference, so use whatever you prefer.
294
295
296
+
### React Compiler
297
+
298
+
We now also have React Compiler, which is a relatively new build-time tool that can automatically optimize our React app. It will analyze our code and memoize appropriate components and hooks, primarily focusing on improving update performance (re-rendering existing components). This means that in most cases, we *don't have to* manually memoize components or hooks.
299
+
300
+
So if React Compiler memoizes our hooks and components automatically, do we need to know about `React.memo`, or hooks like `useMemo` and `useCallback`? Short answer - yes. Understanding manual memoization helps us understand what React Compiler does under the hood, not to mention that you will almost certainly run into manual memoization techniques in existing codebases. Lastly, there may be cases where we need more control over what gets memoized and how compared to what React Compiler will automatically do, such as making sure a `useEffect` dependency is memoized to ensure it doesn't change unnecessarily and fire the effect.
301
+
295
302
### Conclusion
296
303
297
-
Phew, this was a long lesson. Refs and memoization are difficult concepts to grasp, but we're sure you'll understand them with practice. Refs particularly are really useful for some use-cases, as for memoization, only reach out to it when you absolutely need it. These topics also make for great interview questions, so make sure you know the difference between `useMemo` and `useCallback`!
304
+
Phew, this was a long lesson. Refs and memoization are difficult concepts to grasp, but we're sure you'll understand them with practice. Refs particularly are really useful for some usecases. As for automatic memoization, it can help optimize our app without needing to write any additional code. Manual memoization may also be necessary in certain cases, but you should only reach out for it when you absolutely need it. These topics also make for great interview questions, so make sure you know the difference between `useMemo` and `useCallback`!
298
305
299
306
### Assignment
300
307
@@ -304,6 +311,7 @@ Phew, this was a long lesson. Refs and memoization are difficult concepts to gra
304
311
1. We've only learned about a basic implementation of the `useRef` hook. For more examples about its usage and why we should be wary of using the hook (more on the links they provided in the guide), check out the interactive guide of the React documentation for [useRef hook](https://react.dev/reference/react/useRef) .
305
312
1. The article [useRef instead of querySelector in React](https://meje.dev/blog/useref-not-queryselector) by Caleb Olojo briefly tells some unexpected behaviors when trying to manipulate the DOM directly with DOM manipulation methods and why we should prefer `useRef` over other DOM manipulation methods like `querySelector`. Check it out!
306
313
1. As we have learned, the `useRef` hook has other uses other than what we've primarily covered which is DOM Manipulation. Get to know more about its use-cases in this great article by Dan Abramov [Making setInterval Declarative with React Hooks](https://overreacted.io/making-setinterval-declarative-with-react-hooks/).
314
+
1. Go through [React Compiler docs](https://react.dev/learn/react-compiler) to learn a bit more about it, and to find out how to install and configure it in your projects.
307
315
308
316
</div>
309
317
@@ -315,3 +323,4 @@ The following questions are an opportunity to reflect on key topics in this less
315
323
-[What is the difference between useMemo and useCallback?](#usememo-or-usecallback)
316
324
-[How do useMemo and useCallback help optimize the performance of React components?](#optimization-description)
317
325
-[When should you memoize a value?](https://kentcdodds.com/blog/usememo-and-usecallback)
326
+
-[What is React Compiler and how does it optimize React applications?](https://react.dev/learn/react-compiler/introduction)
0 commit comments