Module

React.Basic.Hooks.Aff

#useAff

useAff :: forall deps a. 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

newtype UseAff deps a hooks

Instances

#useAffReducer

useAffReducer :: forall state action. 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

newtype AffReducer state action

#mkAffReducer

mkAffReducer :: forall state action. (state -> action -> { effects :: Array (Aff (Array action)), state :: state }) -> Effect (AffReducer state action)

#runAffReducer

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

Run a wrapped Reducer function as a normal function (like runFn2). Useful for testing, simulating actions, or building more complicated hooks on top of useReducer

#noEffects

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

#UseAffReducer

newtype UseAffReducer state action hooks

Instances

Modules