- 使用官方的脚手架
npm install -g create-react-app
create-react-app my-app
npm start
npm install redux --save
删除的src/* 的文件,创建一个的index.js的文件
index.js的内容
import { createStore} from 'redux'
const ADD = 'add'
const REMOVE = 'remove'
// 通过reducer建立
function counter (state=0,action) {
switch (action.type){
case ADD:
return state+1
case REMOVE:
return state-1
default:
return 10
}
}
// 新建store
const store = createStore(counter)
// 获取当前的state
const init = store.getState()
console.info('init',init)
function listener () {
const current = store.getState()
console.info(`当前为${current}`)
}
// 订阅事件 打印每次出现的数据
store.subscribe(listener)
// 派发事件
store.dispatch({ type:ADD })
console.info(store.getState())
store.dispatch({ type:ADD })
console.info(store.getState())
store.dispatch({ type:REMOVE })
console.info(store.getState())