Creating a Store

In caro-kann, a store is defined as an external space where global state is saved. To create such a store, you must use the create function provided by caro-kann. This function takes the initial value of the state as a parameter, stores it in an internal store, and returns a useStore hook.

The create function must be called outside of a component. If called inside a component, a new store will be created every time the component re-renders, causing the previous state to be lost. This behavior contradicts the purpose of global state management, so special care must be taken.

The useStore hook is the only way to access the store created through the create function, and it returns a tuple of [value, setValue], just like React's useState hook.

Guides - Getting Started with Caro-Kann | ilokesto - React Library Collection