@@ -5,8 +5,7 @@ import { applyReducerState } from './helpers'
55
66export function createStateProvider < S , R extends React . Reducer < any , any > > ( options : {
77 initialState : S
8- actions ?: Record < string , string >
9- reducer ?: R
8+ actions : Record < string , string >
109 actionCases ?: StateActionCases < S >
1110 providerHelpers ?: ( dispatch : React . Dispatch < React . ReducerAction < R > > ) => StateProviderHelpers
1211} ) : [
@@ -30,10 +29,10 @@ export function createStateProvider<S, R extends React.Reducer<any, any>>(option
3029 helpers : any
3130 state : S
3231 }
33- if ( ! options . reducer && ! options . actions ) {
32+ if ( ! options . actions ) {
3433 throw new Error ( "The 'reducer' or 'actions' option must be provided, one of them needs to be passed." )
3534 }
36- const actionTypes = Object . values ( options . actions ?? { } )
35+ const actionTypes = Object . values ( options . actions )
3736 const knownActions = JSON . stringify ( actionTypes )
3837 const Context = React . createContext < ProviderProps > ( {
3938 // eslint-disable-next-line @typescript-eslint/no-unused-vars
@@ -42,18 +41,15 @@ export function createStateProvider<S, R extends React.Reducer<any, any>>(option
4241 state : options . initialState , // dispatch/state will be maintained by Reducer provided in StateProvider going forward
4342 } )
4443 function StateProvider ( props : StateProviderProps ) : React . ReactElement < React . ProviderProps < ProviderProps > > {
45- const reducer = options . reducer
46- ? options . reducer
47- : ( prevState : S , action : StateAction ) : any => {
48- if ( ! actionTypes . includes ( action . type ) ) {
49- throw new Error ( `Unknown action: "${ action . type } ", known actions: ${ knownActions } ` )
50- }
51- if ( typeof options ?. actionCases ?. [ action . type ] !== 'function' ) {
52- return applyReducerState ( prevState , action )
53- }
54- return options . actionCases [ action . type ] ( prevState , action )
55- }
56- const [ state , dispatch ] = React . useReducer ( reducer , options . initialState )
44+ const [ state , dispatch ] = React . useReducer ( ( prevState : S , action : StateAction ) : any => {
45+ if ( ! actionTypes . includes ( action . type ) ) {
46+ throw new Error ( `Unknown action: "${ action . type } ", known actions: ${ knownActions } ` )
47+ }
48+ if ( typeof options ?. actionCases ?. [ action . type ] !== 'function' ) {
49+ return applyReducerState ( prevState , action )
50+ }
51+ return options . actionCases [ action . type ] ( prevState , action )
52+ } , options . initialState )
5753 return (
5854 < Context . Provider
5955 value = { {
0 commit comments