diff --git a/docs/guides/typescript-guide/using-react-context-to-avoid-prop-drilling.mdx b/docs/guides/typescript-guide/using-react-context-to-avoid-prop-drilling.mdx index 1bbb680..136f221 100644 --- a/docs/guides/typescript-guide/using-react-context-to-avoid-prop-drilling.mdx +++ b/docs/guides/typescript-guide/using-react-context-to-avoid-prop-drilling.mdx @@ -2,6 +2,8 @@ title: Using React Context to Avoid Prop-Drilling --- +import CircuitPreview from "@site/src/components/CircuitPreview" + React Context is a powerful tool for sharing data across deeply nested components without having to pass props through every level of the tree. When building TypeScript-powered circuit design tools, context lets you centralize configuration and provide well-typed data to any component that needs it. ## When to use React Context @@ -121,20 +123,6 @@ export const InstrumentPanel = () => ( Because every component inside the provider shares the same context, you can introduce additional consumers (for example, status displays or documentation overlays) without modifying intermediate components. -## Testing Context Consumers - -When unit testing components that depend on the context, render them with the provider to supply the necessary data. Libraries like [`@testing-library/react`](https://testing-library.com/docs/react-testing-library/intro/) make this pattern straightforward: - -```tsx -render( - - - -) -``` - -This keeps tests realistic while preserving the benefits of type safety. - ## Key Takeaways - Define a context value type that captures the shared configuration. @@ -143,3 +131,90 @@ This keeps tests realistic while preserving the benefits of type safety. - Wrap only the subtree that needs the shared data, keeping providers focused and intentional. With these patterns, React Context becomes a reliable way to manage shared state in your TypeScript tscircuit projects without the noise of prop drilling. + +(null) + +const BoardSettingsProvider = ({ + children, + value, +}: { + children: ReactNode + value: BoardSettings +}) => { + const memoizedValue = useMemo(() => value, [value]) + + return ( + + {children} + + ) +} + +const useBoardSettings = () => { + const context = useContext(BoardSettingsContext) + + if (!context) { + throw new Error("useBoardSettings must be used within a BoardSettingsProvider") + } + + return context +} + +const ResistorList = ({ names }: { names: string[] }) => { + const { + defaultFootprints: { resistor }, + } = useBoardSettings() + + return ( + + {names.map((name) => ( + + ))} + + ) +} + +const DecouplingCapacitor = ({ name }: { name: string }) => { + const { + defaultFootprints: { capacitor }, + } = useBoardSettings() + + return +} + +export default function InstrumentPanel() { + return ( + + + + + + + ) +} +`} +/>