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
npm install use-reducer-with-side-effects
67
Supply Chain
99.5
Quality
75.4
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
88 Stars
91 Commits
18 Forks
3 Watching
10 Branches
5 Contributors
Updated on 09 Sept 2024
Minified
Minified + Gzipped
JavaScript (100%)
Cumulative downloads
Total Downloads
Last day
108.6%
340
Compared to previous day
Last week
17.6%
1,438
Compared to previous week
Last month
-6.6%
5,865
Compared to previous month
Last year
-62.1%
61,358
Compared to previous year
1
React's useReducer
hook should be given a reducer that is a pure function with no side effects. (useReducer
might call the reducer more than once with the same initial state.) Sometimes, however, it makes sense to include network calls or other side effects within the reducer in order to keep your program logic all in one place.
Inspired by the reducerComponent
of ReasonReact
, this library 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)
NoUpdate()
state
, and dispatch
as arguments. SideEffect((state, dispatch) => { /* do something */ }
Update
and SideEffect
combined. It takes the updated state as the first argument (as Update
) and a side-effect callback as the second argument (as SideEffect
). The callback function receives the updated state
(newState) and a dispatch
. UpdateWithSideEffect(newState, (state, dispatch) => { /* do something */ })
Nearly direct replacement to React's useReducer hook, however, the provided reducer must return the result of one of the above functions (Update
/NoUpdate
/UpdateWithSideEffects
/SideEffects
) instead of an updated object. See the useReducer documentation for different options on how to define the initial state.
const [state, dispatch] = useReducerWithSideEffects(reducer, initialState, init)
const [state, dispatch] = useReducerWithSideEffects(reducer, {})
If you've got an existing reducer that works with React's useReducer
and you want to modify to use this library, do the following:
Modify every state change return to use Update
.
old: return {...state, foo: 'bar'}
new: return Update({...state, foo: 'bar'}
Modify every unchanged state return to use NoUpdate
.
old: return state
new: return NoUpdate()
Now the reducer may be used with useReducerWithSideEffects
and can have side effects added by using the SideEffect
or UpdateWithSideEffect
methods.
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
30 existing vulnerabilities detected
Details
Score
Last Scanned on 2024-11-25
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