React Hooks Tips Only the Pros Know

Rate this content
Bookmark

The addition of the hooks API to React was quite a major change. Before hooks most components had to be class based. Now, with hooks, these are often much simpler functional components. Hooks can be really simple to use. Almost deceptively simple. Because there are still plenty of ways you can mess up with hooks. And it often turns out there are many ways where you can improve your components a better understanding of how each React hook can be used.

You will learn all about the pros and cons of the various hooks. You will learn when to use useState() versus useReducer(). We will look at using useContext() efficiently. You will see when to use useLayoutEffect() and when useEffect() is better.

This workshop has been presented at React Summit Remote Edition 2021, check out the latest edition of this React Conference.

FAQ

The workshop focuses on advanced React Hooks techniques, aiming to teach participants how to use hooks beyond the basics, including custom hooks, state management, and performance optimization in React applications.

The workshop is led by Boris De Beier, known as 'The Problem Solver'. He is a Microsoft MVP Developer with extensive experience in front-end development, particularly using React.

Resources such as the code repository and slides are shared through Discord and Zoom chat during the workshop. Participants can also access these resources via email links provided before the workshop.

The workshop is divided into two parts: the first part covers simple examples focusing on individual hooks, while the second part involves creating a more complex application, such as a forms library similar to Formik, using various React features and hooks.

Participants are expected to use their own code editor, like Visual Studio Code, and have Node and NPM installed. Access to the provided GitHub repository is also necessary to follow along with the code examples.

Maurice de Beijer
Maurice de Beijer
177 min
09 Jun, 2021

Comments

Sign in or register to post your comment.
Video Summary and Transcription
This workshop on React Hooks Tips explores hooks beyond the basics, covering common mistakes, advanced hooks, creating custom hooks, and the rules of hooks. It demonstrates the use of useState, useEffect, and useContext hooks, as well as creating custom hooks for managing state and performing side effects. The workshop also covers the use of useRef, useReducer, and useLayoutEffect hooks, and provides insights into how React hooks work under the hood. It concludes with the implementation of a provider component and the use of context and reducers to manage complex state in functional components.

1. React Hooks Tips: Beyond the Basics

Short description:

Welcome to the workshop on React Hooks Tips Only the Pros Know. This is an advanced workshop where we'll explore hooks beyond the basics. We'll cover common mistakes, advanced hooks, creating custom hooks, and the rules of hooks. In the second part, we'll create a simplified clone of a UI or forms library using hooks and context. Make sure to have Node, NPM, and the GitHub repository ready. Clone the repository, switch to the start branch, and run NPM install. We'll be using vita or feet to develop the application in a lightweight and efficient way.

So, welcome everyone to this workshop on React Hooks Tips Only the Pros Know. This is an interactive workshop, so you're expected to fire up Visual Studio Code or whatever editor you use and write code. I'll share the repository. In fact, I already shared the repository in the chat window over on Discord and in Zoom as well. But, Discord is the main chat window, so make sure to keep that open.

So, who am I? My name is Boris De Beier, also known as The Problem Solver. I'm a Microsoft MVP Developer, Instructor, Team Lead, do all sorts of things like that. A lot of front-end developments with React. That's kind of the main thing I do. My Twitter handle, at borisdb. If you want to follow me, send me messages later, or my email address, in case you want to do any of that. So, among the many things I do, I also publish a React newsletter that's sent out every Wednesday. So, yesterday, there was another one. Feel free to subscribe. Just click on the image, or the QR code, or scan the QR code with your phone. If you register, you'll get one email a week with six React articles and one video. Don't like it? You can just unsubscribe. I'm not gonna use your email address for anything else. I'm not gonna sell it to god-knows-who. There is no point in doing that with all the data leaks. I can't really compete with those any, if I would want to, and I don't.

