redux的学习
项目源码TodoList
Redux介绍
Redux是一个用来管理管理数据状态和UI状态的JavaScript应用工具。随着JavaScript单页应用(SPA)开发日趋复杂,JavaScript需要管理比任何时候都要多的state(状态),Redux就是降低管理难度的。(Redux支持React,Angular、jQuery甚至纯JavaScript)

redux的使用
运行 npm i --save redux 安装redux
在项目的src目录下新建store文件夹,store中文件目录如下
1 2 3 4 5 6
| ├ store ├ actionCteater.js ├ actionTypes.js ├ index.js └ reducer.js
|
接下来,我会详细讲解,每一个文件的作用
- 在index.js中书写代码如下
1 2 3 4 5 6 7
| import { createStore } from 'redux'; import reducer from './reducer.js'
const store = createStore(reducer)
export default store
|
- 在 reducer.js中书写一下代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| const defaultState = { inpVal : "say something", list : [ "早上七点起床看redux的知识", "早上八点洗漱吃饭", "早上八点半继续学习", ] }
export default (state=defaultState, action)=>{ return state }
|
- 在组件中使用redux的状态
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| import store from './store'
constructor(props){ super(props) this.state = store.getState() store.subscribe(this.storeChange) }
storeChange=()=>{ this.setState(store.getState()) }
|
我们已经制作了Redux中state仓库,也可以从仓库中取出数据了。接下来我们需要在控制台调试这些仓库里的数据,需要使用Redux DevTools。
- 安装 Redux DevTools 插件
- 使用Chrome浏览器安装插件,在浏览器右上角有三个点,然后点击”更多工具”,再点击”扩展程序”,再点击右侧的”打开Chrome网上商店”,然后搜索Redux DevTools,可以看到Redux DevTools插件,直接安装就可以了。
- 配置Redux Dev Tools
- 在 store 文件夹中的index.js中添加一行代码即可
1 2 3 4 5 6 7 8 9 10 11
| import { createStore } from 'redux'; import reducer from './reducer.js'
const store = createStore(reducer, window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__())
)
export default store
|
通过action改变store中的state值
为了方便大家理解,我就不将 actionType 和 actionCreater 放在单独的文件夹中。后面代码优化的时候,我会分离代码
- 完整的TodoList.js组件代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| import React, { Component } from 'react';
import { Input, Button, List } from 'antd' import store from './store/index.js'
class TodoList extends Component { constructor(props) { super(props) this.state = store.getState() store.subscribe(this.storeChange) } render() { return ( <div style={{ margin: '10px' }}> <div> <Input ref="content" placeholder='write someting' style={{ width: '250px', marginRight: '10px' }} /> {} <Button type="primary" onClick={this.addItem}>增加</Button> </div> <div style={{ margin: '10px', width: '300px' }}> <List bordered dataSource={this.state.list} renderItem={(item, index) => (<List.Item onClick={this.delItem.bind(this, index)}>{item}</List.Item>)} /> </div> </div> ); } storeChange = () => { this.setState(store.getState()) } addItem = () => { let data = this.refs.content.state.value; let action = { type: 'addItem' data } store.dispatch(action) this.refs.content.state.value = ""; } } export default TodoList;
|
- reducer.js中的代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| const defaultState = { inpVal : "say something", list : [ "早上七点起床看redux的知识", "早上八点洗漱吃饭", "早上八点半继续学习", ] }
export default (state=defaultState, action)=>{ if(action.type === "addItem"){ let newState = JSON.parse(JSON.stringify(state)) newState.list.push(action.data) return newState; } return state }
|