Hookstate
repository·master·Indexed 23 days ago
https://github.com/avkonst/hookstateA fast, extensible state management library for React built on top of state hooks as an alternative to Redux or Mobx. It features a core library for local and global state management with support for nested state access, partial updates via merge(), and asynchronous promised state. The ecosystem includes various plugins such as @hookstate/localstored for persistence, @hookstate/broadcasted for cross-tab synchronization, @hookstate/validation for data rules, and @hookstate/devtools for debugging.
What's inside Hookstate
- Hookstate is a modern, high-performance state management library for React. It is designed as a straightforward and extensible alternative to Redux, Mobx, and Recoil, leveraging React state hooks to provide predictable behavior and scalability for large applications.
Use @hookstate/subscribable to subscribe to state changes
masterThe@hookstate/subscribableplugin for@hookstate/coreprovides a mechanism to easily subscribe a custom callback function to state changes. This allows you to trigger side effects or external logic whenever the Hookstate value is updated.Use @hookstate/serializable to enable state serialization
masterThe@hookstate/serializableplugin extends@hookstate/coreby providing the ability to serialize and deserialize Hookstate atoms. This is useful for persisting state to storage (likelocalStorage) or sending state over a network.Use @hookstate/initializable to enable one-off initialization callbacks
master@hookstate/initializable is a plugin for@hookstate/corethat allows you to define a one-off callback that runs when a state is initialized. This is useful for performing side effects or setting up external resources exactly once when the state is first created.Use @hookstate/localstored to persist state in browser local storage
master@hookstate/localstoredis a plugin for@hookstate/corethat enables the automatic persistence of managed data states to the browser'slocalStorage. This allows your application's state to survive page reloads and browser sessions.To use it, you must have
@hookstate/coreinstalled and then apply the plugin to your Hookstate states. For detailed implementation details and advanced usage, refer to the official documentation at https://hookstate.js.org/docs/extensions-localstored.Use @hookstate/snapshotable to track state changes
masterThe@hookstate/snapshotableplugin extends@hookstate/coreby providing access to two key pieces of information for your state: the initial value and the current modification status. This is useful for implementing features like 'undo/redo', 'reset to default', or 'unsaved changes' indicators.Use @hookstate/broadcasted to sync state across browser tabs
masterThe@hookstate/broadcastedplugin is an extension for@hookstate/corethat enables the synchronization of a Hookstate instance across different browser tabs. This allows multiple open tabs of the same application to stay in sync with a shared state automatically.Use @hookstate/validation to validate data state
masterThe@hookstate/validationplugin is an extension for@hookstate/corethat enables validation of your data state. It allows you to define validation rules for your Hookstate atoms and ensures that the state adheres to those rules.Use @hookstate/clonable to define state cloning capabilities
master@hookstate/clonableis a plugin for@hookstate/corethat allows you to define how state is cloned. This is useful when you need to create deep or specific copies of your Hookstate objects.Use @hookstate/identifiable to label and identify state
masterThe
@hookstate/identifiableplugin for@hookstate/coreenables state labeling and identification by name. This allows you to track and identify specific state instances within your application using unique identifiers.To use this plugin, you must have
@hookstate/coreinstalled and then integrate@hookstate/identifiableinto your Hookstate setup.Use @hookstate/comparable to enable state value comparison
masterThe@hookstate/comparableplugin extends@hookstate/coreby enabling comparison logic for state values. This allows Hookstate to determine if a state value has actually changed before triggering re-renders or updates, which can be useful for optimizing performance or implementing custom equality checks.Using state with non-JSON serializable data
masterHookstate allows states (both root-level and nested) to hold instances of custom classes or standard classes like
Date.When accessing these non-JSON values, Hookstate automatically enables the
noproxyoption for theState.getfunction. This means the values are tracked as whole instances for re-rendering purposes; accessing a single property of such a value will cause the entire object to be treated as the dependency for re-renders.Handling functions in state objects If a state value holds a function, but the value itself is an instance of the
Objectclass, you must explicitly setnoproxy: truebefore accessing the function to avoid errors.let state = useHookstate({ callback: () => {} }) state.get({ noproxy: true }).callback()