In the Quick Start, we implemented the simplest counter model, which still required 10 lines of code. In fact, Modern.js supports automatically generating commonly used actions based on the declared state
type, which reduces the amount of boilerplate code. The currently supported types are:
As shown above, we only need one line to create a simple countModel
. Here's an example code that uses the model:
When the state is an array type, an example code for automatically generating actions is shown below:
We can use the methods of the JavaScript Array object to modify the state.
When the state is a simple object type, an example code for automatically generating actions is shown below:
Three different fields a
, b
, and c
each generate setA
, setB
, and setC
actions, respectively.
When the user-defined action and the action automatically generated by Modern.js have the same name, the user-defined action takes precedence. For example, if setA
action is already defined in countModel
, calling actions.setA()
executes the user-defined setA
.
For more information on related APIs, please refer to here.