So what are the goals of this workshop? Well, we're gonna take a look at hooks. We're gonna skip on the basics. This is an advanced workshop. So I'm expecting everyone to be familiar with React, familiar with the basics of hooks. And we're gonna take it beyond that. We're gonna take a look at how you can go wrong, because even with the simple hooks, they are pretty simple, but you can still use them in the wrong way and get a bug or some unintended result or maybe a performance degradation or something like that. And then we're going to go beyond the basics hooks. How can you use the more advanced hooks? When would you want to? And which are there, of course. We're not going to look at all of them, there are a couple of pretty edge case, ones like use debug value. We're not going to use that, but we'll take a look at most of them. We'll create some custom hooks. We'll take a look at the rules of hooks from why you need to follow them, what can happen if you don't, and how they're actually built up. Part of that is we'll take a look behind the scenes at how the ESLint rules and how React actually work under the hood with hooks. And then the second part of the workshop, we're going to do a more complex example. The first part is pretty simple examples where we're focused on one hook at a time. The second part, we're going to create a bit of a UI library or a forms library, maybe you're familiar with formic, which is a really nice library if you want to do forms over data in React. Well, we're going to create a simple, pretty, very much simplified clone of that. See how hooks and context and a couple of different React features will work together to create a very elegant solution to do something like that. I'm going to provide you with all of the code you need in the Git repo, so if you want, you can copy everything. But I would highly recommend to type as much of it as you can. Like there are some bigger snippets where I'm going to copy it as well, because there is no point in watching me type 25 lines of code with lots of typos. But for the most part, I would recommend you actually type it in because you'll make mistakes. I make mistakes. Everyone does. And you'll make those in real life as well when it's not some workshop. And if you've made them now, you've seen what goes wrong. You can get help. Then you'll recognize them when they happen in some other scenario. So it will help making errors actually is a great way to figure things out. Prerequisites for the code you'll need Node and NPM and the GitHub repository. With Node and NPM, I'm pretty much expecting everyone to be good there. Given that this is an advanced course, and I'm expecting everyone to be familiar with React. I've got Node 12.18, I've actually got a newer version of NPM. What is it? I've got NPM 7.11 here. I've actually got Node 14.15, unlike what it says in my slide. I guess that's a bit old, a bit dated, but a relatively new version of Node should work and a relatively new version of NPM, it doesn't need to be exactly this version. The other thing you'll need is the GitHub repository. Now I've pasted links to it, both in Discord and in the Zoom chat window. So you can get there. You should also have received an email with links to it. So plenty of ways to get at it. If you've got the copy of the slides, which I highly recommend you keep handy because there are lots of links in there. You can click on the QR code here or on the image and it will take you right to the repository, except it does so on my other screen. So here it is. Basically, all the codes we're going to write this in here, there are a bunch of branches in here. When you clone this, we just leave it on the main branch to do the npm install. And after that switch to the zero zero start branch. That's the one we're going to start with, which doesn't have all the code we're going to write. The main branch is complete. All the other branches here are kind of at the end of a step. So zero one custom hook is after we've done a custom hook, zero two layout effects is the same thing, et cetera. So you can see we've got 14 different steps to go through. Actually, is that all? I think so. All branches here. Yeah, 14 of them. So make sure to clone that and run NPM install on that. And that shouldn't take that long, but you'll get a bunch of packages. This is not done with create react app. So it's actually a bit smaller than that. A bit of an experiment, but I'm using vita too, or feet. I should actually say that's how it's pronounced to build and run the application in development mode. Maybe you've heard of it, maybe you haven't. It's a lightweight way of hosting applications or sorry, developing applications where the development server starts up really fast. It uses ECMAScript modules to serve everything up. So code will be compiled as needed, but it's not like everything has to be bundled with every change. So completion steps are really small and it's only compiled as needed, which actually works really well. I haven't used this on large projects yet where it actually shines even more because then the startup time would create React that becomes kind of slow. But the smaller projects I've used this on, I was really happy with it. So I used it for this workshop and it works very well.

Read also

2. Introduction to React Hooks

Short description:

