Options
All
  • Public
  • Public/Protected
  • All
Menu

@twocatmoon/react-actions

Index

Type aliases

Action<State, Input>: { id: string; resolve: any }

Type parameters

  • State

  • Input

Type declaration

    • (...args: Input extends undefined ? [data?: undefined] : [data: Input]): ActionPayload<Input>
    • Called internally by the Store to resolve action state

      Parameters

      • Rest ...args: Input extends undefined ? [data?: undefined] : [data: Input]

      Returns ActionPayload<Input>

  • id: string

    Unique ID for each action. Automatically assigned based on key names from the ActionMap passed into createStore

  • resolve:function
    • resolve(state: State, data: Input): State
    • User-defined resolver function for the action

      Parameters

      • state: State
      • data: Input

      Returns State

ActionMap: {}

Type declaration

  • [key: string]: Action<any, any>

    A collection of Actions with human-readable keys

ActionPayload<Input>: [actionId: string, data: Input]

Type parameters

  • Input = any

ActionSet<State, Input, Result>: (data?: Input) => [ActionSetExecute<State, Input, Result>, Input]

Type parameters

  • State = any

  • Input = any

  • Result = void

Type declaration

ActionSetExecute<State, Input, Result>: (dispatch: Dispatch, state: State, data: Input) => Promise<Result>

Type parameters

  • State

  • Input

  • Result

Type declaration

    • (dispatch: Dispatch, state: State, data: Input): Promise<Result>
    • Parameters

      • dispatch: Dispatch
      • state: State
      • data: Input

      Returns Promise<Result>

ActionSetMap: {}

Type declaration

  • [key: string]: ActionSet<any, any, any>

    A collection of ActionsSets with human-readable keys

CreateStoreContextResult<State>: { Provider: any; useStore: any }

Type parameters

  • State

Type declaration

  • Provider:function
    • Provider(props: { children: React.ReactNode }): Element
    • Parameters

      • props: { children: React.ReactNode }
        • children: React.ReactNode

      Returns Element

  • useStore:function
    • useStore(): [state: State, dispatch: Dispatch<any>, execute: Execute, clearStorage: () => void]
CreateStoreEventBusResult<State>: { useStore: any }

Type parameters

  • State

Type declaration

  • useStore:function
    • useStore(): [state: State, dispatch: Dispatch<any>, execute: Execute, clearStorage: () => void]
CreateStoreOptions: { ssr?: boolean; storageKey?: string; storageType?: "local" | "session" }

Type declaration

  • Optional ssr?: boolean

    Enable support for server-side rendering

  • Optional storageKey?: string

    Key to use when storing state in local/session storage

  • Optional storageType?: "local" | "session"

    Storage API to use, if any

Dispatch<Payload>: (payload: ActionPayload<Payload>) => void

Type parameters

  • Payload = any

Type declaration

Execute: (actionSetPayload: [ActionSetExecute<any, any, any>, any]) => void

Type declaration

ListenerMap: { [ Property in keyof StoreEvents]: ((...args: any[]) => void)[] }
Reducer<State>: (prevState: State, actionPayload: ActionPayload) => State

Type parameters

  • State

Type declaration

StoreContext<State>: { dispatch: Dispatch; execute: Execute; state: State }

The shape of the React context object that contains the Store's state and dispatch function.

Type parameters

  • State

Type declaration

StoreEvents: { state_changed: any }

Type declaration

  • state_changed:function
    • state_changed(event: { newState: any }): void
    • Parameters

      • event: { newState: any }
        • newState: any

      Returns void

Functions

  • action<State, Input>(resolver: (state: State, data: Input) => State): Action<State, Input>
  • Generates an Action object, which when passed into createStore as part of an ActionMap, can be called to mutate the Store's state.

    example
    // store.ts

    const actions = {
    incrementCounter: action<State, number>((prevState, amount) => {
    return {
    ...prevState,
    counter: prevState.counter + amount
    }
    })
    }

    ...

    // Component.tsx

    function Component () {
    const [ state, dispatch ] = useStore()
    dispatch(actions.incrementCounter(2))

    ...
    }

    Type parameters

    • State

    • Input

    Parameters

    • resolver: (state: State, data: Input) => State

      The resolver function to be called by the Store's reducer. This function takes in user-input, and should return the modified state. This function can be thought of as a single case in a switch statement as you'd see in Redux or useReducer

        • (state: State, data: Input): State
        • User-defined resolver function for the action

          Parameters

          • state: State
          • data: Input

          Returns State

    Returns Action<State, Input>

    • The modified full state of the Store
  • Generates an ActionSet object, which can be used to execute asynchronous functions and dispatch Actions to the store.

    example
    // store.ts

    const actionSets = {
    fetchCounterData: actionSet<State, number>(async (dispatch, state, input) => {
    const nextValue = await fetch(`/api/counter/${input}`)
    dispatch(actions.incrementCounter(nextValue))
    })
    }

    ...

    // Component.tsx

    function Component () {
    const [ state, dispatch, execute ] = useStore()
    execute(actionSets.fetchCounterData(2))

    ...
    }

    Type parameters

    • State

    • Input = any

    • Result = void

    Parameters

    Returns ActionSet<State, Input, Result>

    • Optionally returns the Result
  • clearStorage(storageApi: undefined | Storage, storageKey: undefined | string): void
  • Used internally to clear the cached state

    Parameters

    • storageApi: undefined | Storage
    • storageKey: undefined | string

    Returns void

  • example
    type State = {
    counter: number
    }

    const initialState = {
    counter: 0
    }

    const actions = {
    ...
    }

    const options: CreateStoreOptions = {
    storageKey: 'myStore',
    storageType: 'local'
    }

    export const { Provider, useStore } = createStoreContext<State>(initialState, actions, options)

    Type parameters

    • State

    Parameters

    • initialState: State

      The initial state of the Store

    • actions: ActionMap

      The map of Actions to bind to the returned Store

    • Optional options: CreateStoreOptions

    Returns CreateStoreContextResult<State>

  • example
    type State = {
    counter: number
    }

    const initialState = {
    counter: 0
    }

    const actions = {
    ...
    }

    const options: CreateStoreOptions = {
    storageKey: 'myStore',
    storageType: 'local'
    }

    export const { useStore } = createStoreEventBus<State>(initialState, actions, options)

    Type parameters

    • State

    Parameters

    • initialState: State

      The initial state of the Store

    • actions: ActionMap

      The map of Actions to bind to the returned Store

    • Optional options: CreateStoreOptions

    Returns CreateStoreEventBusResult<State>

  • getStorage<State>(storageKey: undefined | string, storageType: undefined | "local" | "session"): [Storage | undefined, State | undefined]
  • Used internally to retrieve the storage API and initial state

    Type parameters

    • State

    Parameters

    • storageKey: undefined | string
    • storageType: undefined | "local" | "session"

    Returns [Storage | undefined, State | undefined]

  • makeReducer<State>(actions: ActionMap, storageApi: undefined | Storage, storageKey: undefined | string): (state: State, payload: ActionPayload<any>) => any
  • Used internally to generate the reducer for the different Stores

    Type parameters

    • State

    Parameters

    • actions: ActionMap
    • storageApi: undefined | Storage
    • storageKey: undefined | string

    Returns (state: State, payload: ActionPayload<any>) => any

Generated using TypeDoc