Skip to content

Commit bec4a63

Browse files
author
João Moura
committed
Added combineReducers
1 parent b95eefa commit bec4a63

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

example/src/Context/reducer.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { SUCCESS_COUNT, COUNT } from './actionTypes'
2+
import { combineReducers } from 'recost'
23

3-
export default (state, action) => {
4-
if (action.type === SUCCESS_COUNT || action.type === COUNT) {
4+
const reducerOne = (state, action) => {
5+
if (action.type === SUCCESS_COUNT) {
56
return {
67
...state,
78
count: state.count + 1
@@ -10,3 +11,16 @@ export default (state, action) => {
1011

1112
return state
1213
}
14+
15+
const reducerTwo = (state, action) => {
16+
if (action.type === COUNT) {
17+
return {
18+
...state,
19+
count: state.count + 1
20+
}
21+
}
22+
23+
return state
24+
}
25+
26+
export default combineReducers([reducerOne, reducerTwo])

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
"name": "recost",
33
"version": "0.0.2",
44
"main": "lib/index.js",
5-
"repository": "git@github.com:JWebCoder/recost.git",
5+
"repository": {
6+
"type": "git",
7+
"url": "git+https://github.com/JWebCoder/recost.git"
8+
},
69
"keywords": [
710
"js",
811
"react",

src/index.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,25 @@ let dispatch = () => {
1818
console.warn('Still no context created')
1919
}
2020

21+
let combineReducers = (reducers) => {
22+
return function (state, action) {
23+
let newState = state
24+
reducers.forEach(
25+
function (reducer) {
26+
newState = reducer(newState, action)
27+
}
28+
)
29+
return newState
30+
}
31+
}
32+
2133
export {
2234
Context,
2335
Consumer,
2436
Provider,
2537
withState,
26-
dispatch
38+
dispatch,
39+
combineReducers
2740
}
2841

2942
const initContext = (initialState = {}, reducer, middleware = []) => {

0 commit comments

Comments
 (0)