Props best practices — rules that make code cleaner, safer, and easier to understand.
Why it matters: Props are the primary way data flows in React. Clean prop patterns reduce bugs, make components reusable, and serve as documentation. A component with well-designed props is self-documenting — you know exactly what it needs without reading the implementation.
1. Destructuring:
1// Bad2function UserCard(props) {3 return <div>{props.name}</div>;4}56// Good7function UserCard({ name, age, avatar }) {8 return <div>{name}</div>;9}
2. Default values:
1// Destructuring with defaults (preferred)2function Button({ variant = "primary", size = "md", children }) {3 return <button className={`btn btn-${variant} btn-${size}`}>{children}</button>;4}56// defaultProps — outdated method7Button.defaultProps = { variant: "primary" };
3. TypeScript interface:
1interface UserCardProps {2 name: string;3 age: number;4 avatar?: string; // Optional5 onEdit?: (id: number) => void; // Optional callback6}78function UserCard({ name, age, avatar, onEdit }: UserCardProps) { /* ... */ }
4. Don't pass props through multiple levels (prop drilling):
1// Bad: passing through 5 levels2function App() {3 return <Layout theme={theme} />;4}5function Layout({ theme }) { return <Sidebar theme={theme} />; }6function Sidebar({ theme }) { return <Menu theme={theme} />; }78// Good: Context9const ThemeContext = createContext("light");10function Menu() { const theme = useContext(ThemeContext); /* ... */ }
5. Spread props consciously:
1// Good: for passing props to DOM element2function Button({ children, ...props }) {3 return <button {...props}>{children}</button>;4}56// Bad: loses typing and control7function UserCard({ ...props }) { return <div {...props} />; }
6. Avoid passing new objects/arrays as props:
1// Bad: creates new reference every render2<UserCard style={{ color: "red" }} />34// Good: stable reference5const style = useMemo(() => ({ color: "red" }), []);6<UserCard style={style} />
7. Use children as a prop for composition:
1function Card({ title, children }) {2 return (3 <div className="card">4 <h2>{title}</h2>5 <div className="card-body">{children}</div>6 </div>7 );8}910<Card title="Profile">11 <UserInfo user={user} />12</Card>