-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
redux-example.js
29 lines (27 loc) · 942 Bytes
/
redux-example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import {createStore, applyMiddleware} from "redux";
import createLogger from "./logger";
import timestamp from "./timestamp";
// 4. Actionを受け取り新しいStateを返すReducer関数
const reducer = (state = {}, action) => {
switch (action.type) {
case "AddTodo":
return Object.assign({}, state, {title: action.title});
default:
return state;
}
};
// 1. `logger`と`crashReporter`のmiddlewareを適用した`createStore`関数を作る
const createStoreWithMiddleware = applyMiddleware(createLogger(), timestamp)(createStore);
// 2. Reducerを登録したStoreを作成
const store = createStoreWithMiddleware(reducer);
store.subscribe(() => {
// 5. Stateが変更されたら呼ばれる
const state = store.getState();
// 現在のStateを取得
console.log(state);
});
// 3. Storeの変更をするActionをdispatch
store.dispatch({
type: "AddTodo",
title: "Todo title"
});