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. Public state is useful for application config, feature flags, settings, and catalog or inventory values.


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

bash
npm install @skystate/react

@skystate/react depends on @skystate/core. You do not need to install @skystate/core separately.

React 18 or later is required.


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

Contract

ContractDetail
Provider scopeOne mounted provider maps to one account, project, and environment. Remount to switch scope.
Keyed hooksKeyed public hooks return { value }; keyed user hooks return { value, set, clear, syncStatus, draft }.
HealthuseStatus().health reports overall loading/error status.
MetadataUse the console, CLI, or REST API for inspection, audit, or history metadata.

Error Codes

CodeSurfaced
missing_providerThrown: a React SDK hook was called outside a SkyStateProvider.
missing_configThrown: required client configuration is missing, for example an empty account or project.
disposedThrown: a client method was called after the client was disposed.
invalid_pathInvalid key or non-JSON value. Thrown at the call site when detectable there; a functional updater that fails validation at save time instead fires onError and pins a permanent health error until remount.

For broader error handling patterns, see useStatus.


Key Behaviors to Know

BehaviorDetail
Public reads are available before loginPublic state can render for signed-out visitors.
User state requires loginUser state loads and writes require an authenticated end-user session. Call useStatus().auth.loginWithRedirect() to start the login flow.
Production cache TTL: 900 secondsPublic-state reads are cached 15 minutes in production.
Config size limit: 256,000 charsThe serialized JSON for a public-state object must not exceed 256,000 characters.
Account/project route changes break URLsSDK clients must use the current account route identifier and project identifier.