Skip to content

Commit e84be8a

Browse files
committed
Moving react to peerDependencies and removing reducer from options.
1 parent ca80ebb commit e84be8a

File tree

4 files changed

+27
-50
lines changed

4 files changed

+27
-50
lines changed

package-lock.json

Lines changed: 5 additions & 32 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@
4848
"engines": {
4949
"node": "^10.12.0 || >=12.0.0"
5050
},
51+
"peerDependencies": {
52+
"react": "^16.14.0 | ^17.0.1",
53+
"react-dom": "^16.14.0 | ^17.0.1"
54+
},
5155
"dependencies": {
5256
"axios": "^0.21.0",
5357
"qs": "^6.9.4",
54-
"react": "^17.0.1",
55-
"react-dom": "^17.0.1",
5658
"resource-endpoint": "^3.3.0",
5759
"url-join": "^4.0.1"
5860
},

src/context/stateProvider.tsx

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import { applyReducerState } from './helpers'
55

66
export 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={{

webpack.config.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,12 @@ module.exports = {
5454
amd: 'react',
5555
root: 'react',
5656
},
57+
'react-dom': {
58+
commonjs: 'react-dom',
59+
commonjs2: 'react-dom',
60+
amd: 'react-dom',
61+
root: 'react-dom',
62+
},
5763
'url-join': {
5864
commonjs: 'url-join',
5965
commonjs2: 'url-join',

0 commit comments

Comments
 (0)