Facebook Pixel

How to Pass Data Between React Components Using Props

Passing data between components is a daily React task. Here is how to do it cleanly, including parent-to-child and child-to-parent.

How to Pass Data Between React Components Using Props

Moving data between components is one of the most common things you do in React. Here is how to do it in every direction.

Parent to Child

The simplest case. The parent passes data as a prop, and the child reads it. This is the default data flow in React.

Child to Parent

Children cannot directly change parent state. Instead, the parent passes a callback function as a prop, and the child calls it with the new data. The parent updates its state, and the new data flows back down as props.

Sibling to Sibling

Siblings do not talk directly. They share data through their common parent. One sibling sends data up to the parent via a callback, and the parent passes it down to the other sibling as a prop.

Passing Complex Data

You can pass objects, arrays, and functions as props. For deeply nested trees, passing props through many levels, called prop drilling, becomes painful, and that is when Context API helps.

Keep Props Focused

Pass only the data a component needs. Passing an entire user object when a component only needs the name creates unnecessary coupling and re-renders.

Destructure for Readability

Destructure props in the function signature so it is clear what the component expects. This also documents the component's interface.

The Takeaway

Data flows down through props, and events flow up through callbacks. For sibling communication, lift state to the common parent. When prop drilling gets deep, reach for Context.

Pass the data as a prop on the child component in the parent's JSX, and read it from the props object inside the child. This is the default downward data flow in React.

Children cannot change parent state directly. The parent passes a callback function as a prop, and the child calls it with the new data. The parent updates its state, and the new data flows back down as props.

Siblings do not communicate directly. They share data through their common parent. One sibling sends data up via a callback, the parent stores it in state, and passes it down to the other sibling as a prop.

Prop drilling is passing props through many intermediate components to reach a deeply nested child. It becomes painful at scale, which is when the Context API is used to provide data without manual threading.

Pass only what a component needs. Passing an entire user object when a component only needs the name creates unnecessary coupling and can cause extra re-renders. Keep props focused.

Ready to master React completely?

Want to upskill yourself, crack your next interview, and get your dream job? Join our comprehensive course to dive deeper with high-quality video tutorials, solve interview questions, and a premium community.

Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.
Please Login.