mirror of
https://github.com/supabase/supabase.git
synced 2026-10-11 04:15:04 +03:00
Update nextjs-server-components.mdx
Without default value, TS throw error about `createContext` should have `defaultValue` Following this blog, https://kentcdodds.com/blog/how-to-use-react-context-effectively#the-custom-consumer-hook; added `undefined` and checking if the provider is created or not.
This commit is contained in:
1 parent
1271356aa6
commit
e50df765d3
1 file changed
+8
-2
@@ -282,7 +282,7 @@ type SupabaseContext = {
|
||||
supabase: SupabaseClient<Database>
|
||||
}
|
||||
|
||||
const Context = createContext<SupabaseContext>()
|
||||
const Context = createContext<SupabaseContext | undefined>(undefined)
|
||||
|
||||
export default function SupabaseProvider({ children }: { children: React.ReactNode }) {
|
||||
const [supabase] = useState(() => createClient())
|
||||
@@ -294,7 +294,13 @@ export default function SupabaseProvider({ children }: { children: React.ReactNo
|
||||
)
|
||||
}
|
||||
|
||||
export const useSupabase = () => useContext(Context)
|
||||
export const useSupabase = () => {
|
||||
if (context === undefined) {
|
||||
throw new Error("useSupabase must be used inside SupabaseProvider");
|
||||
}
|
||||
|
||||
return useContext(Context)
|
||||
}
|
||||
```
|
||||
|
||||
> TypeScript types can be [generated with the Supabase CLI](https://supabase.com/docs/reference/javascript/typescript-support) and passed to `createBrowserSupabaseClient` to add type support to the Supabase client.
|
||||
|
||||
Reference in new issue
Block a user