How it works
-
State is initiated using a class. This class must be extended from 2n8’s parent class which enhances the store with a few utilities.
-
State changes are made inside actions, which are simply class methods.
When actions are called, any state mutations are auto committed at the end of the action. If you need to commit early, you can use the special inbuilt
this.$commit()
action. -
Generate your React hook:
This uses React’s
useSyncExternalStore
hook to map committed state to selectors:When, and only when, the selected state changes, the component is rerendered by React. This is more optimal than simply passing state down the component tree via props.
-
Select and call actions from the store: