Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add default value option to combineReducers #1910

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/combineReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ function assertReducerSanity(reducers) {
* @returns {Function} A reducer function that invokes every reducer inside the
* passed object, and builds a state object with the same shape.
*/
export default function combineReducers(reducers) {
export default function combineReducers(reducers, defaultValue = {}) {
var reducerKeys = Object.keys(reducers)
var finalReducers = {}
for (var i = 0; i < reducerKeys.length; i++) {
Expand Down Expand Up @@ -126,7 +126,7 @@ export default function combineReducers(reducers) {
sanityError = e
}

return function combination(state = {}, action) {
return function combination(state = defaultValue, action) {
if (sanityError) {
throw sanityError
}
Expand Down
7 changes: 7 additions & 0 deletions test/combineReducers.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,13 @@ describe('Utils', () => {
spy.restore()
})

it('accepts default value as secound params', () => {
const reducer = combineReducers({
fake: (i='') => i
}, { fake: 'hello' })
expect(reducer(undefined, {}).fake).toEqual('hello')
})

it('throws an error if a reducer returns undefined handling an action', () => {
const reducer = combineReducers({
counter(state = 0, action) {
Expand Down