Skip to content

Commit 49e448f

Browse files
committed
docs(create-context): document default value / optional injection pattern
1 parent f52c278 commit 49e448f

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

apps/docs/src/pages/composables/foundation/create-context.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@ providePanel('panel-sidebar', sidebarContext)
7272
const panel = usePanel('panel-main')
7373
```
7474

75+
### Default Values
76+
77+
By default, `useContext` throws if the context isn't found. Pass a `defaultValue` to make injection optional — useful for components that work with or without a parent:
78+
79+
```ts
80+
// Static key — default passed at creation time
81+
const [usePanel, providePanel] = createContext<PanelContext>('v0:panel', fallbackContext)
82+
const panel = usePanel() // returns fallbackContext if not provided
83+
84+
// Dynamic key — default passed at call time
85+
const [usePanel, providePanel] = createContext<PanelContext>()
86+
const panel = usePanel('v0:panel', fallbackContext)
87+
```
88+
7589
### Suffix Pattern
7690

7791
For parent-child context hierarchies where a child needs to know which parent it belongs to:

0 commit comments

Comments
 (0)