By default, the export package name for all APIs in this section is: @modern-js/runtime/model
.
If Reduck is integrated separately from Modern.js, the export package name is: @modern-js-reduck/react
.
The original type of Reduck is complex. The following type definition shows the simplified type information. For the original type, see model.
Create a Model for managing application state.
function model(name: string): { define: function }
string
, the unique id of the Model created.Used to define the detailed structure of the Model, supporting passing in an object type or function type parameter.
function define(modelDesc: ModelDesc): Model;
ModelDesc
, definition of Model structure, includes state
、computed
、actions
、effects
etc. props.function define((context: Context, utils: Utils) => ModelDesc): Model;
context
: Reduck Context, can get underlying store
object. store
support all Redux Store API, also mounts the use
method for consuming the Model, and the unmount
method for unmounting the Model.use
、onMount
. use
is the same as store.use
, onMount
is the hook function after the Model is mounted.For example, through use
, you can get the state
and actions
of the Model itself and other Models.
Define the state of the Model. Technically, any type of State is supported, but in practice it is recommended to use a JSON serializable type.
Define the Actions of the Model. The function type of Actions is:
Reduck internally integrates immer, which can directly return the original state
. When the Action has no explicit return value, Reduck internally returns a modified new State object.
Defines the derived state of the Model. The definition of derived state supports two types:
Defines the Effects of the Model. The function types defined in Effects are: