Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .changeset/stable-useflow-actions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@stackflow/react": patch
---

Memoize the action functions returned by `useFlow` and `useStepFlow` so their references stay stable across renders.

Previously these hooks rebuilt their action object (`push`/`replace`/`pop`, `pushStep`/`replaceStep`/`popStep`) on every render, giving the returned functions a new reference each time. Since the underlying core actions are already a stable reference, the returned actions are now memoized on them (the same approach `usePrepare` already uses). This keeps the functions referentially stable when placed in `useEffect`/`useCallback` dependency arrays, avoiding unnecessary re-runs.
5 changes: 3 additions & 2 deletions integrations/react/src/useFlow.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useCoreActions } from "./core";
import { useMemo } from "react";
import type { Actions } from "./Actions";
import { useCoreActions } from "./core";
import { makeActions } from "./makeActions";

export type FlowOutput = {
Expand All @@ -9,5 +10,5 @@ export type FlowOutput = {
export function useFlow(): Actions {
const coreActions = useCoreActions();

return makeActions(() => coreActions);
return useMemo(() => makeActions(() => coreActions), [coreActions]);
}
3 changes: 2 additions & 1 deletion integrations/react/src/useStepFlow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type {
InferActivityParams,
RegisteredActivityName,
} from "@stackflow/config";
import { useMemo } from "react";
import { useCoreActions } from "./core";
import { makeStepActions } from "./makeStepActions";
import type { StepActions } from "./StepActions";
Expand All @@ -11,5 +12,5 @@ export function useStepFlow<ActivityName extends RegisteredActivityName>(
): StepActions<InferActivityParams<ActivityName>> {
const coreActions = useCoreActions();

return makeStepActions(() => coreActions);
return useMemo(() => makeStepActions(() => coreActions), [coreActions]);
}
Loading