Skip to content

Instantly share code, notes, and snippets.

@chuck0523
Last active March 7, 2016 23:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuck0523/80dc6ea1c4a395386b00 to your computer and use it in GitHub Desktop.
Save chuck0523/80dc6ea1c4a395386b00 to your computer and use it in GitHub Desktop.
import { createStore } from 'redux'
// reducer。actionを受けてstateを変更するメソッド。
// stateの初期値を0としている。
function counter(state = 0, action) {
switch (action.type) {
case 'INCREMENT':
return state + 1
case 'DECREMENT':
return state - 1
default:
return state
}
}
let store = createStore(counter)
// subscribeを使って、リスナーを登録。
store.subscribe(() =>
console.log(store.getState())
)
// actionをdispatchしている。
// action = アプリケーションの情報をstoreに送るためのオブジェクト。
// type: 何を行うかを識別するプロパティ。
store.dispatch({ type: 'INCREMENT' })
// 1
store.dispatch({ type: 'INCREMENT' })
// 2
store.dispatch({ type: 'DECREMENT' })
// 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment