Skip to content

sickn33/agentic-awesome-skills / plugins/aasb-web-app/skills/react-best-practices

react-best-practices

Verified

Comprehensive performance optimization guide for React and Next.js applications, adapted from Vercel’s published guidance. Use when writing new React components or Next.js pages, implementing data fetching (client or server-side), or reviewing code for performance issues.

FileKindSize
AGENTS.mdmarkdown56.3 KB
README.mdmarkdown1.1 KB
SKILL.mdskill md6.0 KB
metadata.jsonconfig641 B
rules/_sections.mdmarkdown1.5 KB
rules/_template.mdmarkdown631 B
rules/advanced-event-handler-refs.mdmarkdown1.7 KB
rules/advanced-use-latest.mdmarkdown1.4 KB
rules/async-api-routes.mdmarkdown1.4 KB
rules/async-defer-await.mdmarkdown2.2 KB
rules/async-dependencies.mdmarkdown1.1 KB
rules/async-parallel.mdmarkdown874 B
rules/async-suspense-boundaries.mdmarkdown2.7 KB
rules/bundle-barrel-imports.mdmarkdown1012 B
rules/bundle-conditional.mdmarkdown1014 B
rules/bundle-defer-third-party.mdmarkdown1.1 KB
rules/bundle-dynamic-imports.mdmarkdown1.0 KB
rules/bundle-preload.mdmarkdown1.4 KB
rules/client-event-listeners.mdmarkdown2.1 KB
rules/client-swr-dedup.mdmarkdown1.4 KB
rules/js-batch-dom-css.mdmarkdown2.2 KB
rules/js-cache-function-results.mdmarkdown1.8 KB
rules/js-cache-property-access.mdmarkdown638 B
rules/js-cache-storage.mdmarkdown2.0 KB
rules/js-combine-iterations.mdmarkdown753 B
rules/js-early-exit.mdmarkdown1.2 KB
rules/js-hoist-regexp.mdmarkdown1.2 KB
rules/js-index-maps.mdmarkdown1.0 KB
rules/js-length-check-first.mdmarkdown1.7 KB
rules/js-min-max-loop.mdmarkdown2.1 KB
rules/js-set-map-lookups.mdmarkdown532 B
rules/js-tosorted-immutable.mdmarkdown1.9 KB
rules/rendering-activity.mdmarkdown819 B
rules/rendering-animate-svg-wrapper.mdmarkdown1.3 KB
rules/rendering-conditional-render.mdmarkdown980 B
rules/rendering-content-visibility.mdmarkdown1.0 KB
rules/rendering-hoist-jsx.mdmarkdown1.0 KB
rules/rendering-hydration-no-flicker.mdmarkdown1.5 KB
rules/rendering-svg-precision.mdmarkdown794 B
rules/rerender-defer-reads.mdmarkdown973 B
rules/rerender-dependencies.mdmarkdown824 B
rules/rerender-derived-state.mdmarkdown724 B
rules/rerender-functional-setstate.mdmarkdown3.1 KB
rules/rerender-lazy-state-init.mdmarkdown2.4 KB
rules/rerender-memo.mdmarkdown1.3 KB
rules/rerender-transitions.mdmarkdown1.3 KB
rules/server-after-nonblocking.mdmarkdown2.2 KB
rules/server-cache-lru.mdmarkdown1.7 KB
rules/server-cache-react.mdmarkdown899 B
rules/server-parallel-fetching.mdmarkdown1.7 KB
rules/server-serialization.mdmarkdown1.3 KB

rules/advanced-event-handler-refs.md

View on GitHub
---
title: Store Event Handlers in Refs
impact: LOW
impactDescription: stable subscriptions
tags: advanced, hooks, refs, event-handlers, optimization
---

## Store Event Handlers in Refs

Store callbacks in refs when used in effects that shouldn't re-subscribe on callback changes.

**Incorrect (re-subscribes on every render):**

```tsx
function useWindowEvent(event: string, handler: () => void) {
  useEffect(() => {
    window.addEventListener(event, handler)
    return () => window.removeEventListener(event, handler)
  }, [event, handler])
}
```

**Correct (stable subscription):**

```tsx
function useWindowEvent(event: string, handler: () => void) {
  const handlerRef = useRef(handler)
  useEffect(() => {
    handlerRef.current = handler
  }, [handler])

  useEffect(() => {
    const listener = () => handlerRef.current()
    window.addEventListener(event, listener)
    return () => window.removeEventListener(event, listener)
  }, [event])
}
```

**Alternative: use `useEffectEvent` if you're on latest React:**

```tsx
import { useEffectEvent } from 'react'

function useWindowEvent(event: string, handler: () => void) {
  const onEvent = useEffectEvent(handler)

  useEffect(() => {
    window.addEventListener(event, onEvent)
    return () => window.removeEventListener(event, onEvent)
  }, [event])
}
```

`useEffectEvent` provides a cleaner API for the same pattern: it accesses the latest committed handler when invoked from Effect-managed events.

Effect Events require a supporting React version and hooks linter. They access latest committed values but do not promise a stable identity; exclude Effect Events from Effect dependencies. Do not use them to hide real dependencies. The ref fallback updates after commit, not synchronously during render.
brainstormingobra/superpowersYou MUST use this before any creative work - creating features, building components, adding functionality, modifying behavior, or planning anything new, in software or out of it (a talk, a business, a renovation).Stars: 322kUpdated Claude CodeCodexVerifiedInstall
brand-guidelinesanthropics/skillsApplies Anthropic's official brand colors and typography to any sort of artifact that may benefit from having Anthropic's look-and-feel. Use it when brand colors or style guidelines, visual formatting, or company design standards apply.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
frontend-designanthropics/skillsGuidance for distinctive, intentional visual design when building new UI or reshaping an existing one. Helps with aesthetic direction, typography, and making choices that don't read as templated defaults.Stars: 198kUpdated Claude CodeCodexVerifiedInstall
mcp-builderanthropics/skillsGuide for creating high-quality MCP (Model Context Protocol) servers that enable LLMs to interact with external services through well-designed tools. Use when building MCP servers to integrate external APIs or services, whether in Python (FastMCP) or Node/TypeScript (MCP SDK).Stars: 198kUpdated Claude CodeCodexVerifiedInstall