A workshop which dives deep into all React built-in hooks
- Node.js >= 20.0.0
- npm or yarn package manager
Clone the repository:
git clone https://github.com/andrekovac/react-hooks-workshop.git cd react-hooks-workshopInstall dependencies:
npm install
Start the development server:
npm run dev
Open your browser and navigate to the URL shown in the terminal (typically
http://localhost:5173)
npm run dev- Start the development servernpm run build- Build the project for productionnpm run preview- Preview the production build locallynpm run lint- Run ESLint to check code quality
This project includes React Compiler support. You can enable or disable it in vite.config.ts:
constReactCompilerConfig={sources: (_filename: string)=>{returntrue;// true = enabled, false = disabled},};See the React Compiler section for interactive examples and learning exercises.
- The heart of this document are the "CodeSandBox" links. Most of them are written in TypeScript, some in JavaScript (marked by "(JS)")
- One part of the document is a journey which gradually introduces hooks via
<Counter />scenarios. - Besides there are other examples which cover other hooks or interesting points concerning hooks.
- dependency array
- how to prevent race conditions when fetching data
- Covering "Removing dependencies: functions" heuristics
useEffectanduseStateinterplay- Covering "Removing dependencies: values" heuristics
Topics covered:
useLayoutEffectvs.useEffect- a-typical use of
useStatesetter function useEffect/useLayoutEffectdependency array with refs- React hooks lifecycle
React.memoHOC performance improvementTopics covered:
- how to avoid unnecessary React re-renderings
- Interplay with
useCallbackhook - How change in props triggers re-renderings
- Discussion of code organization
Topics covered:
- Automatic memoization vs manual optimization
- What React Compiler can and cannot optimize
- Interactive examples with enable/disable exercises
- React Compiler limitations and real-world considerations
- When to still use
React.memo,useCallback, anduseMemo - Reference stability and prop comparison
useEffectEventhook (React 19.2+)Topics covered:
- Reading latest props/state in Effects without re-runs
- Analytics and logging patterns
- Effect Event rules and ESLint enforcement
- Comparison with
useCallback
Topics covered:
SuspensecomponentErrorBoundarycomponentuseTransitionhookuseDeferredValuehookuseTransitionanduseDeferredValueinterplayuseTransitionanduseDeferredValuewithuseEffect
usePrevioushookuseStateerror:setStateon unmounted component- batched
useStatecalls - React Strictmode
- Composition
- The provided links to the React library are from its
serverimplementation. They however exemplify their functionality better then the hook definitions in other parts of the React codebase because they are more abstract there. - Working through all of the examples as part of a workshop will take at least one day, more realistically 1.5 days.
- This is a living document and far from perfect. I'll gradually improve it here and there and add new notes and examples.
This course is a collection of code examples which were inspired by other blog articles and code snippets written by wonderful developers. Sometimes I just adapted them from others. I learned a lot from them while working through their articles and examples. Thanks so much! The sections sometimes contain links to other helpful articles.
I have to point out one article in particular here: Many examples in this course are based on Dan Abramov's blog article A Complete Guide to useEffect. Thanks for this very enlightning read!
And a huge thanks to all my students with whom I worked through the examples. Your questions and comments are continuously improving this course. Thanks!
Open any example directly in StackBlitz:
React State Basics:
- Internal vs External State
- Mutable vs Immutable
- One State Change All New
- Setter Creates New Objects
- useState Basic
Async Setting and Retrieval:
useRef:
useEffect:
useReducer:
Class vs Function:
Performance:
React Compiler:
- React Compiler - Page 1 Problem
- React Compiler - Page 1 Solution
- React Compiler - Page 2 Problem
- React Compiler - Page 2 Solution
Custom Hook:
useEffectEvent:
useLayoutEffect:
useCallback:
- React eli5 Eli5 (explained like a 5 years old) are great articles for every topic. It's great that there's one for React, too.