Skip to main content

Dynamically Setting Up a Machine

Retomus allows you to reconfigure a machine at runtime using the .setup() method.
This is useful for injecting dynamic values, overriding handlers, or preparing the machine for specific flows.


🔧 Basic Example

useEffect(() => {
counterMachine.setup({
ctx: {
state: { count: 10 },
sv: { countSharedValue: 5 },
},
actionHandlers: {
increment: ({ ctx, done }) => {
ctx.state.count += 2;
done();
},
},
});
}, []);

This redefines both the context values and the increment logic at runtime.


🧰 Supported Fields in setup()

FieldDescription
ctxNew context data (state, sv, ref)
actionHandlersRuntime handler overrides
options.ctx.overwriteWhether to replace or merge context

📎 Overwriting vs Merging Context

By default, context is merged:

setup({ ctx: { state: { count: 1 } } });

To completely replace context:

setup({
ctx: {
state: { count: 1 },
},
options: {
ctx: {
overwrite: true,
},
},
});

🔁 When to use setup()

  • Injecting test-specific behavior
  • Bootstrapping from localStorage
  • Changing behavior based on user type
  • Implementing machine reuse with different logic

⚠️ Caution

  • Only call .setup() once after machine creation
  • Changing logic mid-flight may cause unexpected side effects if not isolated