Skip to content

Commit c2a870a

Browse files
committed
Pass state to the callback actions (fully fixes #1)
1 parent c5dcda8 commit c2a870a

File tree

8 files changed

+34
-13
lines changed

8 files changed

+34
-13
lines changed

README.md

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,17 @@ export function incrementAsync() {
6262
}, 1000);
6363
};
6464
}
65+
66+
// Could also look into state in the callback form
67+
export function incrementIfOdd() {
68+
return (dispatch, state) => {
69+
if (state.counterStore.counter % 2 === 0) {
70+
return;
71+
}
72+
73+
dispatch(increment());
74+
};
75+
}
6576
```
6677

6778
### Stores
@@ -85,7 +96,7 @@ function decremenent({ counter }) {
8596
}
8697

8798
// what's important is that Store is a pure function too
88-
export default function CounterStore(state = initialState, action) {
99+
export default function counterStore(state = initialState, action) {
89100
// that returns the new state when an action comes
90101
switch (action.type) {
91102
case INCREMENT_COUNTER:
@@ -111,7 +122,7 @@ import React from 'react';
111122
import { observes } from 'redux';
112123

113124
// Gonna subscribe it
114-
@observes('CounterStore')
125+
@observes('counterStore')
115126
export default class Counter {
116127
render() {
117128
const { counter } = this.props; // injected by @observes
@@ -133,9 +144,9 @@ import { observes } from 'redux';
133144

134145
// With multiple stores, you might want to specify a prop mapper as last argument.
135146
// You can also access `props` inside the prop mapper.
136-
@observes('CounterStore', 'TodoStore', (state, props) => ({
137-
counter: state.CounterStore.counter,
138-
todos: state.TodoStore.todos
147+
@observes('counterStore', 'todoStore', (state, props) => ({
148+
counter: state.counterStore.counter,
149+
todos: state.todoStore.todos
139150
}))
140151
export default class TodosWithCounter {
141152
/* ... */

examples/counter/Counter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import { performs, observes } from 'redux';
33

44
@performs('increment', 'decrement')
5-
@observes('CounterStore')
5+
@observes('counterStore')
66
export default class Counter {
77
render() {
88
const { increment, decrement } = this.props;

examples/counter/actions/CounterActions.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,16 @@ export function increment() {
99
};
1010
}
1111

12+
export function incrementIfOdd() {
13+
return (dispatch, state) => {
14+
if (state.counterStore.counter % 2 === 0) {
15+
return;
16+
}
17+
18+
dispatch(increment());
19+
};
20+
}
21+
1222
export function incrementAsync() {
1323
return dispatch => {
1424
setTimeout(() => {

examples/counter/stores/CounterStore.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ function decremenent({ counter }) {
1313
return { counter: counter - 1 };
1414
}
1515

16-
export default function CounterStore(state = initialState, action) {
16+
export default function counterStore(state = initialState, action) {
1717
switch (action.type) {
1818
case INCREMENT_COUNTER:
1919
return incremenent(state, action);

examples/counter/stores/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export CounterStore from './CounterStore';
1+
export counterStore from './counterStore';

examples/todo/Body.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react';
22
import { observes } from 'redux';
33

4-
@observes('TodoStore')
4+
@observes('todoStore')
55
export default class Body {
66
render() {
77
return (

examples/todo/stores/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const initialState = {
77
}]
88
};
99

10-
export function TodoStore(state = initialState, action) {
10+
export function todoStore(state = initialState, action) {
1111
switch (action.type) {
1212
case ADD_TODO:
1313
return {

src/createDispatcher.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,10 @@ export default function createDispatcher() {
110110
return function dispatchAction(...args) {
111111
const action = actionCreator(...args);
112112
if (typeof action === 'function') {
113-
// Async action creator
114-
action(dispatchInTransaction);
113+
// Callback-style action creator
114+
action(dispatchInTransaction, currentState);
115115
} else {
116-
// Sync action creator
116+
// Simple action creator
117117
dispatchInTransaction(action);
118118
}
119119
};

0 commit comments

Comments
 (0)