react-best-practices
Skill14 starsChanged 37 days agoMITView on GitHub
Vercel Engineering's React & Next.js performance optimization guide. Use when writing React components, implementing data fetching, reviewing code for performance, refactoring, or optimizing bundle size. Contains 40+ rules across 8 priority categories.
- Installs packages
66 other projects use a version of this.
Adopted by
Most recently taken up by
- eea/eea.agent.skillstheir own version, first seen Nov 8, 2026
- amanuelmamo7/Skillstheir own version, first seen Nov 7, 2026
- drinkspiller/skillstheir own version, first seen Nov 6, 2026
- tylergibbs1/codewardentheir own version, first seen Nov 1, 2026
- sbrudz/agent-skillstheir own version, first seen Nov 1, 2026
- poizoncoded/skillstheir own version, first seen Nov 1, 2026
Used with
Projects with it also use planning-with-files, vercel-deploy and 5 more.
Other files people use alongside this one.
Files found in the same public projects as a version of this one, more often than they turn up anywhere else. Each project counts once, large collections count for less, and this file's own repository is left out.
MicrosoftSkillplanning-with-files
**USE THIS FOR COMPLEX TASKS.** Implements Manus-style file-based planning for multi-step tasks. Creates task_plan.md, findings.md, and progress.md in .agent_cache/<task-name>/. Use when: implementing multiple APIs, refactoring modules, research tasks, or ANY task requiring >5 tool calls.
Also in 5 projects that use this
VercelSkillvercel-deploy
Deploy applications and websites to Vercel with a claimable preview. Use when the user requests deployment actions like "deploy my app", "deploy and give me the link", "push this live", or "create a preview deployment". No authentication required - returns preview URL and claimable deployment link.
Also in 3 projects that use this
MicrosoftSkillcode-quality
Statically check changed C# against the same deterministic rules github-code-quality[bot] runs on pull requests, so a contributor can fix likely findings before opening the PR and keep the bot's inline comments to a minimum. Activate when a contributor asks to "check code quality", "run code quality on my changes", "will the code quality bot complain", "pre-check before I open the PR", "code quality check", or similar. Scoped to C#. Reads the diff and compares it against the enumerated csharp-code-quality rule set below — no CodeQL download or build required. This is a fast approximation of a dataflow engine, not a reproduction; report accordingly. Does NOT apply fixes unless explicitly asked.
Also in 5 projects that use this
JetBrainsSkillwebapp-testing
Toolkit for interacting with and testing local web applications using Playwright. Supports verifying frontend functionality, debugging UI behavior, capturing browser screenshots, and viewing browser logs.
Also in 7 projects that use this
ttmouseSkillnotebooklm
Automate Google NotebookLM - create notebooks, add sources, generate podcasts/videos/quizzes, download artifacts. Supports browser-based content extraction for anti-crawler platforms (Twitter/X, LinkedIn, etc.). Activates on explicit /notebooklm or intent like "create a podcast about X
Also in 3 projects that use this
JetBrainsSkilltheme-factory
Toolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with colors/fonts that you can apply to any artifact that has been creating, or can generate a new theme on-the-fly.
Also in 4 projects that use this
- +1 more from microsoft
Other versions66 projects use a version of this.
Word for word · 0
None found.
Edited · 0
None found.
Wrote their own version · 66
- mastra-ai/mastra★ 29ksame name
- openai/plugins★ 7.8ksame name
- ryokun6/ryos★ 1.3ksame name
- involvex/youtube-music-cli★ 477same name
- vercel/vercel-plugin★ 328same name
- mikeOnBreeze/cc-crossbeam★ 297same name
- typv/nest-turbo-starter★ 224same name
- AvivK5498/Beads-Kanban-UI★ 102same name
- mxyhi/token_proxy★ 94same name
- laguagu/claude-code-nextjs-skills★ 72same name
56 more not listed
How matches are found. Word for word: the exact same text. Edited: shares passages with this file (“99% same” means 99% of its five-word phrases also appear here; 10% or more counts). Their own version: similar meaning, or, weakest, the same name. We can’t tell which project had it first. Projects are counted among the 200 most-starred candidates, and mentions are links to the repository we found, up to 50 per site, so both counts are a floor.
In this repository44 other files it gives its agents.
- CLAUDE.mdCLAUDE.md
- adaptSkill
- animateSkill
- auditSkill
- bolderSkill
- brainstormSkill
- build-fixSkill
- business-panelSkill
- checkpointSkill
- clarifySkill
- colorizeSkill
- composition-patternsSkill
- confidence-checkSkill
- config-doctorSkill
- critiqueSkill
- delightSkill
- design-auditSkill
- devils-advocateSkill
- distillSkill
- eval-harnessSkill
- feature-plannerSkill
- fix-prSkill
- frontend-designSkill
- gap-analysisSkill
- grill-with-docsSkill
- hardenSkill
- impeccableSkill
- layoutSkill
- learnSkill
- optimizeSkill
- overdriveSkill
- polishSkill
- pytest-runnerSkill
- python-best-practicesSkill
- quieterSkill
- sc-helpSkill
- security-auditSkill
- shapeSkill
- tddSkill
- typesetSkill
- ui-ux-pro-maxSkill
- uv-packageSkill
- verifySkill
- web-design-guidelinesSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
14 rules. 0 are common, 13 are distinctive, and 1 is very specific to this project.
0
Common
Most of the 56 edited versions we read keep these.
13
Distinctive
This author's own choices. Worth a look.
1
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 25
Required Rule: Use pnpm (npm, yarn forbidden)
- Very specific: line 123
Use better-all for operations with complex dependencies.
- Distinctive: line 162
Use direct source imports instead of barrel files.
- Distinctive: line 325
Use SWR for client-side data fetching with built-in deduplication.
- Distinctive: line 363
Read state at the point of use, not at the top of components.
- Distinctive: line 405
Use primitives instead of objects in dependency arrays.
- Distinctive: line 445
Use transitions for non-critical updates.
- Distinctive: line 497
Use Activity (or custom equivalent) for frequently shown/hidden content.
- Distinctive: line 519
Use ternary operators for clearer render paths.
- Distinctive: line 561
Avoid repeated property lookups.
- Distinctive: line 579
Use classes or cssText for multiple style changes.
- Distinctive: line 595
Avoid recreating RegExp on every render.
- Distinctive: line 625
Avoid mutating original arrays.
- Distinctive: line 679
Progressive Enhancement: Start with critical optimizations (Priority 1-2) before lower priorities.
“Common” is measured: kept by at least 6 in 10 of the 56 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Vercel React Best Practices
Comprehensive performance optimization guide for React and Next.js applications, maintained by Vercel Engineering. Contains 40+ rules across 8 categories prioritized by impact.
When to Apply
Reference these guidelines when:
- Writing new React components or Next.js pages
- Implementing data fetching patterns
- Reviewing code for performance issues
- Refactoring existing code
- Optimizing bundle size or load times
Priority 0: Package Management (MANDATORY)
Required Rule: Use pnpm (npm, yarn forbidden)
Rule 0.1: Package Manager
| Item | Rule |
| Package Manager | pnpm (mandatory) |
| Lock File | pnpm-lock.yaml (must commit) |
| Workspace | pnpm-workspace.yaml (monorepo) |
# ✅ Correct
pnpm add react next
pnpm add -D typescript @types/react
pnpm install --frozen-lockfile
# ❌ Incorrect
npm install react next
yarn add react nextRule 0.2: Dockerfile Pattern
# ✅ Correct - pnpm in Docker
FROM node:20-slim
RUN corepack enable && corepack prepare pnpm@latest --activate
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN pnpm install --frozen-lockfile --prod
COPY . .
CMD ["pnpm", "start"]Rule 0.3: CI/CD Pattern (GitHub Actions)
# ✅ Correct - pnpm in CI
- uses: pnpm/action-setup@v2
with:
version: 9
- uses: actions/setup-node@v4
with:
node-version: '20'
cache: 'pnpm'
- run: pnpm install --frozen-lockfile
- run: pnpm test
- run: pnpm buildDetection: Suggest migration to pnpm if package-lock.json or yarn.lock found
Priority 1: Eliminating Waterfalls (CRITICAL)
Impact: 2-10x performance improvement
Rule 1.1: Defer await to Point of Use
Move await statements to where the data is actually needed.
// ❌ Incorrect - blocks entire function
async function Page() {
const data = await fetchData();
const user = await fetchUser();
return <Component data={data} user={user} />;
}
// ✅ Correct - parallel fetching
async function Page() {
const dataPromise = fetchData();
const userPromise = fetchUser();
return <Component dataPromise={dataPromise} userPromise={userPromise} />;
}Rule 1.2: Use Promise.all for Independent Operations
Execute independent operations concurrently.
// ❌ Incorrect - sequential
const users = await getUsers();
const posts = await getPosts();
const comments = await getComments();
// ✅ Correct - parallel
const [users, posts, comments] = await Promise.all([
getUsers(),
getPosts(),
getComments()
]);Rule 1.3: Dependency-Based Parallelization
Use better-all for operations with complex dependencies.
import { all } from 'better-all';
// ✅ Correct - handles dependencies automatically
const { user, posts, notifications } = await all({
user: () => fetchUser(userId),
posts: ({ user }) => fetchPosts(user.id),
notifications: ({ user }) => fetchNotifications(user.id)
});Rule 1.4: Strategic Suspense Boundaries
Deploy Suspense boundaries for faster initial paint.
// ✅ Correct - progressive loading
function Page() {
return (
<div>
<Header /> {/* Renders immediately */}
<Suspense fallback={<Skeleton />}>
<SlowContent /> {/* Streams in when ready */}
</Suspense>
</div>
);
}Priority 2: Bundle Size Optimization (CRITICAL)
Impact: 200-800ms faster load times
Rule 2.1: Avoid Barrel File Imports
Use direct source imports instead of barrel files.
// ❌ Incorrect - imports entire barrel
import { Button } from '@/components';
import { formatDate } from '@/utils';
// ✅ Correct - direct imports
import { Button } from '@/components/Button';
import { formatDate } from '@/utils/formatDate';Rule 2.2: Dynamic Import Heavy Components
Lazy-load components that aren't immediately needed.
import dynamic from 'next/dynamic';
// ✅ Correct - lazy load heavy component
const HeavyChart = dynamic(() => import('./HeavyChart'), {
loading: () => <ChartSkeleton />,
ssr: false // Skip SSR for client-only components
});Rule 2.3: Defer Third-Party Scripts
Load non-critical scripts after hydration.
import Script from 'next/script';
// ✅ Correct - deferred loading
<Script
src="https://analytics.example.com/script.js"
strategy="lazyOnload"
/>Rule 2.4: Conditional Module Loading
Load modules only when features are active.
// ✅ Correct - load only when needed
async function handleAdvancedFeature() {
const { advancedFunction } = await import('./advancedModule');
return advancedFunction();
}Rule 2.5: Intent-Based Preloading
Preload bundles based on user intent.
// ✅ Correct - preload on hover
function NavLink({ href, children }) {
const handleMouseEnter = () => {
import(`./pages${href}`); // Preload the route
};
return (
<Link href={href} onMouseEnter={handleMouseEnter}>
{children}
</Link>
);
}Priority 3: Server-Side Performance (HIGH)
Rule 3.1: Use React.cache for Request Deduplication
Deduplicate data fetching within a single request.
import { cache } from 'react';
// ✅ Correct - cached per request
export const getUser = cache(async (id: string) => {
return await db.user.findUnique({ where: { id } });
});Rule 3.2: Cross-Request LRU Caching
Cache expensive computations across requests.
import { LRUCache } from 'lru-cache';
const cache = new LRUCache<string, any>({ max: 500 });
// ✅ Correct - cross-request cache
export async function getExpensiveData(key: string) {
if (cache.has(key)) return cache.get(key);
const data = await computeExpensive(key);
cache.set(key, data);
return data;
}Rule 3.3: Minimize RSC Serialization
Reduce data passed across RSC boundaries.
// ❌ Incorrect - passes entire object
<ClientComponent user={fullUserObject} />
// ✅ Correct - minimal data
<ClientComponent
userName={user.name}
userAvatar={user.avatar}
/>Rule 3.4: Parallel Data Fetching via Composition
Structure components to enable parallel fetching.
// ✅ Correct - parallel through composition
function Page() {
return (
<>
<UserSection userId={id} /> {/* fetches user */}
<PostsSection userId={id} /> {/* fetches posts in parallel */}
<CommentsSection postId={pid} /> {/* fetches comments in parallel */}
</>
);
}Rule 3.5: Non-Blocking Operations with after()
Schedule operations that don't block the response.
import { after } from 'next/server';
// ✅ Correct - non-blocking analytics
export async function GET() {
const data = await fetchData();
after(() => {
logAnalytics(data); // Runs after response sent
});
return Response.json(data);
}Priority 4: Client-Side Data Fetching (MEDIUM-HIGH)
Rule 4.1: Automatic Deduplication with SWR
Use SWR for client-side data fetching with built-in deduplication.
import useSWR from 'swr';
// ✅ Correct - deduplicated across components
function useUser(id: string) {
return useSWR(`/api/user/${id}`, fetcher);
}Rule 4.2: useSWRSubscription for Event Listeners
Deduplicate global event listeners.
import useSWRSubscription from 'swr/subscription';
// ✅ Correct - single listener, shared state
function useOnlineStatus() {
return useSWRSubscription('online-status', (key, { next }) => {
const handler = () => next(null, navigator.onLine);
window.addEventListener('online', handler);
window.addEventListener('offline', handler);
return () => {
window.removeEventListener('online', handler);
window.removeEventListener('offline', handler);
};
});
}Priority 5: Re-render Optimization (MEDIUM)
Rule 5.1: Defer State Reads
Read state at the point of use, not at the top of components.
// ❌ Incorrect - reads at top, re-renders entire tree
function Parent() {
const count = useStore(state => state.count);
return <Child count={count} />;
}
// ✅ Correct - reads where needed
function Parent() {
return <Child />;
}
function Child() {
const count = useStore(state => state.count);
return <span>{count}</span>;
}Rule 5.2: Extract Work into Memoized Components
Isolate expensive renders.
// ✅ Correct - isolated expensive render
const ExpensiveList = memo(function ExpensiveList({ items }) {
return items.map(item => <ExpensiveItem key={item.id} item={item} />);
});
function Parent({ items, otherState }) {
return (
<div>
<ExpensiveList items={items} />
<CheapComponent state={otherState} />
</div>
);
}Rule 5.3: Narrow Effect Dependencies
Use primitives instead of objects in dependency arrays.
// ❌ Incorrect - object reference changes
useEffect(() => {
fetchData(user);
}, [user]);
// ✅ Correct - stable primitive
useEffect(() => {
fetchData(userId);
}, [userId]);Rule 5.4: Functional setState Updates
Prevent stale closures with functional updates.
// ❌ Incorrect - may use stale count
setCount(count + 1);
// ✅ Correct - always uses current value
setCount(prev => prev + 1);Rule 5.5: Lazy State Initialization
Defer expensive initial state computation.
// ❌ Incorrect - runs every render
const [data, setData] = useState(expensiveComputation());
// ✅ Correct - runs once
const [data, setData] = useState(() => expensiveComputation());Rule 5.6: Mark Non-Urgent Updates as Transitions
Use transitions for non-critical updates.
import { useTransition } from 'react';
function SearchResults() {
const [isPending, startTransition] = useTransition();
const handleSearch = (query) => {
startTransition(() => {
setSearchResults(filterResults(query));
});
};
}Priority 6: Rendering Performance (MEDIUM)
Rule 6.1: Hoist Static JSX
Move static elements outside components.
// ✅ Correct - static JSX hoisted
const StaticHeader = <header><h1>Welcome</h1></header>;
function Page() {
return (
<div>
{StaticHeader}
<DynamicContent />
</div>
);
}Rule 6.2: Use content-visibility for Long Lists
Apply CSS containment for off-screen content.
/* ✅ Correct - skip rendering off-screen items */
.list-item {
content-visibility: auto;
contain-intrinsic-size: 0 50px;
}Rule 6.3: Activity Components for Frequent Toggles
Use Activity (or custom equivalent) for frequently shown/hidden content.
// ✅ Correct - preserves state, skips unmount/remount
<Activity mode={isVisible ? 'visible' : 'hidden'}>
<ExpensiveComponent />
</Activity>Rule 6.4: Hardware-Accelerated SVG Animation
Animate wrapper elements for better performance.
// ✅ Correct - hardware acceleration
<motion.div animate={{ x: 100 }}>
<svg>{/* static SVG content */}</svg>
</motion.div>Rule 6.5: Explicit Conditional Rendering
Use ternary operators for clearer render paths.
// ❌ Incorrect - implicit falsy render
{items.length && <List items={items} />}
// ✅ Correct - explicit conditional
{items.length > 0 ? <List items={items} /> : null}Priority 7: JavaScript Performance (LOW-MEDIUM)
Rule 7.1: Build Index Maps for Lookups
Replace repeated array searches with Map lookups.
// ❌ Incorrect - O(n) per lookup
const user = users.find(u => u.id === targetId);
// ✅ Correct - O(1) lookup
const userMap = new Map(users.map(u => [u.id, u]));
const user = userMap.get(targetId);Rule 7.2: Use Set for Membership Checks
O(1) instead of O(n) for includes checks.
// ❌ Incorrect - O(n)
if (allowedIds.includes(id)) { }
// ✅ Correct - O(1)
const allowedSet = new Set(allowedIds);
if (allowedSet.has(id)) { }Rule 7.3: Cache Property Access in Loops
Avoid repeated property lookups.
// ❌ Incorrect - repeated access
for (let i = 0; i < arr.length; i++) {
process(obj.nested.value);
}
// ✅ Correct - cached access
const { length } = arr;
const { value } = obj.nested;
for (let i = 0; i < length; i++) {
process(value);
}Rule 7.4: Batch DOM CSS Changes
Use classes or cssText for multiple style changes.
// ❌ Incorrect - multiple reflows
el.style.width = '100px';
el.style.height = '100px';
el.style.margin = '10px';
// ✅ Correct - single reflow
el.classList.add('active-state');
// or
el.style.cssText = 'width:100px;height:100px;margin:10px';Rule 7.5: Hoist RegExp Outside Renders
Avoid recreating RegExp on every render.
// ❌ Incorrect - creates new RegExp each render
function Component({ value }) {
const isValid = /^[a-z]+$/.test(value);
}
// ✅ Correct - single instance
const ALPHA_REGEX = /^[a-z]+$/;
function Component({ value }) {
const isValid = ALPHA_REGEX.test(value);
}Rule 7.6: Early Returns
Exit functions early when possible.
// ✅ Correct - early return pattern
function processUser(user) {
if (!user) return null;
if (!user.active) return null;
return performExpensiveOperation(user);
}Rule 7.7: Prefer .toSorted() Over .sort()
Avoid mutating original arrays.
// ❌ Incorrect - mutates original
const sorted = items.sort((a, b) => a.id - b.id);
// ✅ Correct - returns new array
const sorted = items.toSorted((a, b) => a.id - b.id);Priority 8: Advanced Patterns (LOW)
Rule 8.1: Store Event Handlers in Refs
Stable subscriptions without effect re-runs.
// ✅ Correct - stable callback reference
const callbackRef = useRef(callback);
callbackRef.current = callback;
useEffect(() => {
return subscribe(() => callbackRef.current());
}, []); // Empty deps - never re-subscribesRule 8.2: useEffectEvent for Fresh Callbacks
Access latest values without re-running effects.
// ✅ Correct - stable effect with fresh values
const onTick = useEffectEvent(() => {
logCurrentState(state); // Always has latest state
});
useEffect(() => {
const id = setInterval(onTick, 1000);
return () => clearInterval(id);
}, []); // Never re-creates intervalImportant Caveats
- React Compiler: Some optimizations (memo, useMemo hoisting) are handled automatically by React Compiler when enabled.
- Measure First: Apply optimizations based on measured bottlenecks, not assumptions.
- Context Matters: Pattern selection depends on specific use cases and trade-offs.
- Progressive Enhancement: Start with critical optimizations (Priority 1-2) before lower priorities.
Tools Referenced
- better-all: Dependency-based parallelization
- lru-cache: Cross-request caching
- SWR: Client-side data fetching with deduplication
- SVGO: SVG optimization
- Motion: Animation library for React
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

