React.Basic.Hooks.Aff
- Package
- purescript-react-basic-hooks
- Repository
- spicydonuts/purescript-react-basic-hooks
#useAff Source
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!
#useAffReducer Source
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 Fiber
s 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
#mkAffReducer Source
mkAffReducer :: forall state action. (state -> action -> { effects :: Array (Aff (Array action)), state :: state }) -> Effect (AffReducer state action)
#runAffReducer Source
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
#UseAffReducer Source
newtype UseAffReducer state action hooks
Constructors
UseAffReducer (hooks & (UseMemo (UnsafeReference (AffReducer state action)) (Reducer { effects :: Array (Aff (Array action)), state :: state } action)) & (UseReducer { effects :: Array (Aff (Array action)), state :: state } action) & (UseEffect (UnsafeReference (Array (Aff (Array action))))))
Instances
Newtype (UseAffReducer state action hooks) _