
Company News
Socket Joins New OpenJS Program to Fund Node.js Security Work
Socket is joining the OpenJS Security Stewardship Program to fund Node.js vulnerability research, maintainer remediation, and security releases.
@schematichq/schematic-react
Advanced tools
`schematic-react` is a client-side React library for [Schematic](https://schematichq.com) which provides hooks to track events, check flags, and more. `schematic-react` provides the same capabilities as [schematic-js](https://github.com/schematichq/schema
schematic-react is a client-side React library for Schematic which provides hooks to track events, check flags, and more. schematic-react provides the same capabilities as schematic-js, for React apps.
npm install @schematichq/schematic-react
# or
yarn add @schematichq/schematic-react
# or
pnpm add @schematichq/schematic-react
You can use the SchematicProvider to wrap your application and provide the Schematic instance to all components:
import { SchematicProvider } from "@schematichq/schematic-react";
ReactDOM.render(
<SchematicProvider publishableKey="your-publishable-key">
<App />
</SchematicProvider>,
document.getElementById("root"),
);
To set the user context for events and flag checks, you can use the identify function provided by the useSchematicEvents hook:
import { useSchematicEvents } from "@schematichq/schematic-react";
const MyComponent = () => {
const { identify } = useSchematicEvents();
useEffect(() => {
identify({
keys: { id: "my-user-id" },
company: {
keys: { id: "my-company-id" },
traits: { location: "Atlanta, GA" },
},
});
}, []);
return <div>My Component</div>;
};
To learn more about identifying companies with the keys map, see key management in Schematic public docs.
Once you've set the context with identify, you can track events:
import { useSchematicEvents } from "@schematichq/schematic-react";
const MyComponent = () => {
const { track } = useSchematicEvents();
useEffect(() => {
track({ event: "query" });
}, []);
return <div>My Component</div>;
};
If you want to record large numbers of the same event at once, or perhaps measure usage in terms of a unit like tokens or memory, you can optionally specify a quantity for your event:
track({ event: "query", quantity: 10 });
To check a flag, you can use the useSchematicFlag hook:
import { useSchematicFlag } from "@schematichq/schematic-react";
import { Feature, Fallback } from "./components";
const MyComponent = () => {
const isFeatureEnabled = useSchematicFlag("my-flag-key");
return isFeatureEnabled ? <Feature /> : <Fallback />;
};
You can check entitlements (i.e., company access to a feature) using a flag check as well, and using the useSchematicEntitlement hook you can get additional data to render various feature states:
import {
useSchematicEntitlement,
useSchematicIsPending,
} from "@schematichq/schematic-react";
import { Feature, Loader, NoAccess } from "./components";
const MyComponent = () => {
const schematicIsPending = useSchematicIsPending();
const {
featureAllocation,
featureUsage,
featureUsageExceeded,
value: isFeatureEnabled,
} = useSchematicEntitlement("my-flag-key");
// loading state
if (schematicIsPending) {
return <Loader />;
}
// usage exceeded state
if (featureUsageExceeded) {
return (
<div>
You have used all of your usage ({featureUsage} /{" "}
{featureAllocation})
</div>
);
}
// either feature state or "no access" state
return isFeatureEnabled ? <Feature /> : <NoAccess />;
};
Note: useSchematicIsPending is checking if entitlement data has been loaded, typically via identify. It should, therefore, be used to wrap flag and entitlement checks, but never the initial call to identify.
The SDK includes built-in fallback behavior you can use to ensure your application continues to function even when unable to reach Schematic (e.g., during service disruptions or network issues).
When flag checks cannot reach Schematic, they use fallback values in the following priority order:
flagCheckDefaults or flagValueDefaults options when initializing the providerfalse if no fallback is configured// Provide a fallback value at the callsite
import { useSchematicFlag } from "@schematichq/schematic-react";
const MyComponent = () => {
const isFeatureEnabled = useSchematicFlag("feature-flag", {
fallback: true, // Used if API request fails
});
return isFeatureEnabled ? <Feature /> : <Fallback />;
};
// Or configure defaults at initialization
import { SchematicProvider } from "@schematichq/schematic-react";
ReactDOM.render(
<SchematicProvider
publishableKey="your-publishable-key"
flagValueDefaults={{
"feature-flag": true, // Used if API request fails and no callsite fallback
}}
flagCheckDefaults={{
"another-flag": {
flag: "another-flag",
value: true,
reason: "Default value",
},
}}
>
<App />
</SchematicProvider>,
document.getElementById("root"),
);
When events (track, identify) cannot be sent due to network issues, they are automatically queued and retried:
maxEventQueueSize)maxEventRetries)In WebSocket mode, if the WebSocket connection fails, the SDK will provide the last known value or the configured fallback values as outlined above. The WebSocket will also automatically attempt to re-establish it's connection with Schematic using an exponential backoff.
When a React Native app is backgrounded for an extended period, the WebSocket connection may be closed by the OS. When the app returns to the foreground, the connection will automatically reconnect, but this happens on an exponential backoff timer which may cause a delay before fresh flag values are available.
For cases where you need immediate flag updates when returning to the foreground (e.g., after an in-app purchase), you can use one of these methods to re-establish the connection:
forceReconnect(): Always closes and re-establishes the WebSocket connection, guaranteeing fresh valuesreconnectIfNeeded(): Only reconnects if the current connection is unhealthy (more efficient for frequent foreground events)import { useSchematic } from "@schematichq/schematic-react";
import { useEffect } from "react";
import { AppState } from "react-native";
const SchematicAppStateHandler = () => {
const { client } = useSchematic();
useEffect(() => {
const subscription = AppState.addEventListener("change", (state) => {
if (state === "active") {
// Use forceReconnect() for guaranteed fresh values
client.forceReconnect();
// Or use reconnectIfNeeded() to skip if connection is healthy
// client.reconnectIfNeeded();
}
});
return () => subscription.remove();
}, [client]);
return null;
};
Render this inside your SchematicProvider.
For debugging and development, Schematic supports two special modes:
Enables console logging of all Schematic operations:
// Enable at initialization
import { SchematicProvider } from "@schematichq/schematic-react";
ReactDOM.render(
<SchematicProvider publishableKey="your-publishable-key" debug={true}>
<App />
</SchematicProvider>,
document.getElementById("root"),
);
// Or via URL parameter
// https://yoursite.com/?schematic_debug=true
Prevents network requests and returns fallback values for all flag checks:
// Enable at initialization
import { SchematicProvider } from "@schematichq/schematic-react";
ReactDOM.render(
<SchematicProvider publishableKey="your-publishable-key" offline={true}>
<App />
</SchematicProvider>,
document.getElementById("root"),
);
// Or via URL parameter
// https://yoursite.com/?schematic_offline=true
Offline mode automatically enables debug mode to help with troubleshooting.
MIT
Need help? Please open a GitHub issue or reach out to support@schematichq.com and we'll be happy to assist.
FAQs
`schematic-react` is a client-side React library for [Schematic](https://schematichq.com) which provides hooks to track events, check flags, and more. `schematic-react` provides the same capabilities as [schematic-js](https://github.com/schematichq/schema
The npm package @schematichq/schematic-react receives a total of 3,968 weekly downloads. As such, @schematichq/schematic-react popularity was classified as popular.
We found that @schematichq/schematic-react demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 4 open source maintainers collaborating on the project.

Company News
Socket is joining the OpenJS Security Stewardship Program to fund Node.js vulnerability research, maintainer remediation, and security releases.

Security News
Two compromised GitHub Actions were re-enabled with malicious tags intact, exposing thousands of downstream repositories to Mini Shai-Hulud.

Research
/Security News
A malicious Firefox extension fetches its payload after installation to evade detection, steal Google session cookies, and automate account takeover.