Comparing version 1.0.1 to 1.0.2
{ | ||
"name": "repatch", | ||
"version": "1.0.1", | ||
"version": "1.0.2", | ||
"description": "Dispatch reducers", | ||
@@ -5,0 +5,0 @@ "main": "lib/store/index", |
@@ -10,14 +10,14 @@ # Repatch | ||
```javascript | ||
dispatch(state => ({ ...state, isFetching: true })); | ||
store.dispatch(state => ({ ...state, counter: state.counter + 1 })); | ||
``` | ||
In this terminology action is a function that returns a reducer. | ||
**In this terminology action is a function that returns a reducer:** | ||
```javascript | ||
const selectUser = userId => state => ({ | ||
const increment = amount => state => ({ | ||
...state, | ||
selectedUser: userId | ||
counter: state.counter + amount | ||
}); | ||
dispatch(selectUser(123)); | ||
store.dispatch(increment(42)); | ||
``` | ||
@@ -41,3 +41,3 @@ | ||
const store = new Store(<initialState>); | ||
const store = new Store(initialState); | ||
``` | ||
@@ -63,14 +63,18 @@ | ||
We do not need to reduce always the whole state of the store. A good practice to avoid this effort is using sub-reducers. | ||
We do not need to reduce always the whole state of the store. Repatch also offers a way to combine sub-reducers, those describe a deeply nested property in the state. We just define a function that takes a nested reducer as argument, and returns a reducer that reduces the whole state: | ||
```javascript | ||
const reduceFoo = reducer => state => ({ | ||
const reduceFoo = fooReducer => state => ({ | ||
...state, | ||
bar: { | ||
...state.bar, | ||
foo: reducer(state.bar.foo) | ||
foo: fooReducer(state.bar.foo) | ||
} | ||
}); | ||
``` | ||
const fooAction = payload => reduceFoo(state => ({ ...state, ...payload })); | ||
Using that we can define easily an action, that sets an `x` property in the `foo` object: | ||
```javascript | ||
const setX = x => reduceFoo(state => ({ ...state, x })); | ||
``` | ||
@@ -89,3 +93,3 @@ | ||
```javascript | ||
const store = new Store(<initialState>) | ||
const store = new Store(initialState) | ||
.addMiddleware(mw1) | ||
@@ -102,3 +106,3 @@ .addMiddleware(mw2, mw3); | ||
const store = new Store(<initialState>).addMiddleware(thunk); | ||
const store = new Store(initialState).addMiddleware(thunk); | ||
``` | ||
@@ -135,3 +139,3 @@ | ||
const store = new Store(<initialState>) | ||
const store = new Store(initialState) | ||
.addMiddleware(thunk.withExtraArgument({ api, hashHistory })); | ||
@@ -138,0 +142,0 @@ ``` |
13782
192