Interview question
What is compound component pattern? Compound component pattern क्या है?
Answer
// Compound components - share state between related components
const Tab = ({ children }) => {
const [activeTab, setActiveTab] = React.useState(0);
return React.Children.map(children, (child) =>
React.cloneElement(child, { activeTab, setActiveTab })
);
};
Tab.Header = ({ children, activeTab, setActiveTab }) => {
return <div>{children}</div>;
};
Tab.Body = ({ children, activeTab, setActiveTab }) => {
return <div>{children}</div>;
};Compound components - related components को साथ काम करने के लिए।Was this answer clear?