All skills
siberiacancode avatar

/react-best-practices

@53c8ff5

React and Next.js performance optimization guidelines from Vercel Engineering. This skill should be used when writing, reviewing, or refactoring React/Next.js code to ensure optimal performance patterns. Triggers on tasks involving React components, Next.js pages, data fetching, bundle optimization, or performance improvements.

  • 77 files
  • 225.1 KB
  • MIT
  • Updated 4 months ago
  • GitHub

Use this Skill: https://skilld.dev/gh/siberiacancode/agent-skills/react-best-practices

This session only. Nothing lands on disk.

rulesrerender-no-inline-components.md

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

Don't Define Components Inside Components

Impact: HIGH (prevents remount on every render)

Defining a component inside another component creates a new component type on every render. React sees a different component each time and fully remounts it, destroying all state and DOM.

A common reason developers do this is to access parent variables without passing props. Always pass props instead.

Incorrect (remounts on every render):

function UserProfile({ user, theme }) {
  // Defined inside to access `theme` - BAD
  const Avatar = () => (
    <img
      src={user.avatarUrl}
      className={theme === 'dark' ? 'avatar-dark' : 'avatar-light'}
    />
  )

  // Defined inside to access `user` - BAD
  const Stats = () => (
    <div>
      <span>{user.followers} followers</span>
      <span>{user.posts} posts</span>
    </div>
  )

  return (
    <div>
      <Avatar />
      <Stats />
    </div>
  )
}

Every time UserProfile renders, Avatar and Stats are new component types. React unmounts the old instances and mounts new ones, losing any internal state, running effects again, and recreating DOM nodes.

Correct (pass props instead):

function Avatar({ src, theme }: { src: string; theme: string }) {
  return (
    <img
      src={src}
      className={theme === 'dark' ? 'avatar-dark' : 'avatar-light'}
    />
  )
}

function Stats({ followers, posts }: { followers: number; posts: number }) {
  return (
    <div>
      <span>{followers} followers</span>
      <span>{posts} posts</span>
    </div>
  )
}

function UserProfile({ user, theme }) {
  return (
    <div>
      <Avatar src={user.avatarUrl} theme={theme} />
      <Stats followers={user.followers} posts={user.posts} />
    </div>
  )
}

Symptoms of this bug:

  • Input fields lose focus on every keystroke
  • Animations restart unexpectedly
  • useEffect cleanup/setup runs on every parent render
  • Scroll position resets inside the component

Source: SKILL.md on GitHub

No third-party reports yet.

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

Last checked against GitHub 2 weeks ago.

Activeupdated 4 months ago
metadata
{
  "author": "vercel",
  "version": "1.0.0"
}

README badge

README badge for siberiacancode/agent-skills/react-best-practices