Welcome to the workshop on React Hooks Tips Only the Pros Know. This is an advanced workshop where we'll explore hooks beyond the basics. We'll cover common mistakes, advanced hooks, creating custom hooks, and the rules of hooks. In the second part, we'll create a simplified clone of a UI or forms library using hooks and context. Make sure to have Node, NPM, and the GitHub repository ready. Clone the repository, switch to the start branch, and run NPM install. We'll be using vita or feet to develop the application in a lightweight and efficient way.

And I can recommend it for other stuff as well. Another thing you'll notice with the code, all the code is in TypeScript. Now, not everyone is a TypeScript fan, I am. I'm not going to apologize for that. I very much like TypeScript. I very much think it's well, a huge benefit when you're doing serious applications. With a small like demo application from proof of concept, I'll probably use JavaScript as well just to do things quickly. But any serious work I do in TypeScript. Now this workshop isn't a serious application, but it is meant to be realistic even though it's pretty tiny. So I decided to use TypeScript. Now, if you're not used to TypeScript, there is a file. Let me open up Visual Studio Code first, and show you where is it? TS config down here. This controls how TypeScript works. There is this setting here, strict is true. That's means that TypeScript will do lots of checks. If you don't really want or have the experience with TypeScript, you can set this to false and then it will start bothering you a lot less. There's also the any keyword. You can type things as any and get rid of a lot of compilation warnings that way as well. But I would highly recommend you to stick with TypeScript and use it even in serious projects because it works really well. Another couple of links to the repository and these slides. That's not the point of this slide though. The point is, you'll see lots of images like this where I have code snippets, use person, function in this case. And if you hover over it with the mouse, you'll notice that that's actually a link which opens up on my other monitor again. So the point of this link and the code that shows up, it's a diff of what we're actually doing in this step. So in this step, we can see that I added to use ref here and I changed something, et cetera, exactly what I removed, like remove some code here, stuff like that. Useful if you want to do something and you're not quite sure what the changes are. There are some subtle highlights here where you can see where code was changed, which will help as well, but you can open up the code. And of course, if it's a bigger change, you can always go in here and just copy stuff from here, which I'll occasionally do. Also, you'll see this gentleman come along. Jean-Luc Picard, the captain of the Enterprise. And basically any time you see him come up, it's a cue that it's your turn to do something. So basically, I'm gonna show you what we're gonna do. I'm gonna do it. And then after that, it's your turn to do the same. Of course, I'll explain how and everything, how everything's supposed to work, et cetera. But during those interactive parts where you're actually coding, I'm gonna open up to Zoom breakout rooms. I'm gonna split the, well, we're currently at 23 people. So I'm gonna split that up into something like seven, maybe eight rooms, depending on the actual number then. So there will be something like three to four people in a given room. Unfortunately, sometimes in Zoom, people are dropped from a room. In that case, I'll add you back. But I typically don't know exactly which room you were originally in because they're randomly created. So sorry if you hop from one room to another as a result of that. Nothing much I can do about that. So that's the intro part.

QnA

Watch more workshops on topic

