nexus
Integrations

React Integration

Use Nexus instances and State from React.

@nexus-js/react lets React components access a Nexus instance and use Nexus State.

import { createNexusScope, NexusProvider, useNexus } from "@nexus-js/react";

For synchronized remote state, continue to the Nexus State React guide.

NexusProvider

Provide a Nexus instance to the React tree:

<NexusProvider nexus={nexus}>
  <App />
</NexusProvider>

useNexus()

useNexus() returns the Nexus instance from the nearest NexusProvider. It fails fast outside a provider.

createNexusScope()

Use a typed React scope when one application uses more than one adapter. The scope prevents a target or State StoreToken for one adapter from being used with another adapter’s Nexus instance:

import { createNexusScope } from "@nexus-js/react";
import {
  usingBackgroundScript,
  type ChromeAdapterModel,
} from "@nexus-js/chrome";

const ChromeNexus = createNexusScope<ChromeAdapterModel>();
const chromeNexus = usingBackgroundScript();

function ChromeApp() {
  return (
    <ChromeNexus.NexusProvider nexus={chromeNexus}>
      <App />
    </ChromeNexus.NexusProvider>
  );
}

Use ChromeNexus.useNexus() inside that tree. The scope also exposes typed Nexus State hooks and scope factories documented in the Nexus State React guide.

Service Sessions

Keep the Connection returned by nexus.connect(...) alongside a service proxy when the UI must observe its session. Register connection.onDisconnected to request a replacement from application state; existing proxies never retarget. There is no React proxy-status hook. See Connection lifecycle for the Core observer and replacement rules.

State host stores are ordinary Zustand stores bound with createNexusStore(...) or bindNexusStore(...); React consumes the remote mirror and does not add a withNexusState middleware.

Nexus State

For createRemoteStoreScope(), useRemoteStore(), useStoreStatus(), Zustand’s useStore(), and State-specific loading, replacement, and selector semantics, use the Nexus State React guide.

Edit this page on GitHub