- 下面是一个简单的计数器例子,这里写了一个增长减数字相加的问题
复制代码
<div id="counter">
<div id="counterValue"></div>
<button id="incrementBtn">+</button>
<button id="decrementBtn">-</button>
</div>
复制代码
// 引入了redux ,调用了createStore这个方法
import {createStore} from "redux"
let initState = 0;
// 定义了两个独一无二的类型 Symbol
const INCREMENT = Symbol.for('INCREMENT')
const DECREMENT = Symbol.for('DECREMENT')
// 用来表示动做类型reducer接收两个参数,一个是状态,另一个是触发的行为
// reduce 就是根据触发行为的不一样来返回对应的状态
function reducer(state =initState ,action){
console.log(action)
switch(action.type){
case INCREMENT:
return state+1;
case DECREMENT:
return state-1;
default:
return state;
}
}
// 新老状态对比而后生成新的状态
let store = createStore(reducer)
let state = store.getState()
console.log(state)
let counterValue = document.getElementById("counterValue")
let incrementBtn = document.getElementById("incrementBtn")
let decrementBtn = document.getElementById("decrementBtn")
function render(){
counterValue.innerHTML = store.getState()
}
// 能够手动订阅更新,也能够事件绑定到视图层。
store.subscribe(render)
incrementBtn.addEventListener("click",function(){
console.log(1111)
store.dispatch({type:INCREMENT,data:99})
})
decrementBtn.addEventListener("click",function(){
store.dispatch({type:DECREMENT})
})
复制代码
能够看看上面例子git
在上面的例子中咱们引入了redux ,其中运用了几个API,在上述例子中获得了引用,那么咱们能够来看看这几个api在源码是怎么样?是如何暴露出来的?github
这是github上的源码目录,目录文件不是不少,第一篇首先分析的是入口redux的createStore.js redux
那么咱们再来看看createStore这个api,能够看到上述例子有这么一段代码,咱们知道createStore 是一个函数,而且他返回了一个对象store,也就是咱们常说的存储状态的仓库api
let store = createStore(reducer)
let state = store.getState()
复制代码
以下图,如今让咱们继续深刻下去,能够看到createStore这个函数接收三个参数,那么这时候咱们内心确定在想三个参数分别干吗用的呢?咱们先把它们列出来,在逐一分析每一个参数的含义bash
function reducer(state =initState ,action){
console.log(action)
switch(action.type){
case INCREMENT:
return state+1;
case DECREMENT:
return state-1;
default:
return state;
}
}
复制代码
写了这么多,首先咱们来看看他的第一个api,store.getState()并发
function getState() {
if (isDispatching) {
throw new Error(
'You may not call store.getState() while the reducer is executing. ' +
'The reducer has already received the state as an argument. ' +
'Pass it down from the top reducer instead of reading it from the store.'
)
}
return currentState
}
复制代码
其实前面那个判断只是判断当reduce执行的时候不会进行获取状态,防止并发,若是把这个判断删掉,其实真正有用的就只有一行代码,也便是执行getState()方法 返回一个状态app
function getState() {
return currentState
}
复制代码
let currentState = preloadedState
复制代码
那第二个问题要考虑的就是在哪里改变这个状态的呢,由于咱们在项目中但是有需求想要去改变状态的,而不仅是存着它们,当祖宗同样供着,那样消耗也会很大...如今让咱们继续追踪,redux 究竟是如何进行状态的改变?函数
function dispatch(action) {
currentState = currentReducer(currentState, action)
return action
}
复制代码
如上述例子,能够看到源码中这么一段代码,我进行简单的删减,只留下了这三行代码,从这三行代码咱们就能够看出来不少问题。ui