Gathering detailed insights and metrics for use-reducer-with-side-effects
Gathering detailed insights and metrics for use-reducer-with-side-effects
Gathering detailed insights and metrics for use-reducer-with-side-effects
Gathering detailed insights and metrics for use-reducer-with-side-effects
compatible-use-reducer-with-side-effects
[](https://github.com/conorhastings/react-syntax-highlighter/actions) [
use-reducer-with-effects
React Hook that colocates reducer and side effects
use-reducer-effect
A tiny library that enables side effects with the useReducer hook
react-async-saga-reducer
Use redux saga with useReducer hook to handle side effects without Redux
npm install use-reducer-with-side-effects
Typescript
Module System
Node Version
NPM Version
JavaScript (100%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
88 Stars
91 Commits
18 Forks
3 Watchers
10 Branches
5 Contributors
Updated on Sep 09, 2024
Latest Version
2.2.0
Package Id
use-reducer-with-side-effects@2.2.0
Unpacked Size
16.62 kB
Size
4.89 kB
File Count
8
NPM Version
6.9.0
Node Version
10.16.3
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
1
Inspired by the reducerComponent
of ReasonReact
, this provides a way to declaratively declare side effects with updates, or to execute a side effect through the reducer while keeping the reducer pure.
The general idea being that the side effects simply declare intent to execute further code, but belong with the update.
reducers always return one of Update
, NoUpdate
, UpdateWithSideEffects
, or SideEffects
function.
One example in which this may be useful is when dispatching a second action depends on the success of the first action, instead of waiting to find out, one can declare the side effect along side the update.
npm install use-reducer-with-side-effects
yarn add use-reducer-with-side-effects
Update(newState)
Outside of returning the functions of above inside you're reducer this should function almost identically to the built in useReducer.
1import React, { useReducer } from 'react'; 2 3function Avatar({ userName }) { 4 const [state, dispatch] = useReducer( 5 (state, action) => { 6 switch (action.type) { 7 case FETCH_AVATAR: { 8 return { ...state, fetchingAvatar: true }; 9 } 10 case FETCH_AVATAR_SUCCESS: { 11 return { ...state, fetchingAvatar: false, avatar: action.avatar }; 12 } 13 case FETCH_AVATAR_FAILURE: { 14 return { ...state, fetchingAvatar: false }; 15 } 16 } 17 }, 18 { avatar: null } 19 ); 20 21 useEffect(() => { 22 dispatch({ type: FETCH_AVATAR }); 23 fetch(`/avatar/${userName}`).then( 24 avatar => dispatch({ type: FETCH_AVATAR_SUCCESS, avatar }), 25 dispatch({ type: FETCH_AVATAR_FAILURE }) 26 ); 27 }, [userName]); 28 29 return <img src={!state.fetchingAvatar && state.avatar ? state.avatar : DEFAULT_AVATAR} /> 30}
Library with colocated async action
1import useReducerWithSideEffects, { UpdateWithSideEffect, Update } from 'use-reducer-with-side-effects'; 2 3function Avatar({ userName }) { 4 const [{ fetchingAvatar, avatar }, dispatch] = useReducerWithSideEffects( 5 (state, action) => { 6 switch (action.type) { 7 case FETCH_AVATAR: { 8 return UpdateWithSideEffect({ ...state, fetchingAvatar: true }, (state, dispatch) => { // the second argument can also be an array of side effects 9 fetch(`/avatar/${userName}`).then( 10 avatar => 11 dispatch({ 12 type: FETCH_AVATAR_SUCCESS, 13 avatar 14 }), 15 dispatch({ type: FETCH_AVATAR_FAILURE }) 16 ); 17 }); 18 } 19 case FETCH_AVATAR_SUCCESS: { 20 return Update({ ...state, fetchingAvatar: false, avatar: action.avatar }); 21 } 22 case FETCH_AVATAR_FAILURE: { 23 return Update({ ...state, fetchingAvatar: false }) 24 } 25 } 26 }, 27 { avatar: null } 28 ); 29 30 useEffect(() => dispatch({ type: FETCH_AVATAR }) , [userName]); 31 32 return <img src={!fetchingAvatar && avatar ? avatar : DEFAULT_AVATAR} />; 33}
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 6/24 approved changesets -- score normalized to 2
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
33 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More