Akita also supports using a
class as the underlying value instead of a plain object. In most cases, we don't recommend doing so for the following reasons:
- We can’t store classes in the database. A typical example of this is when you need to save the store snapshot.
- Classes are harder to use with web workers, due to serialization concerns.
- There are various third-party tools (for example
immer) that only work with plain objects.
However, sometimes they can be useful. Here's an example that uses a class:
The class constructor should accept only one parameter which is a plain object
In this case, when you call
set(), Akita will take care to instantiate a new
User by merging the entity's current state with the new parameters.