useEffectReducer
A React hook for managing side-effects in your reducers.
Inspired by the useReducerWithEmitEffect
hook idea by Sophie Alpert.
If you know how to useReducer
, you already know how to useEffectReducer
.
💻 CodeSandbox example: Dog Fetcher with useEffectReducer
- Installation
- Quick Start
- Named Effects
- Effect Implementations
- Initial Effects
- Effect Entities
- Effect Cleanup
- Replacing Effects
- String Events
- API
exec.replace(entity, effect)
- TypeScript
Installation
Install it:
npm install use-effect-reducer
Import it:
;
Create an effect reducer:
const someEffectReducer = { // execute effects like this: ; // or parameterized (better): ; // and treat this like a normal reducer! // ... return state;});
// ...const state dispatch = ; // Just like useReducer:;
Isn't this unsafe?
No - internally, useEffectReducer
(as the name implies) is abstracting this pattern:
// pseudocodeconst myReducer = { const effects = ; const exec = effects; const nextState = // calculate next state return nextState effects;} // in your componentconst allState dispatch = ; ;
Instead of being implicit about which effects are executed and when they are executed, you make this explicit in the "effect reducer" with the helper exec
function. Then, the useEffectReducer
hook will take the pending effects and properly execute them within a useEffect()
hook.
Quick Start
An "effect reducer" takes 3 arguments:
state
- the current stateevent
- the event that was dispatched to the reducerexec
- a function that captures effects to be executed and returns an effect entity that allows you to control the effect
; // I know, I know, yet another counter exampleconst countReducer = { }; const App = { const state dispatch = ; return <div> <output>Count: statecount</output> <button onClick= >Increment</button> </div> ;};
Named Effects
A better way to make reusable effect reducers is to have effects that are named and parameterized. This is done by running exec(...)
an effect object (instead of a function) and specifying that named effect's implementation as the 3rd argument to useEffectReducer(reducer, initial, effectMap)
.
const fetchEffectReducer = { }; const initialState = status: 'idle' user: undefined ; const fetchFromAPIEffect = { ;}; const Fetcher = { const state dispatch = ; return <button onClick= { ; } > Fetch user </div> ;};
Effect Implementations
An effect implementation is a function that takes 3 arguments:
- The
state
at the time the effect was executed withexec(effect)
- The
event
object that triggered the effect - The effect reducer's
dispatch
function to dispatch events back to it. This enables dispatching within effects in theeffectMap
if it is written outside of the scope of your component. If your effects require access to variables and functions in the scope of your component, write youreffectMap
there.
The effect implementation should return a disposal function that cleans up the effect:
// Effect defined inline;
// Parameterized effect implementation// (in the effect reducer); // ... // (in the component)const state dispatch = ;
Initial Effects
The 2nd argument to useEffectReducer(state, initialState)
can either be a static initialState
or a function that takes in an effect exec
function and returns the initialState
:
const fetchReducer = { if eventtype === 'RESOLVE' return ...state data: eventdata ; return state;}; const getInitialState = { ; return data: null ;}; // (in the component)const state dispatch = ;
Effect Entities
The exec(effect)
function returns an effect entity, which is a special object that represents the running effect. These objects can be stored directly in the reducer's state:
const someReducer = { // ... return ...state // state.someEffect is now an effect entity someEffect: ;};
The advantage of having a reference to the effect (via the returned effect entity
) is that you can explicitly stop those effects:
const someReducer = { // ... // Stop an effect entity exec; return ...state // state.someEffect is no longer needed someEffect: undefined ;};
Effect Cleanup
Instead of implicitly relying on arbitrary values in a dependency array changing to stop an effect (as you would with useEffect
), effects can be explicitly stopped using exec.stop(entity)
, where entity
is the effect entity returned from initially calling exec(effect)
:
const timerReducer = { if eventtype === 'START' return ...state timer: ; else if eventtype === 'STOP' // Stop the effect entity exec; return state; return state;};
All running effect entities will automatically be stopped when the component unmounts.
Replacing Effects
If you want to replace an effect with another (likely similar) effect, instead of calling exec.stop(entity)
and calling exec(effect)
to manually replace an effect, you can call exec.replace(entity, effect)
as a shorthand:
const doSomeDelay = { const id = ; return { ; };}; const timerReducer = { if eventtype === 'START' return ...state timer: ; else if eventtype === 'LAP' // Replace the currently running effect represented by `state.timer` // with a new effect return ...state timer: exec ; else if eventtype === 'STOP' // Stop the effect entity exec; return state; return state;};
String Events
The events handled by the effect reducers are intended to be event objects with a type
property; e.g., { type: 'FETCH', other: 'data' }
. For events without payload, you can dispatch the event type alone, which will be converted to an event object inside the effect reducer:
// dispatched as `{ type: 'INC' }`// and is the same as `dispatch({ type: 'INC' })`;
API
useEffectReducer
hook
The useEffectReducer
hook takes the same first 2 arguments as the built-in useReducer
hook, and returns the current state
returned from the effect reducer, as well as a dispatch
function for sending events to the reducer.
const SomeComponent = { const state dispatch = ; // ...};
The 2nd argument to useEffectReducer(...)
can either be a static initialState
or a function that takes in exec
and returns an initialState
(with executed initial effects). See Initial Effects for more information.
const SomeComponent = { const state dispatch = ; // ...};
Additionally, the useEffectReducer
hook takes a 3rd argument, which is the implementation details for named effects:
const SomeComponent = { const state dispatch = ; // ...};
exec(effect)
Used in an effect reducer, exec(effect)
queues the effect
for execution and returns an effect entity.
The effect
can either be an effect object:
// ...const entity = ;
Or it can be an inline effect implementation:
// ...const entity = ;
exec.stop(entity)
Used in an effect reducer, exec.stop(entity)
stops the effect represented by the entity
. Returns void
.
// Queues the effect entity for disposalexec;
exec.replace(entity, effect)
Used in an effect reducer, exec.replace(entity, effect)
does two things:
- Queues the
entity
for disposal (same as callingexec.stop(entity)
) - Returns a new effect entity that represents the
effect
that replaces the previousentity
.
TypeScript
The effect reducer can be specified as an EffectReducer<TState, TEvent, TEffect>
, where the generic types are:
- The
state
type returned from the reducer - The
event
object type that can be dispatched to the reducer - The
effect
object type that can be executed
; ; ; ; ;