redux使用数据流程以下:
javascript
由上可知,咱们须要一个函数来保存状态值,并暴露出修改,获取,同步的方法,下面开始进行推理html
没有react-create-app,安装后再建立reduxLearnjava
npm install -g create-react-app
复制代码
react-create-app reduxLearn复制代码
删除public
和src
下全部的文件,建立reduxLearn/public/index.html
react
新建reduxLearn/src/index.js
git
设置一个变量来保存咱们想要的效果github
let state = {
title: {
color: 'red',
text: '标题'
},
content: {
color: 'pink',
text: '内容'
}
}
复制代码
建立渲染方法npm
function renderApp(appState) {
renderTitle(appState.title)
renderContent(appState.content)
}
function renderTitle(title) {
let titleEle = document.getElementById('title')
titleEle.innerHTML = title.text
titleEle.style.color = title.color
}
function renderContent(content) {
let contentEle = document.getElementById('content')
contentEle.innerHTML = content.text
contentEle.style.color = content.color
}
function render() {
renderApp(state)
}复制代码
若是在执行 renderApp(initState)以前的某个地方执行了initState=nullredux
页面就会报错 TypeError: Cannot read property 'title' of null
数组
function createStore() {
let state = {
title: {
color: 'red',
text: '标题'
},
content: {
color: 'pink',
text: '内容'
}
}
}复制代码
变量被保护起来了,须要用的时候怎么获取呢?bash
function createStore() {
let state = {
title: {
color: 'red',
text: '标题'
},
content: {
color: 'pink',
text: '内容'
}
}
//获取状态
function getState() {
return state
}
return {
getState
}
}
//使用状态
let store = createStore(reducer)
...
function render() {
renderApp(store.getState())
}
复制代码
若是咱们想改变这个状态的值怎么办呢?
const TITLE_COLOR = 'TITLE_COLOR'
const TITLE_TEXT = 'TITLE_TEXT'
const CONTENT_COLOR = 'CONTENT_COLOR'
const CONTENT_TEXT = 'CONTENT_TEXT'复制代码
//派发
function dispatch(action) {
switch (action.type) {
case TITLE_COLOR:
state.title.color = action.color
break;
case TITLE_TEXT:
state.title.text = action.text
break;
case CONTENT_COLOR:
state.content.color = action.color
break;
case CONTENT_TEXT:
state.content.text = action.text
break;
default:
return state
}
}
复制代码
store.dispatch({ type: TITLE_COLOR, color: 'blue' })
store.dispatch({ type: TITLE_TEXT, text: '修改标题了' })
render()
复制代码
function createStore(reducer) {
let state;
//派发
function dispatch(action) {
state = reducer(state, action)//经过reducer处理返回结果值
}
//获取状态
function getState() {
return state
}
//执行一次,目的是设置默认值
dispatch({ type: "@@TYPE_INIT_STATE" })
return {
dispatch,
getState,
}
}
复制代码
//声明初始值
const initState = {
title: {
color: 'red',
text: '标题'
},
content: {
color: 'pink',
text: '内容'
}
}
//reducer 这里返回的数据应是新对象->state = reducer(state, action)//经过reducer处理返回结果值
function reducer(state = initState, action) {
switch (action.type) {
case TITLE_COLOR:
return { ...state, title: { ...state.title, color: action.color } }
case TITLE_TEXT:
state.title.text = action.text
return { ...state, title: { ...state.title, text: action.text } }
case CONTENT_COLOR:
state.content.color = action.color
return { ...state, content: { ...state.content, color: action.color } }
case CONTENT_TEXT:
return { ...state, content: { ...state.content, text: action.text } }
default:
return state
}
}复制代码
let store = createStore(reducer)复制代码
发现每次修改都要从新调用render()
才生效,很麻烦,若是咱们在每次修改时添加监听事件,触发订阅模式,则能够一劳永逸
function createStore(reducer) {
let state;
//订阅添加监听函数数组
let listeners = []
//派发
function dispatch(action) {
state = reducer(state, action)
//订阅
listeners.forEach(l => l())
}
//订阅,返回一个取消订阅的方法
function subscribe(listener) {
listeners.push(listener)
return function () {
listeners = listeners.filter(item => item != listener)
}
}
//获取状态
function getState() {
return state
}
dispatch({ type: "@@TYPE_INIT_STATE" })
return {
dispatch,
getState,
subscribe
}
}
复制代码
store.subscribe(render)复制代码
store.subscribe(render)
setTimeout(() => {
store.dispatch({ type: TITLE_COLOR, color: 'blue' })
store.subscribe(render)()//取消订阅
store.dispatch({ type: TITLE_TEXT, text: '修改标题了' })
}, 1000);
复制代码
export default function createStore(reducer) {
let state;
let listeners = []
//派发
function dispatch(action) {
state = reducer(state, action)
//订阅
listeners.forEach(l => l())
}
//订阅,返回一个取消订阅的方法
function subscribe(listener) {
listeners.push(listener)
return function () {
listeners = listeners.filter(item => item != listener)
}
}
//获取状态
function getState() {
return state
}
dispatch({ type: "@@TYPE_INIT_STATE" })
return {
dispatch,
getState,
subscribe
}
}
复制代码
import createStore from './createStore'
export {
createStore,
}复制代码
import { createStore } from './redux'复制代码
要想保护数据要找个函数包裹起来createStore
,可变的因素reducer
能够经过参数传递进来,对数据的操做能够在函数内部暴露给外面getstate,dispatch,subscribe
在使用时直接把使用函数赋值给变量store
,使用变量.函数store.getState()
便可
https://github.com/XinYueXiao/interviewHighlights/tree/master/reduxLearn