typescriptadvanced
Compound Component Pattern
Build flexible compound components using React Context for implicit parent-child communication.
typescriptPress ⌘/Ctrl + Shift + C to copy
import { createContext, useContext, useState, ReactNode } from 'react';
interface AccordionContext {
openId: string | null;
toggle: (id: string) => void;
}
const Ctx = createContext<AccordionContext | null>(null);
const useAccordion = () => {
const ctx = useContext(Ctx);
if (!ctx) throw new Error('Must be used within Accordion');
return ctx;
};
export function Accordion({ children }: { children: ReactNode }) {
const [openId, setOpenId] = useState<string | null>(null);
const toggle = (id: string) => setOpenId((prev) => (prev === id ? null : id));
return <Ctx.Provider value={{ openId, toggle }}>{children}</Ctx.Provider>;
}
Accordion.Item = function Item({ id, title, children }: { id: string; title: string; children: ReactNode }) {
const { openId, toggle } = useAccordion();
return (
<div>
<button onClick={() => toggle(id)}>{title}</button>
{openId === id && <div>{children}</div>}
</div>
);
};Use Cases
- Accordion components
- Tab interfaces
- Dropdown menus
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptintermediate
Render Prop Pattern
Share stateful logic between components using the render prop pattern for maximum flexibility.
Best for: Mouse tracking
#patterns#render-prop
typescriptadvanced
React Compound Component Pattern
Build flexible compound components using React context for shared state between related parts.
Best for: Building flexible component libraries
#react#patterns
typescriptadvanced
Context Selector Pattern
Avoid unnecessary re-renders with a context selector pattern that subscribes to specific state slices.
Best for: High-performance global state
#react#context
typescriptadvanced
Context Provider Factory
Utility function that creates a typed React Context with a Provider and a custom hook in one call.
Best for: Creating typed contexts
#context#provider