Rendering and Hydration
Seed a Picked client from the server snapshot, render your slots, and hydrate. This page covers React and Svelte.
React: PickedProvider
PickedProvider gives a client to each component below it. Its props are a union of
two modes:
| Props | Mode | Description |
|---|---|---|
{ options, snapshot? } | Create | The provider calls createClient(options) and hydrates the client from the snapshot. |
{ client, snapshot? } | Adopt | The provider uses a client that is there already, such as a global one for the whole application. It can also seed that client from the snapshot. |
The provider seeds the client at once, so the server and the first paint on the
client give the same markup. In the browser the provider calls client.start() from
an effect. start() is idempotent, so it changes nothing on a client that already
started.
import { PickedProvider, ExperienceSlot } from '@picked/react'
<PickedProvider options={options} snapshot={snapshot}>
<ExperienceSlot name="page" />
</PickedProvider>Svelte: setPickedContext
setPickedContext does the same work in the Svelte style. Call it one time at the
top of the <script> block of a component. It takes the same two modes,
{ options, snapshot? } or { client, snapshot? }, and it returns the client.
<script lang="ts">
import { ExperienceSlot, setPickedContext } from '@picked/svelte'
setPickedContext({ options, snapshot })
</script>
<ExperienceSlot name="page" />Navigate in place between server-rendered pages
An application can change the page without a full page load. For example, a router
loads the next page and its snapshot, then renders the page in the same document. Use
hydrateFromSnapshot from @picked/client/server to apply the new snapshot to the
client that is live.
import { hydrateFromSnapshot, type PickedSnapshot } from '@picked/client/server'
function showPage(snapshot: PickedSnapshot) {
hydrateFromSnapshot(client, snapshot)
// Render the next page here.
}hydrateFromSnapshot is synchronous. It registers the content of the snapshot and
seeds the condition results. The slots then show the experiences that the snapshot
chooses. PickedProvider and setPickedContext use the same function for the first
snapshot.
The client records the versionId of the first snapshot. When a later snapshot has
the same versionId, the function does not register the project config again. The
registered experiences and their live instances stay. When a later snapshot has a
different versionId, or its versionId is null, the function registers the
project config of that snapshot.
Apply the snapshot before you render the next page. If you render first, the slots show the experiences of the previous page.
Render the page component
The SDK does not connect content to a component. You own the component that a Page
renders. Register it under the name that the Page uses. The Page Layout in the dashboard
points at that same name. The component reads the resolved entry data
through a content hook: useStepContent() in React, and getStepContent() in
Svelte.
import { useStepContent } from '@picked/react/hooks'
export function CMSPage() {
const content = useStepContent()
if (!content) {
return <p>Loading page content…</p>
}
const title = typeof content.title === 'string' ? content.title : ''
return (
<article>
<h1>{title}</h1>
</article>
)
}<script lang="ts">
import { getStepContent } from '@picked/svelte/hooks'
const content = getStepContent()
</script>
{#if $content}
<article>
<h1>{typeof $content.title === 'string' ? $content.title : ''}</h1>
</article>
{:else}
<p>Loading page content…</p>
{/if}useStepContent() returns the data object of the entry directly. It returns null
while no content is registered. getStepContent() returns a store, and you read it
with $content.
Related
- How to server-render a page — the full example.
- Slots and placements
