Skip to main content

@idhub/identity-adapter-nextjs

Interfaces

IdentityProviderProps

Defined in: identity-adapter-nextjs/src/context.tsx:36

Properties

children?

optional children?: ReactNode

Defined in: identity-adapter-nextjs/src/context.tsx:68

optional cookie?: AnonymousIdCookieConfig

Defined in: identity-adapter-nextjs/src/context.tsx:64

Must match the settings passed to the middleware.

initialAnonymousId?

optional initialAnonymousId?: string | AnonymousId

Defined in: identity-adapter-nextjs/src/context.tsx:61

The anonymous id read on the SERVER (getAnonymousIdFromCookies in a Server Component) and passed down explicitly.

This is preferred over letting the client read document.cookie during the first render, and the reason is correctness rather than convenience: this component is a Client Component but Next.js still server-renders it. On the server there is no document, so a cookie-derived initial state would fall back to a freshly generated id, which would then disagree with the id the browser actually holds — a hydration mismatch on every cold load. Seeding from the server value makes the SSR pass and the hydration pass render the same id.

Omit it and the provider still works; it just renders an empty anonymous id until the post-hydration effect reads the real one from the cookie.

onWarning?

optional onWarning?: (code, message) => void

Defined in: identity-adapter-nextjs/src/context.tsx:66

Parameters
code

IdentityWarningCode

message

string

Returns

void

providers

providers: IdentityProvider[]

Defined in: identity-adapter-nextjs/src/context.tsx:43

Vendor provider instances (@idhub/identity-provider-*). Read once, when the client is constructed — later changes to this array are ignored on purpose, because re-creating the client mid-session would re-fire onAnonymous on every vendor and split their sessions.


UseIdentityResult

Defined in: identity-adapter-nextjs/src/context.tsx:26

What useIdentity hands back to client components.

Properties

identify

identify: (userId, traits?) => void

Defined in: identity-adapter-nextjs/src/context.tsx:28

Parameters
userId

string

traits?

Traits

Returns

void

page

page: (name?, properties?) => void

Defined in: identity-adapter-nextjs/src/context.tsx:31

Parameters
name?

string

properties?

Traits

Returns

void

reset

reset: () => void

Defined in: identity-adapter-nextjs/src/context.tsx:29

Returns

void

state

state: IdentityState

Defined in: identity-adapter-nextjs/src/context.tsx:27

track

track: (event, properties?) => void

Defined in: identity-adapter-nextjs/src/context.tsx:30

Parameters
event

string

properties?

Traits

Returns

void

Functions

IdentityProvider()

IdentityProvider(__namedParameters): Element

Defined in: identity-adapter-nextjs/src/context.tsx:91

App Router compatible: this is a Client Component, so a Server Component parent (typically app/layout.tsx) can render it and pass children straight through — the children stay Server Components.

// app/layout.tsx — a Server Component
export default async function RootLayout({ children }) {
const anonymousId = getAnonymousIdFromCookies(await cookies())
return (
<html><body>
<IdentityProvider providers={[segment(), launchDarkly()]}
initialAnonymousId={anonymousId}>
{children}
</IdentityProvider>
</body></html>
)
}

Parameters

__namedParameters

IdentityProviderProps

Returns

Element


useIdentity()

useIdentity(): UseIdentityResult

Defined in: identity-adapter-nextjs/src/context.tsx:167

{ state, identify, reset, track, page } for client components. Throws rather than returning a silent no-op object: a missing provider means every vendor call is being dropped on the floor, which is exactly the class of bug this package exists to remove.

Returns

UseIdentityResult