🎯

react-key-prop

🎯Skill

from flpbalada/my-opencode-config

VibeIndex|
What it does

Guides developers in using React's key prop correctly by providing best practices for generating stable, unique identifiers when rendering dynamic lists.

πŸ“¦

Part of

flpbalada/my-opencode-config(40 items)

react-key-prop

Installation

πŸ“‹ No install commands found in docs. Showing default command. Check GitHub for actual instructions.
Quick InstallInstall with npx
npx skills add flpbalada/my-opencode-config --skill react-key-prop
1Installs
-
AddedFeb 4, 2026

Skill Details

SKILL.md

Guides proper usage of the key prop in React lists. Use this skill when rendering lists, mapping arrays to components, or troubleshooting list-related state bugs.

Overview

# React: Key Prop Best Practices

Core Principle

Use stable, unique IDs from your data. Never use array index for dynamic lists.

The key prop provides stable identity to list elements during React's reconciliation process.

When to Use What

Use Data IDs (Preferred)

Always use unique, stable identifiers directly from your data:

```jsx

// βœ… Correct

{todos.map((todo) => (

  • {todo.text}
  • ))}

    ```

    Ideal keys are:

    • Unique - No two items share the same key
    • Stable - Never changes during component lifetime
    • Predictable - Directly tied to the data item

    Generate IDs on Data Load

    When data lacks IDs, create them once when receiving data:

    ```jsx

    import { nanoid } from 'nanoid';

    useEffect(() => {

    fetch('/api/items')

    .then(res => res.json())

    .then(data => {

    const itemsWithIds = data.map(item => ({

    ...item,

    _tempId: nanoid() // Stable ID generated once

    }));

    setItems(itemsWithIds);

    });

    }, []);

    ```

    When Index Is Acceptable (Rare)

    Index as key is acceptable ONLY when ALL conditions are met:

    • List is absolutely static
    • Items never added/removed (except at the end)
    • Order never changes
    • Items have no internal state

    Anti-Patterns to Avoid

    Never Generate Keys During Render

    ```jsx

    // ❌ WRONG: Creates new key every render

    {items.map(item => (

  • {item.name}
  • ))}

    ```

    This forces React to destroy and recreate all components on every render.

    Don't Use Index for Dynamic Lists

    ```jsx

    // ❌ WRONG for dynamic lists

    {items.map((item, index) => (

  • {item.name}
  • ))}

    ```

    Index fails when:

    • Items are added/removed from beginning or middle
    • List order changes (sorting, filtering)
    • Items have internal state (like form inputs)

    The bug: Index represents position, not data identity. When positions change but indexes stay the same, React incorrectly "mutates" existing components instead of creating new ones, causing state mismatch.

    Don't Use `useId()` for List Keys

    React's useId() hook is for accessibility (linking labels to inputs), not for generating list keys.

    Quick Reference

    DO

    • Always use key when rendering lists
    • Prefer unique, stable id from your data
    • Generate IDs once at data load time (nanoid/uuid)

    DON'T

    • Never generate key during render (Math.random(), Date.now())
    • Avoid index as key for dynamic lists
    • Don't use useId() for list keys

    References

    • [React Docs - Rendering Lists](https://react.dev/learn/rendering-lists#keeping-list-items-in-order-with-key)
    • [nanoid - Tiny ID generator](https://github.com/ai-cookie/nanoid)

    More from this repository10

    🎯
    five-whys🎯Skill

    Systematically uncovers root causes of problems through iterative questioning, revealing underlying issues beyond surface-level symptoms.

    🎯
    social-proof-psychology🎯Skill

    Optimizes user trust and conversion by strategically displaying social validation through testimonials, user stats, and expert endorsements.

    🎯
    cognitive-fluency-psychology🎯Skill

    Enhances user comprehension and engagement by applying cognitive fluency principles to simplify information processing across content and interfaces.

    🎯
    hooked-model🎯Skill

    Designs habit-forming products by mapping user triggers, actions, rewards, and investments to create engaging, addictive product experiences.

    🎯
    cognitive-biases🎯Skill

    Applies cognitive bias insights to optimize product design, user experiences, and decision-making strategies by leveraging psychological principles.

    🎯
    typescript-satisfies-operator🎯Skill

    Validates TypeScript object types while preserving precise literal types, preventing type widening and catching type-related errors early.

    🎯
    typescript-best-practices🎯Skill

    Enforces TypeScript best practices and coding standards through comprehensive linting and configuration rules for consistent, high-quality code.

    🎯
    status-quo-bias🎯Skill

    Helps design product changes and migrations by understanding users' psychological resistance to change and creating strategies to overcome status quo bias.

    🎯
    kanban🎯Skill

    Visualize and optimize team workflow by creating Kanban boards that track tasks, limit work-in-progress, and improve delivery efficiency.

    🎯
    theme-epic-story🎯Skill

    I apologize, but I cannot generate a description without seeing the actual content or context of the "theme-epic-story" skill from the repository. Could you provide more details about what this spe...