Skip to content

SkyStateProvider

SkyStateProvider is a React context provider that initializes a SkyState client, begins fetching public state, restores auth when possible, and makes state available to all descendant hooks.

Place it near the root of your component tree (or near the root of the subtree that needs SkyState). All React SDK hooks below it read from the same client instance.


Basic Example

tsx
import { SkyStateProvider } from '@skystate/react';

createRoot(document.getElementById('root')!).render(
  <SkyStateProvider account="acc_example" project="my-project" environment="production">
    <App />
  </SkyStateProvider>,
);

Props

typescript
interface SkyStateProviderProps {
  /** Base URL of the SkyState API. Defaults to https://api.skystate.io. */
  apiUrl?: string;
  /** Base URL of the hosted auth page used by loginWithRedirect(). Defaults to https://auth.skystate.io. */
  authFrontUrl?: string;
  /** Account route identifier, for example acc_123. */
  account: string;
  /** Project identifier. */
  project: string;
  /** Environment slug. Defaults to 'development'. */
  environment?: string;
  /** Accepted for compatibility; browser apps should not use project API keys. */
  apiKey?: string;
  /** Accepted for compatibility. */
  debug?: boolean;
  /** Optional explicit auth callback URL. */
  callbackUrl?: string;
  /** Called when provider, auth, or state errors occur. */
  onError?: (error: SkyStateError) => void;
  /** Called after an auth callback completes. */
  onLoginComplete?: (returnTo: string) => void;
  children: ReactNode;
}
PropTypeRequiredDescription
accountstringYesAccount route identifier, for example acc_example.
projectstringYesProject identifier.
environmentstringNodevelopment, staging, or production. Defaults to development.
apiUrlstringNoSkyState API base URL. Defaults to https://api.skystate.io.
authFrontUrlstringNoHosted auth page base URL used by loginWithRedirect(). Defaults to https://auth.skystate.io.
callbackUrlstringNoExplicit hosted-auth callback URL.
onError(error: SkyStateError) => voidNoReceives provider, auth, and state errors.
onLoginComplete(returnTo: string) => voidNoCalled after an auth callback completes.
apiKeystringNoCompatibility prop. Do not put project API keys in browser bundles.
debugbooleanNoCompatibility prop. Most apps can omit it.
childrenReactNodeYesDescendant React tree.

Dynamic Environment Selection

The environment prop is a plain string you supply. The SDK does not read it from any environment variable — wire it however your build system provides it:

tsx
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { SkyStateProvider } from '@skystate/react';
import App from './App';

// Any build variable that resolves to 'development', 'staging', or 'production'.
// import.meta.env.MODE is 'development' in the dev server and 'production' for vite build
// (even staging builds), so use --mode staging and handle all three cases explicitly.
// You can also use a custom variable, e.g. import.meta.env.VITE_ENV.
const mode = import.meta.env.MODE as string;
const environment = mode === 'staging' ? 'staging' : mode === 'development' ? 'development' : 'production';

createRoot(document.getElementById('root')!).render(
  <StrictMode>
    <SkyStateProvider
      account="acc_example"
      project="my-project"
      environment={environment}
    >
      <App />
    </SkyStateProvider>
  </StrictMode>,
);

The apiUrl and authFrontUrl props default to the hosted SkyState service (https://api.skystate.io, https://auth.skystate.io). Most apps do not need to override them. For local-stack or custom-host setups, each resolves with the same precedence: the explicit prop wins; otherwise the provider reads VITE_SKYSTATE_API_URL, then NEXT_PUBLIC_SKYSTATE_API_URL, then REACT_APP_SKYSTATE_API_URL from the build environment (the _AUTH_FRONT_URL variants for authFrontUrl); otherwise the hosted default applies.


Lifecycle

The provider owns one SkyState client for the mounted account, project, and environment.

Props are read once at mount. To switch account, project, or environment, unmount and remount the provider, or give it a different React key.


Using Multiple Providers

Each SkyStateProvider instance connects to one (apiUrl, account, project, environment) tuple. If your application reads state from multiple projects, nest or render multiple providers:

tsx
<SkyStateProvider account="acc_example" project="website">
  <WebsiteSection />
</SkyStateProvider>

<SkyStateProvider account="acc_example" project="admin">
  <AdminSection />
</SkyStateProvider>

Hooks will use the nearest SkyStateProvider ancestor in the tree.