Course contentsShow
← React
Lesson 256 of 2,3968. Props PatternsFree lesson

Children Prop Anti-patterns

Common mistakes: mutating children directly, assuming children structure, and overusing cloneElement for state sharing.

Children Prop Anti-patterns

You'll learn the common mistakes developers make when working with the children prop—and how to avoid them.

What Not to Do with Children

Now that you've learned powerful tools like React.Children.map() and cloneElement(), it's tempting to overuse them. However, several patterns are considered anti-patterns because they make components fragile, hard to maintain, or unpredictable.

Common Anti-patterns

1. Mutating Children Directly

Children props are immutable—never try to modify them directly:

// ❌ BAD: Trying to mutate children
function Wrapper({ children }) {
  children.props.className = 'new-class'; // Error!
  return <div>{children}</div>;
}

2. Assuming Children Structure

Don't assume children will always be a specific type or structure:

// ❌ BAD: Assumes children is always an array
function List({ children }) {
  return <ul>{children[0]}</ul>; // Breaks with single child!
}

Use React.Children.toArray() if you need array operations.

3. Overusing cloneElement for State Sharing

cloneElement() should be rare. If you're using it to inject state into many children, you probably need a better pattern (like Context, which you'll learn later):

// ❌ BAD: Cloning every child to pass state
function Parent({ children }) {
  const [state, setState] = useState(0);
  return React.Children.map(children, child =>
    React.cloneElement(child, { sharedState: state }) // Fragile!
  );
}

This assumes all children accept sharedState props and breaks easily when children change.

Better Approaches

Instead of these patterns, prefer explicit props, render props, or composition patterns that make data flow obvious and type-safe.

Key Takeaway: Avoid mutating children, assuming their structure, or over-relying on cloneElement() for state—these create brittle, unpredictable components.