Pullstate

Pullstate

  • Docs
  • GitHub

›Async Action Hooks

Getting Started

  • Installation
  • Quick example
  • Quick example (server rendering)

Reading Store State

  • useStoreState (hook)
  • <InjectStoreState>
  • Subscribe

Updating Store State

  • update()
  • Reactions

Async Actions

  • Introduction
  • Creating an Async Action
  • Use Async Actions
  • Async Action Hooks

    • Hooks overview
    • Post action hook
    • Short circuit hook
    • Cache break hook
  • Other Async Action Options
  • Cache clearing
  • Resolve async state on the server

Dev Tools

  • Redux Devtools

Async hooks overview

The second argument while creating Async Actions allows us to pass hooks for the action:

const searchPicturesForTag = createAsyncAction(async ({ tag }) => {
  // action code
}, hooksGoHere);

This hooksGoHere object has three hook types which we can set for this action, as follows:

{
  postActionHook,
  shortCircuitHook,
  cacheBreakHook
}

Async hooks flow diagram

To try and give you a quick overview of how actions work with hooks, lets look at a top-down flow diagram of an Async Action's execution:

Async Action hooks in action

Quick overview of each

postActionHook({ args, result, stores, context })

Post action hook is for consistently running state updates after an action completes for the first time or hits a cached value.

Read more on the post action hook.

shortCircuitHook({ args, stores })

The short circuit hook is for checking the current state of your app and manually deciding that an action does not actually need to be run, and returning a replacement resolved value yourself.

Read more on the short circuit hook.

cacheBreakHook({ args, result, stores, timeCached })

This hook is run only when an action has already resolve at least once. It takes the currently cached value and decides whether we should "break" the cache and run the action again instead of returning it.

Read more on the cache break hook.


NOTE: In all these hooks, stores is only available when you are doing server rendering and you have used your centralized Pullstate "Core" to create your Async Actions, and are making use of <PullstateProvider> (see the server-rendering part Creating an Async Action). If you have a client-side only app, just import and use your stores directly.

← Use Async ActionsPost action hook →
  • Async hooks flow diagram
  • Quick overview of each
    • postActionHook({ args, result, stores, context })
    • shortCircuitHook({ args, stores })
    • cacheBreakHook({ args, result, stores, timeCached })
Pullstate
Docs
InstallationA Quick ExampleMore
Community
GitHub
Pullstate
Created by Paul Myburgh