Module

React.Basic.Hooks.Aff

Package
purescript-react-basic-hooks
Repository
spicydonuts/purescript-react-basic-hooks

#useAff Source

useAff :: forall a deps. Eq deps => deps -> Aff a -> Hook (UseAff deps a) (Maybe a)

useAff is used for asynchronous effects or Aff. The asynchronous effect is re-run whenever the deps change. If another Aff runs when the deps change before the previous async resolves, it will cancel the previous in-flight effect.

Note: This hook requires parent components to handle error states! Don't forget to implement a React error boundary or avoid Aff errors entirely by incorporating them into your result type!

#UseAff Source

newtype UseAff deps a hooks

Instances

#useAffReducer Source

useAffReducer :: forall action state. state -> AffReducer state action -> Hook (UseAffReducer state action) (state /\ (action -> Effect Unit))

Provide an initial state and a reducer function. This is a more powerful version of useReducer, where a state change can additionally queue asynchronous operations. The results of those operations must be mapped into the reducer's action type. This is essentially the Elm architecture.

Generally, I recommend useAff paired with tools like useResetToken over useAffReducer as there are many ways useAffReducer can result in race conditions. useAff with proper dependency management will handle previous request cancellation and ensure your Aff result is always in sync with the provided deps, for example. To accomplish the same thing with useAffReducer would require tracking Fibers manually in your state somehow.. :c

That said, useAffReducer can still be helpful when converting from the current React.Basic (non-hooks) API or for those used to Elm.

Note: Aff failures are thrown. If you need to capture an error state, be sure to capture it in your action type!

#AffReducer Source

newtype AffReducer state action

#noEffects Source

noEffects :: forall action state. state -> { effects :: Array (Aff (Array action)), state :: state }

#UseAffReducer Source

newtype UseAffReducer state action hooks

Instances