Function: useJourney()
@rxova/journey-react / useJourney
function useJourney<TBundle>(factory): TBundle;Defined in: react/src/use-journey.ts:38
Owns a bundle for one component instance: creates it once, keeps it across re-renders, and disposes it when the component really unmounts.
Use it instead of a useState lazy initializer. React double-invokes those
initializers under StrictMode, so useState(() => createLinearJourney(...))
builds two machines per mount — two plugin setup() passes, two
persistence reads and writes, two armed autosave timers — and silently
abandons one without disposing it.
const signup = useJourney(() => createLinearJourney({ context: initialContext, steps: ["email", "review"] }));Later renders ignore the factory, so it may close over props freely, but changing them will not rebuild the bundle — deliberate, since the machine is the journey’s identity.
Disposal is deferred by a macrotask so StrictMode’s synchronous unmount/remount cycle cancels it: a real unmount disposes, a simulated one does not. Module-scope bundles are unaffected — they are never disposed, by design.
React 18 caveat. React 18’s StrictMode re-mounts hooks on its second render pass, handing the component a fresh ref, so the factory runs twice there in development (once on React 19, and once everywhere in production). Only the committed bundle is ever started — the discarded one never mounts, so its start effect never runs and it holds no timers, no subscriptions, and no journey state before being collected. Avoid side effects in the factory beyond building the bundle.
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
TBundle extends OwnedJourneyBundle |
Parameters
Section titled “Parameters”| Parameter | Type |
|---|---|
factory | () => TBundle |
Returns
Section titled “Returns”TBundle