Index of every page on this site: llms.txt
All skills
grahamcrackers avatar

/bulletproof-react-patterns

@ca83de4
Run it yourself

Bulletproof React architecture patterns for scalable, maintainable applications. Covers feature-based project structure, component patterns, state management boundaries, API layer design, error handling, security, and testing strategies. Use when structuring a React project, designing application architecture, organizing features, or when the user asks about React project structure or scalable patterns.

Use this Skill: https://skilld.dev/gh/grahamcrackers/skills/bulletproof-react-patterns

Nothing lands on disk. Nothing to clean up.

Fork this Skill

Edit a local copy. It keeps the author and licence.

referencesperformance.md

≈1.1k tokens on demand. Your agent reads this file only when SKILL.md points to it.

Performance

Code Splitting

Split production JavaScript at the route level to optimize loading times. Only essential code is loaded initially — additional parts are fetched lazily as needed.

// app/router.tsx
import { lazy } from "react";

const Dashboard = lazy(() => import("./routes/dashboard"));
const Settings = lazy(() => import("./routes/settings"));

Avoid excessive splitting — too many small chunks increases request overhead. Focus on critical route-level boundaries.

Data Prefetching

Prefetch data before the user navigates to reduce perceived load time:

function DiscussionLink({ id }: { id: string }) {
    const queryClient = useQueryClient();

    const prefetch = () => {
        queryClient.prefetchQuery({
            queryKey: ["discussion", id],
            queryFn: () => getDiscussion(id),
        });
    };

    return (
        <Link to={`/discussions/${id}`} onMouseEnter={prefetch} onFocus={prefetch}>
            View Discussion
        </Link>
    );
}

State Optimization

Split State

Don't put everything in a single state — split to avoid unnecessary re-renders:

// Instead of one monolithic state:
const [state, setState] = useState({ count: 0, name: "", isOpen: false });

// Split by concern:
const [count, setCount] = useState(0);
const [name, setName] = useState("");
const [isOpen, setIsOpen] = useState(false);

Keep State Close

Keep state as close as possible to where it's used. Don't hoist state to a global store when it's only needed by one component tree.

State Initializer Functions

Use initializer functions for expensive computations:

// Runs on every render:
const [state, setState] = useState(expensiveFn());

// Runs only once:
const [state, setState] = useState(() => expensiveFn());

Atomic State Libraries

For applications tracking many elements at once, consider atomic state libraries like Jotai for granular updates.

Children as Optimization

The children prop is the simplest way to avoid unnecessary re-renders. JSX passed as children represents an isolated VDOM structure that does not re-render when the parent's state changes:

// Not optimized — PureComponent re-renders on every count update
function Counter() {
    const [count, setCount] = useState(0);
    return (
        <div>
            <button onClick={() => setCount((c) => c + 1)}>count is {count}</button>
            <PureComponent />
        </div>
    );
}

// Optimized — PureComponent does NOT re-render
function App() {
    return (
        <Counter>
            <PureComponent />
        </Counter>
    );
}

function Counter({ children }: { children: React.ReactNode }) {
    const [count, setCount] = useState(0);
    return (
        <div>
            <button onClick={() => setCount((c) => c + 1)}>count is {count}</button>
            {children}
        </div>
    );
}

Context Performance

React Context is good for low-velocity data (themes, user data, small state). For medium/high-velocity data:

  • Use use-context-selector for selector support
  • Or use state libraries with built-in selectors (Zustand, Jotai)

Before reaching for Context, consider lifting state up or proper component composition.

Styling Performance

For high-frequency updates, prefer zero-runtime styling over runtime CSS-in-JS:

Zero Runtime (Preferred) Runtime (Avoid for Perf)
Tailwind CSS styled-components
vanilla-extract emotion
CSS Modules

Image Optimization

  • Lazy load images not in the viewport.
  • Use modern formats (WebP) for faster loading.
  • Use srcset to load optimal images for the client's screen size.

Web Vitals

Monitor Core Web Vitals (LCP, INP, CLS) using Lighthouse and PageSpeed Insights.

Source: SKILL.md on GitHub

No rule matched.

skilld matched fixed text patterns in SKILL.md and file names. Patterns miss obfuscated code.

skilld run checks every file with the same patterns. It asks for approval before it loads a Skill with a behavior marked Needs approval.

No alerts2mo5 checks · Risk SAFE
  • Gen Agent Trust Hub2mo

    This skill is an architectural guide for React development, providing best practices for project structure, state management, and testing. It contains exclusively educational content and code examples with no executable or malicious scripts.

  • Socket2mo

    No alerts

  • Snyk2mo

    Risk: LOW · No issues

  • Runlayer9mo

    1/11 files flagged

  • ZeroLeaks7mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at ca83de4. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 2 months ago.

Dormantupdated 9 months ago

README badge

README badge for grahamcrackers/skills/bulletproof-react-patterns