Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Store<S>

The Store. A home for your state, and from whence you can pull it, shape it and listen to it in various ways.

Type parameters

  • S: object = object

    Your store's state interface

Hierarchy

  • Store

Index

Constructors

constructor

  • new Store(initialState: S): Store
  • Create your Store with an initial state.

    Parameters

    • initialState: S

      The initial state for this store

    Returns Store

Methods

applyPatches

  • applyPatches(patches: Patch[]): void

createReaction

  • React to changes on this store by watching a sub-selection of its state and then updating the store directly (similar to subscribe, but allows direct batched updates to the store on changes)

    Type parameters

    • T

      The type of the returned watched value

    Parameters

    • watch: (state: S) => T

      The selector function. Return the values you want to watch for changes on.

        • (state: S): T
        • Parameters

          • state: S

          Returns T

    • reaction: TReactionFunction<S, T>

      The reaction. The function which will run with those changes whenever they have changed.

    • Optional options: ICreateReactionOptions

    Returns () => void

    An un-subscribe function. Stops listening for watched changes.

      • (): void
      • Returns void

getRawState

  • getRawState(): S
  • Returns the raw state object contained within this store at this moment


    WARNING

    Most of the time, if you're using this in your App, there's probably a better way to do it


    Returns S

listenToPatches

  • listenToPatches(patchListener: PatchListener): () => void
  • Listen to all changes made to this store's state in the form of "patches" sent to a patch listener. This is basically a direct link to Immer's functionality of patches.

    See also: Store.applyPatches

    Parameters

    • patchListener: PatchListener

      A function which matches exactly the patch callback functionality in Immer

    Returns () => void

      • (): void
      • Returns void

replace

  • replace(newState: S): void
  • Replace the store's state entirely with a new state value

    Parameters

    • newState: S

    Returns void

subscribe

  • subscribe<T>(watch: (state: S) => T, listener: (watched: T, allState: S, previousWatched: T) => void): () => void
  • Subscribe to changes on this store by watching a sub-selection of its state

    Type parameters

    • T

      The type of the returned watched value

    Parameters

    • watch: (state: S) => T

      The selector function. Return the values you want to watch for changes on.

        • (state: S): T
        • Parameters

          • state: S

          Returns T

    • listener: (watched: T, allState: S, previousWatched: T) => void

      The listener. The function which will run with those changes whenever they have changed.

        • (watched: T, allState: S, previousWatched: T): void
        • Parameters

          • watched: T

            The new watched value (returned from the selection function)

          • allState: S

            Your entire store's state (read-only, do not mutate)

          • previousWatched: T

            The previous value that was watched

          Returns void

    Returns () => void

    An un-subscribe function. Stops listening for watched changes.

      • (): void
      • Returns void

update

  • update(updater: TUpdateFunction<S> | TUpdateFunction<S>[], patchesCallback?: undefined | ((patches: Patch[], inversePatches: Patch[]) => void)): void
  • The store's update function. Pass in a function which takes the current store's state and mutates it directly, thanks to Immer

    Parameters

    • updater: TUpdateFunction<S> | TUpdateFunction<S>[]

      The update function. Takes the current state and mutates it to the next state.

    • Optional patchesCallback: undefined | ((patches: Patch[], inversePatches: Patch[]) => void)

      Exactly the same as the Immer patches callback. Gives you the patches which represent the exact granular changes that happened to your store's state during this update.

    Returns void

useLocalCopyInitial

  • useLocalCopyInitial(deps?: ReadonlyArray<any>): Store<S>
  • 🎣 React hook

    Makes a local component-bound copy ot this store, using the initial state that was set when this store was created, which otherwise functions exactly the same.

    Parameters

    • Optional deps: ReadonlyArray<any>

      Dependencies, which if changed will cause a new local Store to be created and returned - again set to the initial state.

    Returns Store<S>

useLocalCopySnapshot

  • useLocalCopySnapshot(deps?: ReadonlyArray<any>): Store<S>
  • 🎣 React hook

    Makes a local component-bound copy of this store, using the current snapshot of its state, which otherwise functions exactly the same.

    Parameters

    • Optional deps: ReadonlyArray<any>

      Dependencies, which if changed will cause a new local Store to be created and returned - again at the Store's current snapshot at that time

    Returns Store<S>

useState

  • useState(): S
  • useState<SS>(getSubState: (state: S) => SS, deps?: ReadonlyArray<any>): SS
  • 🎣 React hook

    Select and use the store's state in your React components

    Returns S

  • Type parameters

    • SS = any

    Parameters

    • getSubState: (state: S) => SS
        • (state: S): SS
        • Parameters

          • state: S

          Returns SS

    • Optional deps: ReadonlyArray<any>

    Returns SS

Generated using TypeDoc