Define a module

To define a module, use the defineModule api with the initial state value. You can extend a module's ability by invoking their builder methods.

Make sure to call the build methods in the end, to turn the module factory into a real module.

Basic

const module = defineModule({ count: 0 })
  .actions({
    add: (draft) => draft.count++,
    minus: (draft) => draft.count--,
  })
  .computed({
    doubled: (state) => state.count * 2,
  })
  .build();

Module Factory

Each steps of building will produce a new factory. With the pure functional design, all steps is resuable and composable.

const addModuleBase = defineModule({ count: 0 })

const moduleA = addModuleBase.actions(...).build()
const moduleB = addModule.actions(...).build()

const withAdd = (module) => module.actions({ add: (state) => state.count++ })
const moduleC = withAdd(addModuleBase).build()

Last updated