Skip to content

Don't recommend the use of import * as reducers. #590

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

Merged
merged 3 commits into from
Aug 25, 2015
Merged
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
40 changes: 26 additions & 14 deletions docs/api/combineReducers.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ reducing function you can pass to [`createStore`](createStore.md).

The resulting reducer calls every child reducer, and gather their results into a single state object. The shape of the state object matches the keys of the passed `reducers`.

>##### A Note for Flux Users
> ##### A Note for Flux Users

>This function helps you organize your reducers to manage their own slices of state, similar to how you would have different Flux Stores to manage different state. With Redux, there is just one store, but `combineReducers` helps you keep the same logical division between reducers.
> This function helps you organize your reducers to manage their own slices of state, similar to how you would have different Flux Stores to manage different state. With Redux, there is just one store, but `combineReducers` helps you keep the same logical division between reducers.

#### Arguments

1. `reducers` (*Object*): An object whose values correspond to different reducing functions that need to be combined into one. One handy way to obtain it is to use ES6 `import * as reducers` syntax, but you can also construct this object manually. See the notes below for some rules every passed reducer must follow.
1. `reducers` (*Object*): An object whose values correspond to different reducing functions that need to be combined into one. See the notes below for some rules every passed reducer must follow.

> Earlier documentation suggested the use of the ES6 `import * as reducers` syntax to obtain the reducers object. This was the source of a lot of confusion, which is why we now recommend exporting a single reducer obtained using `combineReducers()` from `reducers/index.js` instead. An example is included below.

#### Returns

Expand All @@ -35,19 +37,23 @@ While `combineReducers` attempts to check that your reducers conform to some of

#### Example

#### `reducers.js`
#### `reducers/todos.js`

```js
export function todos(state = [], action) {
export default function todos(state = [], action) {
switch (action.type) {
case 'ADD_TODO':
return state.concat([action.text]);
default:
return state;
}
}
```

export function counter(state = 0, action) {
#### `reducers/counter.js`

```js
export default function counter(state = 0, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1;
Expand All @@ -59,19 +65,25 @@ export function counter(state = 0, action) {
}
```

#### `reducers/index.js`

```js
import { combineReducers } from 'redux';
import todos from './todos';
import counter from './counter';

export default combineReducers({
todos,
counter
});
```

#### `App.js`

```js
import { createStore, combineReducers } from 'redux';
import reducer from './reducers/index';

import * as reducers from './reducers';
console.log(reducers);
// {
// todos: Function,
// counter: Function
// }

let reducer = combineReducers(reducers);
let store = createStore(reducer);
console.log(store.getState());
// {
Expand Down