Hookstate

repository·master·Indexed 23 days ago

https://github.com/avkonst/hookstate

A 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.

Tokens
19.5K
Snippets
31
Records
139
Agent score
80%

What's inside Hookstate

  1. Overview of Hookstate

    master
    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.
  2. Use @hookstate/initializable to enable one-off initialization callbacks

    master
    @hookstate/initializable is a plugin for @hookstate/core that 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.
  3. Use @hookstate/localstored to persist state in browser local storage

    master

    @hookstate/localstored is a plugin for @hookstate/core that enables the automatic persistence of managed data states to the browser's localStorage. This allows your application's state to survive page reloads and browser sessions.

    To use it, you must have @hookstate/core installed 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.

  4. Use @hookstate/snapshotable to track state changes

    master
    The @hookstate/snapshotable plugin extends @hookstate/core by 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.
  5. Use @hookstate/broadcasted to sync state across browser tabs

    master
    The @hookstate/broadcasted plugin is an extension for @hookstate/core that 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.
  6. Use @hookstate/identifiable to label and identify state

    master

    The @hookstate/identifiable plugin for @hookstate/core enables 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/core installed and then integrate @hookstate/identifiable into your Hookstate setup.

  7. Use @hookstate/comparable to enable state value comparison

    master
    The @hookstate/comparable plugin extends @hookstate/core by 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.
  8. Using state with non-JSON serializable data

    master

    Hookstate 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 noproxy option for the State.get function. 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 Object class, you must explicitly set noproxy: true before accessing the function to avoid errors.

    let state = useHookstate({ callback: () => {} })
    state.get({ noproxy: true }).callback()