Skip to content

Commit e534757

Browse files
authored
fix(hooks): use createInitialState in reducer (#1556)
1 parent fd6ab68 commit e534757

File tree

5 files changed

+27
-17
lines changed

5 files changed

+27
-17
lines changed

src/hooks/useCombobox/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@ function useCombobox(userProps = {}) {
3939
itemToString,
4040
} = props
4141
// Initial state depending on controlled props.
42-
const initialState = getInitialState(props)
4342
const [state, dispatch] = useControlledReducer(
4443
downshiftUseComboboxReducer,
45-
initialState,
4644
props,
45+
getInitialState,
4746
)
4847
const {isOpen, highlightedIndex, selectedItem, inputValue} = state
4948

src/hooks/useCombobox/utils.js

+8-4
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,17 @@ const propTypes = {
5656
* compute the rest of the state.
5757
*
5858
* @param {Function} reducer Reducer function from downshift.
59-
* @param {Object} initialState Initial state of the hook.
60-
* @param {Object} props The hook props.
59+
* @param {Object} props The hook props, also passed to createInitialState.
60+
* @param {Function} createInitialState Function that returns the initial state.
6161
* @returns {Array} An array with the state and an action dispatcher.
6262
*/
63-
export function useControlledReducer(reducer, initialState, props) {
63+
export function useControlledReducer(reducer, props, createInitialState) {
6464
const previousSelectedItemRef = useRef()
65-
const [state, dispatch] = useEnhancedReducer(reducer, initialState, props)
65+
const [state, dispatch] = useEnhancedReducer(
66+
reducer,
67+
props,
68+
createInitialState,
69+
)
6670

6771
// ToDo: if needed, make same approach as selectedItemChanged from Downshift.
6872
useEffect(() => {

src/hooks/useMultipleSelection/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ function useMultipleSelection(userProps = {}) {
3838
// Reducer init.
3939
const [state, dispatch] = useControlledReducer(
4040
downshiftMultipleSelectionReducer,
41-
getInitialState(props),
4241
props,
42+
getInitialState,
4343
)
4444
const {activeIndex, selectedItems} = state
4545

src/hooks/useSelect/index.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,10 @@ function useSelect(userProps = {}) {
4242
getA11yStatusMessage,
4343
} = props
4444
// Initial state depending on controlled props.
45-
const initialState = getInitialState(props)
4645
const [state, dispatch] = useControlledReducer(
4746
downshiftSelectReducer,
48-
initialState,
4947
props,
48+
getInitialState,
5049
)
5150
const {isOpen, highlightedIndex, selectedItem, inputValue} = state
5251

src/hooks/utils.js

+16-8
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ function useLatestRef(val) {
187187
* Also calls the onChange handlers for state values that have changed.
188188
*
189189
* @param {Function} reducer Reducer function from downshift.
190-
* @param {Object} initialState Initial state of the hook.
191-
* @param {Object} props The hook props.
190+
* @param {Object} props The hook props, also passed to createInitialState.
191+
* @param {Function} createInitialState Function that returns the initial state.
192192
* @returns {Array} An array with the state and an action dispatcher.
193193
*/
194-
function useEnhancedReducer(reducer, initialState, props) {
194+
function useEnhancedReducer(reducer, props, createInitialState) {
195195
const prevStateRef = useRef()
196196
const actionRef = useRef()
197197
const enhancedReducer = useCallback(
@@ -206,7 +206,11 @@ function useEnhancedReducer(reducer, initialState, props) {
206206
},
207207
[reducer],
208208
)
209-
const [state, dispatch] = useReducer(enhancedReducer, initialState)
209+
const [state, dispatch] = useReducer(
210+
enhancedReducer,
211+
props,
212+
createInitialState,
213+
)
210214
const propsRef = useLatestRef(props)
211215
const dispatchWithProps = useCallback(
212216
action => dispatch({props: propsRef.current, ...action}),
@@ -234,12 +238,16 @@ function useEnhancedReducer(reducer, initialState, props) {
234238
* returning the new state.
235239
*
236240
* @param {Function} reducer Reducer function from downshift.
237-
* @param {Object} initialState Initial state of the hook.
238-
* @param {Object} props The hook props.
241+
* @param {Object} props The hook props, also passed to createInitialState.
242+
* @param {Function} createInitialState Function that returns the initial state.
239243
* @returns {Array} An array with the state and an action dispatcher.
240244
*/
241-
function useControlledReducer(reducer, initialState, props) {
242-
const [state, dispatch] = useEnhancedReducer(reducer, initialState, props)
245+
function useControlledReducer(reducer, props, createInitialState) {
246+
const [state, dispatch] = useEnhancedReducer(
247+
reducer,
248+
props,
249+
createInitialState,
250+
)
243251

244252
return [getState(state, props), dispatch]
245253
}

0 commit comments

Comments
 (0)