@layers/amba-react
v2.0.3
Published
React hooks for the amba SDK — useAmba, useUser, useCollection, useFlag, and more.
Downloads
662
Readme
@layers/amba-react
Amba is the agent-native backend-as-a-service for mobile and web apps. This package is the React hooks layer — companion to @layers/amba-web.
Install
npm install @layers/[email protected] @layers/[email protected] reactConfigure + first call
import { Amba } from "@layers/amba-web";
import { AmbaProvider, useUser, useCollection } from "@layers/amba-react";
// Configure once, before render.
await Amba.configure({ apiKey: import.meta.env.VITE_AMBA_API_KEY });
function App() {
return (
<AmbaProvider>
<Inner />
</AmbaProvider>
);
}
function Inner() {
const { user, isAuthenticated } = useUser();
const { data: todos, loading } = useCollection<{ id: string; title: string }>(
"todos",
);
if (!isAuthenticated) return <SignIn />;
if (loading) return <Spinner />;
return (
<div>
<p>Welcome {user?.display_name}</p>
<ul>{todos?.map((t) => <li key={t.id}>{t.title}</li>)}</ul>
</div>
);
}Hooks at a glance
| Hook | Returns |
| ------------------------------- | -------------------------------------------------------- |
| useAmba() | Raw SDK handle. |
| useUser() | { user, isAuthenticated, loading }. |
| useCollection(name, options) | { data, loading, error, refetch, response }. |
| useFlag(name) | boolean. |
| useVariant(name) | string \| null (multi-variant flag assignment). |
| useEntitlement(name) | boolean — true if the user has the entitlement. |
| useTrackOnMount(event, props) | Side effect — tracks the event on mount. |
Docs
Full reference: https://docs.amba.dev/sdk/react.
License
MIT
