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/js-hoist-regexp.md

View on GitHub
---
title: Hoist RegExp Creation
impact: LOW-MEDIUM
impactDescription: avoids recreation
tags: javascript, regexp, optimization, memoization
---

## Hoist RegExp Creation

Don't create RegExp inside render. Hoist to module scope or memoize with `useMemo()`.

**Incorrect (new RegExp every render):**

```tsx
function Highlighter({ text, query }: Props) {
  const regex = new RegExp(`(${query})`, 'gi')
  const parts = text.split(regex)
  return <>{parts.map((part, i) => ...)}</>
}
```

**Correct (memoize or hoist):**

```tsx
const EMAIL_REGEX = /^[^\s@]+@[^\s@]+\.[^\s@]+$/

function Highlighter({ text, query }: Props) {
  const regex = useMemo(
    () => new RegExp(`(${escapeRegex(query)})`, 'gi'),
    [query]
  )
  const parts = text.split(regex)
  return <>{parts.map((part, i) => ...)}</>
}
```

**Warning (global regex has mutable state):**

Global regex (`/g`) has mutable `lastIndex` state:

```typescript
const regex = /foo/g
regex.test('foo')  // true, lastIndex = 3
regex.test('foo')  // false, lastIndex = 0
```

Escape user search text and cap input lengths; an empty query needs explicit behavior. Global/sticky regex test/exec mutates lastIndex. Hoisting is valid only when pattern, flags and state semantics are preserved.
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