Fix Your Slow React App With React-Scan
By Jack Herrington
Key Concepts
- React performance killers: excessive rendering and slow components.
- React Scan: A tool to identify and address these performance issues.
- TanStack Store: A simple state management library used in the demo app.
- Derived values: Values in a store that are automatically updated based on dependencies.
useStorehook: A hook from TanStack Store that connects to React components.- Selector function: A function used with
useStoreto select specific data from the store. - FPS (Frames Per Second): A measure of performance, indicating how smoothly the application is running.
React Scan: Addressing Performance Killers
The video introduces React Scan, a tool designed to help developers identify and resolve two common performance bottlenecks in React applications: excessive rendering and slow components. The demonstration uses a TanStack Router app with a TanStack Store example to illustrate these issues.
Setting Up the Demo Application
- Creating the App: The presenter uses
create-ts-router-appwith theadd-onsflag to generate a TanStack Router application. The TanStack Store example is chosen for its suitability in demonstrating over-rendering. - TanStack Store Implementation: The store is created using the
storefunction from TanStack Store. It holdsfirstNameandlastName, and afullNamederived value that automatically updates when either name changes. - Component Usage: The
useStorehook is used in thedemo.store.tsxcomponent to access and update the store's state. ThefirstName,lastName, andfullNamecomponents display the corresponding values from the store.
Integrating and Using React Scan
- Installation: React Scan is installed as a dependency.
- Enabling React Scan: In the
main.tsxfile,scanis imported fromreact-scanand enabled usingscan({ enabled: true }). The order of import is important:react-scanshould be imported beforereact. - Functionality: React Scan adds a visual indicator to the application, showing when components re-render. It highlights components that are rendering more than expected.
Identifying Over-Rendering
- Introducing the Problem: The presenter intentionally introduces over-rendering by removing the selector function from the
useStorehook in thefirstNameandlastNamecomponents. This causes the entire store object to be passed to the components, leading to unnecessary re-renders. - React Scan's Insight: React Scan highlights that all components are re-rendering whenever any field in the store is updated.
- Detailed Analysis: Clicking on a component in React Scan reveals why it re-rendered, showing that the
lastNamechanged even when only thefirstNamewas modified. This level of detail is presented as superior to React DevTools.
Identifying Slow Components
- Creating a Slow Component: The presenter modifies the
fullNamecomponent to render each character individually using aCharactercomponent. - Adding Artificial Delay: The
Charactercomponent is intentionally slowed down by adding a loop that performs a large number of calculations. - React Scan's Notification System: React Scan detects the FPS drop caused by the slow
Charactercomponent and displays a notification. - Detailed Information: Clicking on the notification reveals that the
Charactercomponent is the source of the performance issue, highlighting the excessive number of renders and their slow execution time. - AI Prompts: React Scan generates AI prompts that can be copied and pasted into tools like Claude or ChatGPT to get assistance in resolving the performance problem. The prompt includes relevant information about the component and the performance issue.
Infinite Red Sponsorship
The video includes a sponsorship segment for Infinite Red, a React Native consultancy. Infinite Red is praised for their expertise in React Native, their open-source contributions (including the Ignite framework), and their Chain React conference.
Conclusion
React Scan is presented as a valuable tool for identifying and addressing performance issues in React applications. It helps developers pinpoint excessive rendering and slow components, providing detailed information and AI-powered assistance to resolve these problems. The tool is free to use and offers insights beyond those available in standard React DevTools.
Chat with this Video
AI-PoweredHi! I can answer questions about this video "Fix Your Slow React App With React-Scan". What would you like to know?