You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
123
125
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
-
<BoardSettingsProvidervalue={mockSettings}>
131
-
<ResistorListnames={["R10"]} />
132
-
</BoardSettingsProvider>
133
-
)
134
-
```
135
-
136
-
This keeps tests realistic while preserving the benefits of type safety.
137
-
138
126
## Key Takeaways
139
127
140
128
- 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.
143
131
- Wrap only the subtree that needs the shared data, keeping providers focused and intentional.
144
132
145
133
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"
0 commit comments