Store by event scheme
The best way to use Foxstore
State
Event Scheme is a config there you pass for every event handlers by type.
First of all, let`s create a State. For example, we have some items which we want to display in a window by pages.
interface PagingModel {
pages: number;
currentPage: number;
total: number;
}
// Be careful, use type, but not interface
// Typescript behaviour :(
type State = {
items: Item[];
pageSize: number;
itemsOnDisplay: number;
paging?: PagingModel;
}
const InitState: State = {
items: [],
pageSize: 5,
itemsOnDisplay: 0,
paging: {
pages: 0,
currentPage: 0,
total: 0,
}
}After that we can design our Event Scheme. Let`s begin from Event Scope - which Events do we need?
It is not nessesary to use Enum, but it is good practice to keep strings in constants of enums to easily modify them. Now, prepare your handers.
Handlers
Now we have a few small functions that are our bricks to build full system. Let`s put them on their places!
Event Scheme
So, you can build your scheme from small bricks, take parts of scheme and put it in another files, you can reuse them. That's why a function createHandlers returns another function to get EventName. It is small overhead that allows us to create a pack of handlers and reuse it to a few events.
At the end, we need to create our store and use it.
EventScheme gives us some features, for example - type checking of the payload when you are dispatching event


Last updated
Was this helpful?