React Interview Questions
React.js
FrontendWeb DevelopmentQuestion 18
How do you pass data between sibling components in React?
Answer:
In React, sibling components do not have a direct way to communicate with each other. However, you can pass data between sibling components by lifting the shared state up to their common parent component and then passing the necessary data down to the sibling components as props. Here's how you can achieve this:
Step-by-Step Guide
- Lift State Up: Move the shared state to the parent component.
- Pass State and Callbacks: Pass the state and callback functions from the parent to the child components as props.
- Update State: Use the callback functions in the child components to update the state in the parent component.
Example
Let's consider two sibling components, SiblingA and SiblingB, that need to share data.
Step 1: Create the Parent Component
First, create a parent component that will hold the shared state.
import React, { useState } from 'react';
import SiblingA from './SiblingA';
import SiblingB from './SiblingB';
function ParentComponent() {
const [sharedData, setSharedData] = useState('');
const updateSharedData = (data) => {
setSharedData(data);
};
return (
<div>
<SiblingA sharedData={sharedData} updateSharedData={updateSharedData} />
<SiblingB sharedData={sharedData} updateSharedData={updateSharedData} />
</div>
);
}
export default ParentComponent;
Step 2: Create Sibling Components
Create the sibling components that will receive the shared state and the update function as props.
SiblingA Component
import React from 'react';
function SiblingA({ sharedData, updateSharedData }) {
const handleChange = (event) => {
updateSharedData(event.target.value);
};
return (
<div>
<h2>Sibling A</h2>
<input type="text" value={sharedData} onChange={handleChange} />
</div>
);
}
export default SiblingA;
SiblingB Component
import React from 'react';
function SiblingB({ sharedData }) {
return (
<div>
<h2>Sibling B</h2>
<p>Shared Data: {sharedData}</p>
</div>
);
}
export default SiblingB;
Explanation
-
ParentComponent:
- Manages the shared state
sharedDataand provides a functionupdateSharedDatato update this state. - Passes
sharedDataandupdateSharedDataas props to bothSiblingAandSiblingB.
- Manages the shared state
-
SiblingA:
- Receives
sharedDataandupdateSharedDataas props. - Updates the shared state in the parent component through the
updateSharedDatafunction when the input value changes.
- Receives
-
SiblingB:
- Receives
sharedDataas a prop and displays it.
- Receives
Benefits of This Approach
- Single Source of Truth: The state is managed in a single place (the parent component), which makes it easier to track and debug.
- Data Synchronization: The data is synchronized between sibling components because they share the same state from the parent.
- Scalability: This pattern is scalable and can be applied to more complex component structures.
By lifting the state up to the parent component and passing it down as props, sibling components can effectively share and update data without direct communication. This approach leverages React's unidirectional data flow, ensuring that state management remains predictable and maintainable.