-
Notifications
You must be signed in to change notification settings - Fork 0
Fix: useSidebar hook throws during SSR/prerendering #236
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -52,10 +52,22 @@ type SidebarContextProps = { | |
|
|
||
| const SidebarContext = React.createContext<SidebarContextProps | null>(null) | ||
|
|
||
| // Default sidebar state for when no provider exists (e.g., during SSR/prerendering) | ||
| const DEFAULT_SIDEBAR_STATE: SidebarContextProps = { | ||
| state: "expanded", | ||
| open: true, | ||
| setOpen: () => {}, | ||
| openMobile: false, | ||
| setOpenMobile: () => {}, | ||
| isMobile: false, | ||
| toggleSidebar: () => {}, | ||
| } | ||
|
|
||
| function useSidebar() { | ||
| const context = React.useContext(SidebarContext) | ||
| if (!context) { | ||
| throw new Error("useSidebar must be used within a SidebarProvider.") | ||
| // Return default values to allow components to gracefully degrade without breaking the build | ||
| return DEFAULT_SIDEBAR_STATE | ||
|
Comment on lines
66
to
+70
|
||
| } | ||
|
|
||
| return context | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment mentions "SSR/prerendering" but the actual issue is specifically about static site generation where components are rendered outside of a provider tree. Consider updating the comment to be more precise:
This clarifies that it's not just an SSR concern but a broader architectural pattern for components that may be rendered in isolation.