Skip to content

@skystate/react ​

@skystate/react provides a React context provider and hooks that integrate SkyState public state, user state, and end-user auth into React applications. See what public state is for.


Basic Example ​

tsx
import { SkyStateProvider, useStatus, usePublicState, useUserState } from '@skystate/react';

function Root() {
  return (
    <SkyStateProvider account="acc_example" project="my-project" environment="production">
      <App />
    </SkyStateProvider>
  );
}

function App() {
  const { auth } = useStatus();
  const { value: banner } = usePublicState('banner', { text: 'Welcome' });
  const { value: theme, set: setTheme } = useUserState('theme', 'dark');

  if (auth.status !== 'authenticated') {
    return <button onClick={auth.loginWithRedirect}>Sign in</button>;
  }

  return (
    <>
      <p>{banner?.text}</p>
      <button onClick={() => setTheme(theme === 'dark' ? 'light' : 'dark')}>
        Theme: {theme}
      </button>
    </>
  );
}

Installation ​

See Installation - SDKs. @skystate/react requires React 18 or later and depends on @skystate/core; you do not need to install @skystate/core separately.


API Reference ​

Provider ​

ExportDescription
SkyStateProviderContext provider. Wrap your app (or a subtree) to make SkyState available to hooks.
SkyStateProviderPropsTypeScript type for the provider's props.

Hooks ​

ExportDescription
useStatus()Returns { auth, health }: auth status plus loginWithRedirect() and logout(), and overall client health.
usePublicState<T>(key, fallback?)Returns { value: T | null }.
useUserState<T>(key, fallback?)Returns { value: T | null, set, clear, syncStatus, draft }.

Sections ​


Errors ​

Hooks called outside a SkyStateProvider throw missing_provider. The full error-code list, the per-status write contract, and the retry policy are in Error Handling. For how errors surface in React (health, auth, onError), see useStatus.