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-use-latest.md

View on GitHub
---
title: useLatest for Stable Callback Refs
impact: LOW
impactDescription: prevents effect re-runs
tags: advanced, hooks, useLatest, refs, optimization
---

## useLatest for Stable Callback Refs

Access latest values in callbacks without adding them to dependency arrays. Prevents effect re-runs while avoiding stale closures.

**Implementation:**

```typescript
function useLatest<T>(value: T) {
  const ref = useRef(value)
  useEffect(() => {
    ref.current = value
  }, [value])
  return ref
}
```

**Incorrect (effect re-runs on every callback change):**

```tsx
function SearchInput({ onSearch }: { onSearch: (q: string) => void }) {
  const [query, setQuery] = useState('')

  useEffect(() => {
    const timeout = setTimeout(() => onSearch(query), 300)
    return () => clearTimeout(timeout)
  }, [query, onSearch])
}
```

**Correct (stable effect, fresh callback):**

```tsx
function SearchInput({ onSearch }: { onSearch: (q: string) => void }) {
  const [query, setQuery] = useState('')
  const onSearchRef = useLatest(onSearch)

  useEffect(() => {
    const timeout = setTimeout(() => onSearchRef.current(query), 300)
    return () => clearTimeout(timeout)
  }, [query])
}
```

This ref updates in a passive Effect and can temporarily expose the previous committed callback. Do not remove reactive dependencies merely to silence the linter; use a supported Effect Event when its semantics fit.
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