Skip to content

Commit f8d72b0

Browse files
committed
Merge pull request reduxjs#44 from emmenko/read-state-by-store
Allow to read state by store on action creator callback
2 parents 319377c + ac1be83 commit f8d72b0

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ export function incrementAsync() {
6262
};
6363
}
6464

65-
// Could also look into state in the callback form
65+
// Could also read state of a store in the callback form
6666
export function incrementIfOdd() {
67-
return (dispatch, state) => {
68-
if (state.counterStore % 2 === 0) {
67+
return (dispatch, read) => {
68+
if (read(counterStore) % 2 === 0) {
6969
return;
7070
}
7171

examples/counter/actions/CounterActions.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import {
22
INCREMENT_COUNTER,
33
DECREMENT_COUNTER
44
} from '../constants/ActionTypes';
5+
import { counterStore } from '../stores';
56

67
export function increment() {
78
return {
@@ -10,8 +11,8 @@ export function increment() {
1011
}
1112

1213
export function incrementIfOdd() {
13-
return (dispatch, state) => {
14-
if (state.counterStore.counter % 2 === 0) {
14+
return (dispatch, read) => {
15+
if (read(counterStore) % 2 === 0) {
1516
return;
1617
}
1718

src/createDispatcher.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,14 +121,19 @@ export default function createDispatcher() {
121121
const action = actionCreator(...args);
122122
if (typeof action === 'function') {
123123
// Callback-style action creator
124-
action(dispatch, currentState);
124+
action(dispatch, read);
125125
} else {
126126
// Simple action creator
127127
dispatch(action);
128128
}
129129
};
130130
}
131131

132+
// Allow to read the state of a store
133+
function read(store) {
134+
return currentState[getStoreKey(store)];
135+
}
136+
132137
return {
133138
wrapActionCreator,
134139
observeStores,

0 commit comments

Comments
 (0)