### Example: Lazy Initialization with Large Arrays Source: https://www.react.doctor/docs/rules/react-doctor/rerender-lazy-state-init Illustrates using lazy initialization for precomputing large arrays. This prevents the array from being recreated on every render, optimizing performance for costly setup operations. ```javascript useState(() => [...Array(10000)].map((_, i) => i)) ``` -------------------------------- ### Install Command Usage Source: https://www.react.doctor/docs/reference/cli-reference Install React Doctor, including optional local hooks and agent configurations. Use the '-y' flag to skip prompts and apply default settings. ```bash react-doctor install [options] ``` -------------------------------- ### Direct Path Import Example Source: https://www.react.doctor/docs/rules/react-doctor/no-barrel-import Import components directly from their source file path instead of a barrel file. ```javascript import { Button } from './components/Button' ``` -------------------------------- ### Install React Doctor for CI/CD Source: https://www.react.doctor/docs Run this command to install React Doctor and configure it to check every pull request using GitHub Actions. ```bash npx react-doctor@latest install ``` -------------------------------- ### Example: Dynamic Import for Monaco Editor Source: https://www.react.doctor/docs/rules/react-doctor/prefer-dynamic-import Replace static imports of Monaco Editor with `next/dynamic` and disable SSR. ```javascript const Editor = dynamic(() => import("@monaco-editor/react"), { ssr: false }) ``` -------------------------------- ### No Store Example Source: https://www.react.doctor/docs/rules/react-doctor/server-fetch-without-revalidate Use `{ cache: 'no-store' }` for fully dynamic data that should not be cached. ```javascript fetch(url, { cache: 'no-store' }) ``` -------------------------------- ### Barrel Import Example Source: https://www.react.doctor/docs/rules/react-doctor/no-barrel-import Avoid importing from barrel files like './components' which re-export multiple modules. ```javascript import { Button } from './components' ``` -------------------------------- ### Function Component Conversion Source: https://www.react.doctor/docs/rules/react-doctor/prefer-function-component This demonstrates the converted function component equivalent of the class component example, utilizing props directly. ```javascript function Foo({ foo }) { return
{foo}
; } ``` -------------------------------- ### useState with Non-Conventional Value Name Source: https://www.react.doctor/docs/rules/react-doctor/hook-use-state This example uses a value name that starts with an uppercase letter, preventing the automatic derivation of a `set` setter name. The rule prefers lowercase starting identifiers for state values to ensure a derivable setter name. ```javascript const [RGB, setRGB] = useState(initialValue); ``` -------------------------------- ### Use POST for Data Modifications with Tanstack Start Source: https://www.react.doctor/docs/rules Use `createServerFn({ method: 'POST' })` for data modifications to prevent CSRF vulnerabilities, as GET requests can be triggered by prefetching. ```javascript import { createServerFn } from '@tanstack/start'; const updateData = createServerFn({ method: 'POST', handler: async ({ data }) => { // ... update data ... } }); ``` -------------------------------- ### Correct: Using useMemo for Stable Store Initialization Source: https://www.react.doctor/docs/rules/react-doctor/no-create-store-in-render This snippet illustrates using `useMemo` to create a stable store instance. The store is created only when its dependencies change, preventing unnecessary re-creation on every render. ```javascript import { useMemo } from 'react'; import { create } from 'zustand'; function App(props) { const useStore = useMemo(() => { return create((set) => ({ // Initialize with props or other stable values initialValue: props.initialValue, })); }, [props.initialValue]); // Dependency array ensures re-creation only when needed // ... use the store } ``` -------------------------------- ### Incorrect: Non-'on...' Prop with 'handle...' Handler Source: https://www.react.doctor/docs/rules/react-doctor/jsx-handler-names This example shows a scenario where a prop that does not start with 'on...' is associated with a 'handle...' prefixed function. This violates the naming convention. ```jsx