React Performance Debugging Masterclass
React Summit 2023React Summit 2023
170 min
React Performance Debugging Masterclass
Top Content
Featured Workshop
Ivan Akulov
Ivan Akulov
Ivan’s first attempts at performance debugging were chaotic. He would see a slow interaction, try a random optimization, see that it didn't help, and keep trying other optimizations until he found the right one (or gave up).
Back then, Ivan didn’t know how to use performance devtools well. He would do a recording in Chrome DevTools or React Profiler, poke around it, try clicking random things, and then close it in frustration a few minutes later. Now, Ivan knows exactly where and what to look for. And in this workshop, Ivan will teach you that too.
Here’s how this is going to work. We’ll take a slow app → debug it (using tools like Chrome DevTools, React Profiler, and why-did-you-render) → pinpoint the bottleneck → and then repeat, several times more. We won’t talk about the solutions (in 90% of the cases, it’s just the ol’ regular useMemo() or memo()). But we’ll talk about everything that comes before – and learn how to analyze any React performance problem, step by step.
(Note: This workshop is best suited for engineers who are already familiar with how useMemo() and memo() work – but want to get better at using the performance tools around React. Also, we’ll be covering interaction performance, not load speed, so you won’t hear a word about Lighthouse 🤐)
Next.js for React.js Developers
React Day Berlin 2023React Day Berlin 2023
157 min
Next.js for React.js Developers
Top Content
Featured WorkshopFree
Adrian Hajdin
Adrian Hajdin
In this advanced Next.js workshop, we will delve into key concepts and techniques that empower React.js developers to harness the full potential of Next.js. We will explore advanced topics and hands-on practices, equipping you with the skills needed to build high-performance web applications and make informed architectural decisions.
By the end of this workshop, you will be able to:1. Understand the benefits of React Server Components and their role in building interactive, server-rendered React applications.2. Differentiate between Edge and Node.js runtime in Next.js and know when to use each based on your project's requirements.3. Explore advanced Server-Side Rendering (SSR) techniques, including streaming, parallel vs. sequential fetching, and data synchronization.4. Implement caching strategies for enhanced performance and reduced server load in Next.js applications.5. Utilize React Actions to handle complex server mutation.6. Optimize your Next.js applications for SEO, social sharing, and overall performance to improve discoverability and user engagement.
Concurrent Rendering Adventures in React 18
React Advanced 2021React Advanced 2021
132 min
Concurrent Rendering Adventures in React 18
Top Content
Featured WorkshopFree
Maurice de Beijer
Maurice de Beijer
With the release of React 18 we finally get the long awaited concurrent rendering. But how is that going to affect your application? What are the benefits of concurrent rendering in React? What do you need to do to switch to concurrent rendering when you upgrade to React 18? And what if you don’t want or can’t use concurrent rendering yet?

There are some behavior changes you need to be aware of! In this workshop we will cover all of those subjects and more.

Join me with your laptop in this interactive workshop. You will see how easy it is to switch to concurrent rendering in your React application. You will learn all about concurrent rendering, SuspenseList, the startTransition API and more.
Introducing FlashList: Let's build a performant React Native list all together
React Advanced 2022React Advanced 2022
81 min
Introducing FlashList: Let's build a performant React Native list all together
Top Content
Featured Workshop
David Cortés Fulla
Marek Fořt
Talha Naqvi
3 authors
In this workshop you’ll learn why we created FlashList at Shopify and how you can use it in your code today. We will show you how to take a list that is not performant in FlatList and make it performant using FlashList with minimum effort. We will use tools like Flipper, our own benchmarking code, and teach you how the FlashList API can cover more complex use cases and still keep a top-notch performance.You will know:- Quick presentation about what FlashList, why we built, etc.- Migrating from FlatList to FlashList- Teaching how to write a performant list- Utilizing the tools provided by FlashList library (mainly the useBenchmark hook)- Using the Flipper plugins (flame graph, our lists profiler, UI & JS FPS profiler, etc.)- Optimizing performance of FlashList by using more advanced props like `getType`- 5-6 sample tasks where we’ll uncover and fix issues together- Q&A with Shopify team
React, TypeScript, and TDD
React Advanced 2021React Advanced 2021
174 min
React, TypeScript, and TDD
Top Content
Featured WorkshopFree
Paul Everitt
Paul Everitt
ReactJS is wildly popular and thus wildly supported. TypeScript is increasingly popular, and thus increasingly supported.

The two together? Not as much. Given that they both change quickly, it's hard to find accurate learning materials.

