perf(react): stabilize useFlow/useStepFlow action references#721
Conversation
useFlow and useStepFlow rebuilt their action object on every render, so push/replace/pop and pushStep/replaceStep/popStep had a new reference each render. Since the underlying core actions are already a stable reference, memoize the returned actions on them (the same approach usePrepare uses) so they stay referentially stable across renders — keeping them safe to place in useEffect/useCallback dependency arrays without triggering unnecessary re-runs. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: 2fb9620 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
Disabled knowledge base sources:
📝 WalkthroughSummary by CodeRabbit
WalkthroughThis PR memoizes the action objects returned by ChangesAction Memoization
Estimated code review effort: 1 (Trivial) | ~5 minutes Sequence Diagram(s)sequenceDiagram
participant Component
participant useFlow
participant useStepFlow
participant useMemo
Component->>useFlow: render
useFlow->>useMemo: memoize Actions(coreActions)
useMemo-->>useFlow: stable Actions reference
useFlow-->>Component: return Actions
Component->>useStepFlow: render
useStepFlow->>useMemo: memoize StepActions(coreActions)
useMemo-->>useStepFlow: stable StepActions reference
useStepFlow-->>Component: return StepActions
Related Issues: None specified. Related PRs: None specified. Suggested labels: patch, react Suggested reviewers: None specified. 🐰 A hop, a skip, a memo made, ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Deploying stackflow-demo with
|
| Latest commit: |
1c24c88
|
| Status: | ✅ Deploy successful! |
| Preview URL: | https://8fed06b6.stackflow-demo.pages.dev |
| Branch Preview URL: | https://fix-stable-useflow-actions.stackflow-demo.pages.dev |
Summary
useFlowanduseStepFlowrebuilt their action object on every render, so the returned functions (push/replace/pop,pushStep/replaceStep/popStep) got a new reference on each render. This memoizes them so their references stay stable across renders.coreStore.actionsis already a stable reference for the lifetime of the store (created once inmakeCoreStore, provided via context and never reassigned). So wrappingmakeActions/makeStepActionsinuseMemokeyed on the core actions is enough to keep the returned functions referentially stable — the same approachusePreparealready uses.🤖 Generated with Claude Code