next-federation
API Reference

@next-federation/react

Host-side React components: IslandMount hydrates a remote island, FederationErrorBoundary contains failures, and createRemoteComponent renders a remote's...

Host-side React components: IslandMount hydrates a remote island, FederationErrorBoundary contains failures, and createRemoteComponent renders a remote's client component. Peers: react and react-dom ^18.3.0 || ^19.0.0.

Install

npm install @next-federation/react react react-dom

Usage

import { FederationErrorBoundary } from '@next-federation/react'
;<FederationErrorBoundary fallback={(error) => <p>Widget unavailable</p>} onError={(error) => report(error)} resetKey={route}>
  <Island />
</FederationErrorBoundary>

Entry points

EntryExports
@next-federation/reactFederationErrorBoundary, IslandMount, getLiveIslandCount, ISLAND_EVENT, clearIslandTokens, createTokenClient
@next-federation/react/componentscreateRemoteComponent, loadRemoteModule, preloadRemoteComponent, fetchDescriptorInBrowser, registerSharedModules, provideNavigation, COMPONENT_EVENT
@next-federation/react/shareprovideHostReact, isHostReactProvided

API

FederationErrorBoundary

An error boundary for islands. It is 'use client', so Server Components can import it.

PropTypeDefaultDescription
fallbackReactNode or (error: unknown) => ReactNoderequiredRendered after an error.
onError(error: unknown) => voidnoneCalled with the error.
resetKeyunknownnoneRenders children again when it changes.
childrenReactNoderequiredThe content to protect.

IslandMount

Hydrates a remote island envelope and keeps its root alive across re-renders and StrictMode. data-nf-state becomes live after hydration.

PropTypeDefaultDescription
envelopeIslandEnveloperequiredFrom fetchIsland. Its HTML is rendered as is.
bridgePartial<IslandBridge>noneOverrides for the host navigation bridge.
token{ endpoint: string; identityKey: string | null } | nullnoneToken endpoint and identity for per-user islands.
errorFallbackReactNodenoneShown after an island error or when its code cannot load (NF-011).
inertOnLoadFailurebooleantrue without errorFallback, else falseOn bootstrap failure, keep the server HTML, marked inert.
onEvent(event: IslandClientEvent) => voidnoneCalled for island:hydrate, island:action and island:error.

getLiveIslandCount()

Returns the island roots mounted on the page.

ISLAND_EVENT

'nf:island', the bubbling DOM event each island dispatches. Detail: IslandClientEvent.

clearIslandTokens()

Forgets cached island tokens. Call it on sign-out.

createTokenClient(options)

Creates the browser TokenClient that IslandMount uses. options: endpoint, identityKey, and optional fetch and now for tests.

createRemoteComponent(options)

Binds an exposed client component of a components remote, in a 'use client' module. The result has a preload(descriptor) method.

'use client'
import { createRemoteComponent } from '@next-federation/react/components'

export const Cart = createRemoteComponent<{ items: number }>({
  expose: './Cart',
  fallback: <p>Loading the cart…</p>,
  errorFallback: <p>Cart unavailable</p>,
})
// <Cart descriptor={descriptor} items={3} />
OptionTypeDefaultDescription
exposestringrequiredExposed module, such as './Cart'. Its default export renders.
fallbackReactNodenoneShown while loading, and on failure without errorFallback.
errorFallbackReactNodefallbackShown when the remote fails to load or render (NF-040).
sharedRecord<string, { version: string; module: unknown }>noneModules this host provides besides React. Mismatch: NF-036.

The component takes your props plus descriptor (RemoteComponentDescriptor | null), fallback and errorFallback. Pass descriptor from getComponentDescriptor or fetchDescriptorInBrowser. In Next.js, import from @next-federation/next/components instead.

loadRemoteModule(descriptor, expose)

Loads an exposed module of a remote build, cached per build.

preloadRemoteComponent(descriptor, expose)

Starts loading a module early. It never rejects.

fetchDescriptorInBrowser(url, options?)

For SPA hosts. Fetches the current descriptor in the browser with ssr: null, or null when unreachable.

OptionTypeDefaultDescription
remotestringthe descriptor nameThe remote name to record.
maxBuildsnumber5Builds kept loaded.

registerSharedModules(shared)

Provides { name: { version, module } } to every remote component. Call it at module scope, before anything renders.

provideNavigation(hooks)

Gives remote links this host's router. Call it once at module scope. Without it, links do full page loads. Next.js hosts need not call it.

HookTypeDescription
useNavigate() => (href, options?: { replace?, scroll? }) => voidRequired. React Router's useNavigate fits.
useHref(href: string) => stringOptional.
useLocation() => { pathname: string; search: string }Optional.

COMPONENT_EVENT

'nf:component', the document event for each load and failure. Detail: ComponentEvent.

provideHostReact()

From @next-federation/react/share. Puts the host's React into the island share scope, so islands on the same exact React version download none. Call it at module scope of the app entry, before the first island bootstraps. It returns false in development, with a pre-release React (every App Router host), or when an island already provides that version.

import { provideHostReact } from '@next-federation/react/share'

provideHostReact()

Types

FederationErrorBoundaryProps, IslandMountProps, IslandClientEvent, TokenClient, RemoteComponentOptions, RemoteComponentProps, RemoteComponentType, RemoteProps, RemoteComponentDescriptor, ComponentEvent, RemoteModule, SharedModules, NavigationHooks.

Learn more

License: MIT

On this page