Skip to content

Commit ae79460

Browse files
authored
Add CircuitPreview example to React context guide (#273)
* Add CircuitPreview example to context guide * Fix CircuitPreview example export * Import CircuitPreview in React context guide
1 parent 241176a commit ae79460

File tree

1 file changed

+89
-14
lines changed

1 file changed

+89
-14
lines changed

docs/guides/typescript-guide/using-react-context-to-avoid-prop-drilling.mdx

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
title: Using React Context to Avoid Prop-Drilling
33
---
44

5+
import CircuitPreview from "@site/src/components/CircuitPreview"
6+
57
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.
68

79
## When to use React Context
@@ -121,20 +123,6 @@ export const InstrumentPanel = () => (
121123

122124
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.
123125

124-
## Testing Context Consumers
125-
126-
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:
127-
128-
```tsx
129-
render(
130-
<BoardSettingsProvider value={mockSettings}>
131-
<ResistorList names={["R10"]} />
132-
</BoardSettingsProvider>
133-
)
134-
```
135-
136-
This keeps tests realistic while preserving the benefits of type safety.
137-
138126
## Key Takeaways
139127

140128
- 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.
143131
- Wrap only the subtree that needs the shared data, keeping providers focused and intentional.
144132

145133
With these patterns, React Context becomes a reliable way to manage shared state in your TypeScript tscircuit projects without the noise of prop drilling.
134+
135+
<CircuitPreview
136+
hide3DTab
137+
hidePCBTab
138+
defaultView="schematic"
139+
code={`import { ReactNode, createContext, useContext, useMemo } from "react"
140+
141+
type BoardSettings = {
142+
boardName: string
143+
boardSize: { width: number; height: number }
144+
defaultFootprints: {
145+
resistor: string
146+
capacitor: string
147+
}
148+
}
149+
150+
const BoardSettingsContext = createContext<BoardSettings | null>(null)
151+
152+
const BoardSettingsProvider = ({
153+
children,
154+
value,
155+
}: {
156+
children: ReactNode
157+
value: BoardSettings
158+
}) => {
159+
const memoizedValue = useMemo(() => value, [value])
160+
161+
return (
162+
<BoardSettingsContext.Provider value={memoizedValue}>
163+
{children}
164+
</BoardSettingsContext.Provider>
165+
)
166+
}
167+
168+
const useBoardSettings = () => {
169+
const context = useContext(BoardSettingsContext)
170+
171+
if (!context) {
172+
throw new Error("useBoardSettings must be used within a BoardSettingsProvider")
173+
}
174+
175+
return context
176+
}
177+
178+
const ResistorList = ({ names }: { names: string[] }) => {
179+
const {
180+
defaultFootprints: { resistor },
181+
} = useBoardSettings()
182+
183+
return (
184+
<group name="Resistors">
185+
{names.map((name) => (
186+
<resistor key={name} name={name} footprint={resistor} resistance="1k" />
187+
))}
188+
</group>
189+
)
190+
}
191+
192+
const DecouplingCapacitor = ({ name }: { name: string }) => {
193+
const {
194+
defaultFootprints: { capacitor },
195+
} = useBoardSettings()
196+
197+
return <capacitor name={name} capacitance="10n" footprint={capacitor} />
198+
}
199+
200+
export default function InstrumentPanel() {
201+
return (
202+
<BoardSettingsProvider
203+
value={{
204+
boardName: "Instrumentation Panel",
205+
boardSize: { width: 50, height: 40 },
206+
defaultFootprints: {
207+
resistor: "0402",
208+
capacitor: "0603",
209+
},
210+
}}
211+
>
212+
<board width="50mm" height="40mm" name="InstrumentPanel">
213+
<ResistorList names={["R1", "R2", "R3"]} />
214+
<DecouplingCapacitor name="C1" />
215+
</board>
216+
</BoardSettingsProvider>
217+
)
218+
}
219+
`}
220+
/>

0 commit comments

Comments
 (0)