React+TypeScript, with JetBrains IDEs? That three-part combination is the topic of this series. We'll show a little about a lot. Meaning, the key steps to getting productive, in the IDE, for React projects using TypeScript. Along the way we'll show test-driven development and emphasize tips-and-tricks in the IDE.
Web3 Workshop - Building Your First Dapp
React Advanced 2021React Advanced 2021
145 min
Web3 Workshop - Building Your First Dapp
Top Content
Featured WorkshopFree
Nader Dabit
Nader Dabit
In this workshop, you'll learn how to build your first full stack dapp on the Ethereum blockchain, reading and writing data to the network, and connecting a front end application to the contract you've deployed. By the end of the workshop, you'll understand how to set up a full stack development environment, run a local node, and interact with any smart contract using React, HardHat, and Ethers.js.

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

A Guide to React Rendering Behavior
React Advanced 2022React Advanced 2022
25 min
A Guide to React Rendering Behavior
Top Content
This transcription provides a brief guide to React rendering behavior. It explains the process of rendering, comparing new and old elements, and the importance of pure rendering without side effects. It also covers topics such as batching and double rendering, optimizing rendering and using context and Redux in React. Overall, it offers valuable insights for developers looking to understand and optimize React rendering.
Building Better Websites with Remix
React Summit Remote Edition 2021React Summit Remote Edition 2021
33 min
Building Better Websites with Remix
Top Content
Remix is a web framework built on React Router that focuses on web fundamentals, accessibility, performance, and flexibility. It delivers real HTML and SEO benefits, and allows for automatic updating of meta tags and styles. It provides features like login functionality, session management, and error handling. Remix is a server-rendered framework that can enhance sites with JavaScript but doesn't require it for basic functionality. It aims to create quality HTML-driven documents and is flexible for use with different web technologies and stacks.
Don't Solve Problems, Eliminate Them
React Advanced 2021React Advanced 2021
39 min
Don't Solve Problems, Eliminate Them
Top Content
Kent C. Dodds discusses the concept of problem elimination rather than just problem-solving. He introduces the idea of a problem tree and the importance of avoiding creating solutions prematurely. Kent uses examples like Tesla's electric engine and Remix framework to illustrate the benefits of problem elimination. He emphasizes the value of trade-offs and taking the easier path, as well as the need to constantly re-evaluate and change approaches to eliminate problems.
React Compiler - Understanding Idiomatic React (React Forget)
React Advanced 2023React Advanced 2023
33 min
React Compiler - Understanding Idiomatic React (React Forget)
Top Content
Watch video: React Compiler - Understanding Idiomatic React (React Forget)
Joe Savona
Mofei Zhang
2 authors
The Talk discusses React Forget, a compiler built at Meta that aims to optimize client-side React development. It explores the use of memoization to improve performance and the vision of Forget to automatically determine dependencies at build time. Forget is named with an F-word pun and has the potential to optimize server builds and enable dead code elimination. The team plans to make Forget open-source and is focused on ensuring its quality before release.
Using useEffect Effectively
React Advanced 2022React Advanced 2022
30 min
Using useEffect Effectively
Top Content
Today's Talk explores the use of the useEffect hook in React development, covering topics such as fetching data, handling race conditions and cleanup, and optimizing performance. It also discusses the correct use of useEffect in React 18, the distinction between Activity Effects and Action Effects, and the potential misuse of useEffect. The Talk highlights the benefits of using useQuery or SWR for data fetching, the problems with using useEffect for initializing global singletons, and the use of state machines for handling effects. The speaker also recommends exploring the beta React docs and using tools like the stately.ai editor for visualizing state machines.
Routing in React 18 and Beyond
React Summit 2022React Summit 2022
20 min
Routing in React 18 and Beyond
Top Content
Routing in React 18 brings a native app-like user experience and allows applications to transition between different environments. React Router and Next.js have different approaches to routing, with React Router using component-based routing and Next.js using file system-based routing. React server components provide the primitives to address the disadvantages of multipage applications while maintaining the same user experience. Improving navigation and routing in React involves including loading UI, pre-rendering parts of the screen, and using server components for more performant experiences. Next.js and Remix are moving towards a converging solution by combining component-based routing with file system routing.