从零理解redux
- 0
#讨论区
00条评论
实时对话
loading...
理解本文,你需要以下基础知识
理解redux的方法当然是取实现一个tiny-redux
啦,进入官方仓库,我们可以看到一个最简单的使用示例
js
import { createStore } from 'redux'
function counterReducer(state = { value: 0 }, action) {
switch (action.type) {
case 'counter/incremented':
return { value: state.value + 1 }
case 'counter/decremented':
return { value: state.value - 1 }
default:
return state
}
}
let store = createStore(counterReducer)
store.subscribe(() => console.log(store.getState()))
store.dispatch({ type: 'counter/incremented' })
// {value: 1}
store.dispatch({ type: 'counter/incremented' })
// {value: 2}
store.dispatch({ type: 'counter/decremented' })
// {value: 1}