@@ -187,11 +187,11 @@ function useLatestRef(val) {
187
187
* Also calls the onChange handlers for state values that have changed.
188
188
*
189
189
* @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 .
192
192
* @returns {Array } An array with the state and an action dispatcher.
193
193
*/
194
- function useEnhancedReducer ( reducer , initialState , props ) {
194
+ function useEnhancedReducer ( reducer , props , createInitialState ) {
195
195
const prevStateRef = useRef ( )
196
196
const actionRef = useRef ( )
197
197
const enhancedReducer = useCallback (
@@ -206,7 +206,11 @@ function useEnhancedReducer(reducer, initialState, props) {
206
206
} ,
207
207
[ reducer ] ,
208
208
)
209
- const [ state , dispatch ] = useReducer ( enhancedReducer , initialState )
209
+ const [ state , dispatch ] = useReducer (
210
+ enhancedReducer ,
211
+ props ,
212
+ createInitialState ,
213
+ )
210
214
const propsRef = useLatestRef ( props )
211
215
const dispatchWithProps = useCallback (
212
216
action => dispatch ( { props : propsRef . current , ...action } ) ,
@@ -234,12 +238,16 @@ function useEnhancedReducer(reducer, initialState, props) {
234
238
* returning the new state.
235
239
*
236
240
* @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 .
239
243
* @returns {Array } An array with the state and an action dispatcher.
240
244
*/
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
+ )
243
251
244
252
return [ getState ( state , props ) , dispatch ]
245
253
}
0 commit comments