@unlaxer/tramli-react
v0.2.0
Published
React hooks for tramli — useFlow wraps FlowEngine + FlowStore + FlowInstance lifecycle
Downloads
31
Readme
@unlaxer/tramli-react
tramli の React フック。useFlow が FlowEngine + FlowStore + FlowInstance のライフサイクルをコンポーネントに統合します。
インストール
npm install @unlaxer/tramli-react @unlaxer/tramli react使い方
import { useFlow } from '@unlaxer/tramli-react';
function AuthPage() {
const { state, context, resume, error, isLoading, flowId } = useFlow(authFlowDefinition, {
initialData: new Map([['RequestOrigin', { returnTo: '/' }]]),
});
if (isLoading) return <Spinner />;
if (error) return <ErrorBanner error={error} />;
if (state === 'MFA_PENDING') return <MfaForm onSubmit={code => resume(new Map([['MfaCode', code]]))} />;
if (state === 'COMPLETE') return <Dashboard />;
return <LoginRedirect />;
}API
useFlow<S>(definition, options?)
| 引数 | 型 | 説明 |
|---|---|---|
| definition | FlowDefinition<S> | tramli フロー定義 |
| options.initialData | Map<string, unknown> | 初期データ |
| options.sessionId | string | セッション ID (省略時は自動生成) |
戻り値 (UseFlowResult<S>)
| フィールド | 型 | 説明 |
|---|---|---|
| state | S \| null | 現在のフロー状態 |
| context | FlowContext \| null | フローコンテキスト |
| flowId | string \| null | フローインスタンス ID |
| error | Error \| null | 直近のエラー |
| isLoading | boolean | startFlow/resume 実行中 |
| resume | (data?) => Promise<void> | 外部データでフローを再開 |
動作
FlowEngine+InMemoryFlowStoreはマウント時に一度だけ生成 (useRef)startFlowはuseEffectで非同期実行resumeはuseCallbackで安定参照- 各操作後に
FlowInstanceから React state を同期 - エラーは
errorフィールドに格納 (throw しない) - 操作中は
isLoading = true
License
MIT
