In the previous chapter, we changed the hardcoding mockData
to load from Data Loader.
In this chapter, we will further implement the functions of the project, such as the implementation of the function of the Archive button to put the point of contact archive.
Therefore, we will start to write some business logic that has nothing to do with the UI at all. If we continue to write in the component code, more and more noodle code will be generated. To this end, we introduced a code module called Model to decoupling these business logic and UI.
To use the Model API, you need to opt in runtime.state:
To create a complete Model, you first need to define state, including the name and initial value of data in the state.
We use Model to manage the data of the point of contact list, so define the following data state:
Using TS syntax, you can define more complete type information, such as items in each object should have a name
, email
field. In order to implement archive function, also need to create the archived
field to hold the point of contact has been archived state.
We also need a field to access all archived points of contact. We can define a field of type computed to convert the existing data:
Fields of type computed
are defined as function, but can be accessed through state just like normal fields.
Modern.js integrates Immer and can write such state transfer logic just like normal mutable data in JS.
When implementing the Archive button, we need an archive
function, which is responsible for modifying the archived
field of the specified contact. We call this function action:
An action function is a pure function, where a defined input gets a defined output (a shifted state) and should not have any side effects.
The first parameter of the function is the Draft State provided by Immer, and the second parameter is the parameter passed in when the action is called (more on how to call it later).
We try to implement them completely:
Next we connect the above code and put it in the same Model file. First execute the following command to create a new file directory:
Add src/models/contacts.ts
:
We call a plain object containing elements such as state, action, etc. as Model Spec, Modern.js provides Model API, which can generate Model from Model Spec.
Now let's use this Model directly to complement the logic of the project.
First modify src/components/Item/index.tsx
and add the UI and interaction of the Archive button, the content is as follows:
Next, we add src/routes.page.data
and modify src/routes/page.tsx
to pass more parameters to the <Item>
component:
useModel
is the hooks API provided by the Modern.js. You can provide the state defined in the Model in the component, or call the side effects and actions defined in the Model through actions to change the state of the Model.
Model is business logic, a computational process that does not create or hold state itself. Only after being used by the component with the hooks API, the state is created in the specified place.
Execute pnpm run dev
and click the Archive button to see that the page UI has changed.
In the above example, useLoaderData
is actually executed every time the route is switched. Because we used fake data in the Data Loader, the data returned each time is different. But we use the data in the Model first, so the data does not change when switching routes.