react-core

React Core Best Practices

Safety Notice

This listing is imported from skills.sh public index metadata. Review upstream SKILL.md and repository scripts before running.

Copy this and send it to your AI assistant to learn

Install skill "react-core" with this command: npx skills add alicoder001/agent-skills/alicoder001-agent-skills-react-core

React Core Best Practices

Comprehensive React development guidelines with Vercel optimization patterns.

Instructions

  1. Component Structure

// ✅ Max 200 lines per component // ✅ Single responsibility

interface Props { user: User; onUpdate: (user: User) => void; }

export function UserCard({ user, onUpdate }: Props) { // Hooks first const [isEditing, setIsEditing] = useState(false);

// Derived state (no useState needed) const fullName = ${user.firstName} ${user.lastName};

// Event handlers const handleSubmit = () => { ... };

// Render return ( <div className="user-card"> <h2>{fullName}</h2> ... </div> ); }

  1. Derived State (No useState)

// ❌ Bad - unnecessary state const [fullName, setFullName] = useState(''); useEffect(() => { setFullName(${firstName} ${lastName}); }, [firstName, lastName]);

// ✅ Good - derived state const fullName = ${firstName} ${lastName};

// ✅ Good - expensive computation const sortedItems = useMemo( () => items.sort((a, b) => a.name.localeCompare(b.name)), [items] );

  1. Functional setState

// ❌ Bad - stale state risk setCount(count + 1);

// ✅ Good - always current setCount(prev => prev + 1);

// ✅ Good - object state setUser(prev => ({ ...prev, name: newName }));

  1. Prevent Unnecessary Re-renders

// ✅ Primitive dependencies for useMemo const result = useMemo(() => { return expensiveCalculation(id); }, [id]); // id is primitive

// ✅ Stable callback references const handleClick = useCallback(() => { doSomething(id); }, [id]);

// ✅ Memoize child components const MemoizedChild = memo(ChildComponent);

  1. Event Handlers

// ❌ Bad - creates new function each render <button onClick={() => handleClick(item.id)}>

// ✅ Good - stable reference with data attribute <button data-id={item.id} onClick={handleClick}>

function handleClick(e: React.MouseEvent<HTMLButtonElement>) { const id = e.currentTarget.dataset.id; // ... }

  1. Custom Hooks

// ✅ Extract reusable logic function useLocalStorage<T>(key: string, initialValue: T) { const [value, setValue] = useState<T>(() => { const stored = localStorage.getItem(key); return stored ? JSON.parse(stored) : initialValue; });

useEffect(() => { localStorage.setItem(key, JSON.stringify(value)); }, [key, value]);

return [value, setValue] as const; }

  1. Props Pattern

// ✅ Destructure props function Button({ children, variant = 'primary', ...rest }: ButtonProps) { return ( <button className={btn-${variant}} {...rest}> {children} </button> ); }

  1. Loading & Error States

function UserProfile({ userId }: Props) { const { data, isLoading, error } = useUser(userId);

if (isLoading) return <Skeleton />; if (error) return <ErrorMessage error={error} />; if (!data) return <NotFound />;

return <UserCard user={data} />; }

References

  • React Documentation

  • React TypeScript Cheatsheet

Source Transparency

This detail page is rendered from real SKILL.md content. Trust labels are metadata-based hints, not a safety guarantee.

Related Skills

Related by shared tags or category signals.

Coding

solid

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

reasoning

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

collaboration

No summary provided by upstream source.

Repository SourceNeeds Review
Coding

find-skills

No summary provided by upstream source.

Repository SourceNeeds Review