diff --git a/packages/preact/src/hydration.spec.tsx b/packages/preact/src/hydration.spec.tsx
index 8380126c..646390f4 100644
--- a/packages/preact/src/hydration.spec.tsx
+++ b/packages/preact/src/hydration.spec.tsx
@@ -8,11 +8,13 @@ import {
act
} from '@testing-library/preact'
import {
+ type InjectionContext,
signal,
hydratable
} from '@nano_kit/store'
import {
InjectionContextProvider,
+ useInjectionContext,
useInject,
useSignal
} from './core.js'
@@ -36,6 +38,12 @@ function Test() {
)
}
+function Context({ contexts }: { contexts: (InjectionContext | undefined)[] }) {
+ contexts.push(useInjectionContext())
+
+ return null
+}
+
describe('preact', () => {
describe('hydration', () => {
describe('HydrationProvider', () => {
@@ -86,6 +94,40 @@ describe('preact', () => {
expect(container.innerHTML).toBe('
empty
')
})
+
+ it('should reuse the parent hydration context by default', () => {
+ const contexts: (InjectionContext | undefined)[] = []
+
+ render(
+
+
+
+
+
+
+
+
+ )
+
+ expect(contexts[1]).toBe(contexts[0])
+ })
+
+ it('should create a child context when reuse is disabled', () => {
+ const contexts: (InjectionContext | undefined)[] = []
+
+ render(
+
+
+
+
+
+
+
+
+ )
+
+ expect(contexts[1]).not.toBe(contexts[0])
+ })
})
describe('StaticHydrationProvider', () => {
diff --git a/packages/preact/src/hydration.tsx b/packages/preact/src/hydration.tsx
index 39c4751d..52f743b8 100644
--- a/packages/preact/src/hydration.tsx
+++ b/packages/preact/src/hydration.tsx
@@ -25,6 +25,7 @@ export interface HydrationProviderProps extends InjectionContextProps {
context?: InjectionProvider[]
/**
* Whether to reuse an existing InjectionContext or create a new one.
+ * `true` by default.
*/
reuse?: boolean
}
@@ -36,7 +37,7 @@ export interface HydrationProviderProps extends InjectionContextProps {
export function HydrationProvider({
dehydrated,
context = [],
- reuse,
+ reuse = true,
children
}: HydrationProviderProps) {
const currentContext = useInjectionContext()
diff --git a/website/src/content/docs/store-integrations/preact.mdx b/website/src/content/docs/store-integrations/preact.mdx
index 7ae0837b..6b38e75c 100644
--- a/website/src/content/docs/store-integrations/preact.mdx
+++ b/website/src/content/docs/store-integrations/preact.mdx
@@ -169,7 +169,7 @@ function App({ dehydrated }) {
Props:
- `dehydrated?` — dehydrated key-value pairs `[string, unknown][]`, or a falsy value to skip hydration
- `context?` — additional injection providers
-- `reuse?` — reuse an existing `InjectionContext` instead of creating a new one; set to `true` if you want the hydration context to be shared with the rest of your app instead of isolated
+- `reuse?` — reuse an existing `InjectionContext` instead of creating a new one; `true` by default
#### `StaticHydrationProvider`