Client-side context
Developer preview
Developer Preview
This page documents the developer preview of the SDK, version 20.81.1-alpha.25.sha-f076f9b857.
Most Uniform components render on the server and do not use JavaScript in the browser. These tasks use the browser Context:
- Read or update the quirks of the visitor
- Read the scores of the visitor
- Update the page when the Context changes
warning
Put "use client" at the top of the file of a component that uses useUniformContext, useQuirks, useScores or a browser API. This is a rule of React Server Components.
Keep client components small, and put them low in the component tree. "use client" makes the component and all its children client components. Then more JavaScript goes to the browser. For example, put only the button that updates a quirk in a client component, not the full page.
useUniformContext#
useUniformContext gives the Uniform Context instance in the browser:
note
context is undefined until the browser Context is ready. Always check for undefined.
useQuirks#
useQuirks returns the quirks of the visitor. The component renders again when the quirks change:
useScores#
useScores returns the scores of the visitor. The component renders again when the scores change:
Custom client context#
A custom client context lets you add Context plugins, for example analytics, or change the dev tools. Install @uniformdev/context, then make the component:
components/CustomUniformClientContext.tsx
Give it to UniformComposition (and to UniformPlayground, if you want it there too):
useInitUniformContext makes the Context one time in the browser. On each client-side navigation, it updates the Context with the new URL and the quirks from the middleware.