@coalesce.dev/store-client-react
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published

Shared Store - Client - React

@coalesce.dev/store-client-react

This package provides React hooks and components to use with @coalesce.dev/store-client.

Usage

Wrap your application component with a SharedStoreProvider at a higher level than any usage:

<SharedStoreProvider store={storeClient}>
  <App />
</SharedStoreProvider>

Selecting State

Values can be retrieved from the store using the useSharedValue hook. For easier usage, a store specific hook can be implemented:

export function useMySharedValue<S extends Selector>(selector: S) {
  return useSharedValue<StoreState /* set your store's state type here */, S>(
    selector
  );
}

Mutating State

Values can be mutated using the useSharedState hook. Similarly, a store specific hook can also be implemented:

export function useMySharedState<S extends Selector>(selector: S) {
  return useSharedState<StoreState, S>(selector);
}

This hook returns a tuple containing the current state and a callback to change the value. Note that this callback is asynchronous and changes will not be immediately reflected.

To get a reference to the store, use the useSharedStore hook.

Readme

Keywords

none

Package Sidebar

Install

npm i @coalesce.dev/store-client-react

Weekly Downloads

1

Version

0.1.4

License

GNU GPLv3

Unpacked Size

57.8 kB

Total Files

5

Last publish

Collaborators

  • matt.meeks