14. react 基础 redux 的编写 TodoList 功能
1. 安装 redux 监听工具 ( 需要翻墙 )
打开 谷歌商店
搜索 redux devtool
安装第一个即可
2. 安装 redux
yarn add redux
3. 创建 一个 store 文件夹( 与 项目 index.js 文件 同级 ) 并在 store 内 创建一个 reducer.js 文件 用于 区分各组件状态
# store/reducer.js
// 初始化 各组件的状态
const defaultState = {
inputValue : '', // 初始化 TodoList 的输入框的值
list : [] // 初始化 TodoList 列表的值
};
export default (state = defaultState, action)=>{
return state;
}
4. 在 store 文件夹内 创建 index.js 创建一个公共存储仓库, 将组件的各个状态作为参数传入
#store/index.js
import {createStore} from 'redux';
import reducer from './reducer'
const store = createStore(reducer);
export default store;
5. TodoList 内使用 store 进行数据存储
引入 store.js ( index.js 和 store 文件夹 是在同一级 )
import store from './store';
在 constructor 方法内 引入 store 的 state
constructor(props){
super(props);
this.state = store.getState(); // 当前 this.state 即可获得 store的 state 的内容
}
6. 当输入框改变时 改变 store 内的 inputValue 的数据
Input 添加 onChange 事件
# TodoList.js
<Input onChange={this.inputChange.bind(this)} />
inputChange(e){
// 调用 store.dispatch( action ) 方法 通知 store 变更 inputValue
const action = {
type : 'change_input_value',
value : e.target.value
};
store.dispatch( action );
}
# store 将 接收到的 state 和 action 转发给 reducer
# store/reducer.js
// reducer 可以接收 state 但是绝不可以修改 state
export default (state, action)=>{
// 打印 reducer 接收到的 state 和 action
console.log( state, action );
// 对 state 进行深复制 为 newState , 修改 newState 的值 并返回 newState
if( action.type === 'change_input_value' ){
const newState = JSON.parse( JSON.stringify( state ) );
newState.inputValue = action.value;
return newState;
}
return state;
}
# 当 store 返回数据发生了变化 需要 TodoList 订阅 store 数据变化 并进行 state 的 赋值
# TodoList.js
import store from './store'
constructor(){
// 订阅 store 的变化 并 编写 监听变化的 函数
store.subscribe(this.handStoreChange);
this.handStoreChange = this.handStoreChange.bind(this);
}
// 将 store 的值 重新 赋值给组件
handStoreChange(){
this.setState( store.getState() );
}
7. 同理 编写 列表 和 删除功能
#index.js
import React from 'react';
import ReactDOM from 'react-dom';
import TodoList from './TodoList';
ReactDOM.render(<TodoList />, document.getElementById('root'));
#TodoList.js
import React, { Component } from 'react';
import 'antd/dist/antd.css';
import { Input, Button, List } from 'antd';
import store from './store';
class TodoList extends Component{
constructor(props){
super(props);
this.state = store.getState();
this.inputValueChange = this.inputValueChange.bind(this);
this.addItem = this.addItem.bind(this);
this.delItem = this.delItem.bind(this);
this.handleStoreChange = this.handleStoreChange.bind(this);
store.subscribe(this.handleStoreChange);
}
render(){
return (
<div style={{ marginTop: '10px', marginLeft: '10px' }}>
<div>
<Input placeholder="请输入" value={this.state.inputValue} onChange={this.inputValueChange} style={{marginRight: '10px' , width: '300px'}} />
<Button type="primary" onClick={this.addItem}>提交</Button>
</div>
<div>
<List
style={{width: '300px', marginTop: '10px'}}
bordered
dataSource={this.state.list}
renderItem={(item, val)=> <List.Item index={val} onClick={this.delItem}>{item}</List.Item>}
/>
</div>
</div>
);
}
handleStoreChange(){
this.setState(store.getState());
}
addItem(){
const action = {
type: 'add_item',
value: this.state.inputValue
}
store.dispatch(action);
}
delItem(e){
const action = {
type : 'del_item',
value : e.target.getAttribute('index')
}
store.dispatch(action);
}
inputValueChange(e){
const action = {
type : 'input_value_change',
value : e.target.value
}
store.dispatch(action);
}
}
export default TodoList;
#store/index.js
import {createStore} from 'redux';
import reducer from './reducer';
const store = createStore(reducer);
export default store;
#store/reducer.js
const defaultState = {
inputValue : '',
list : []
}
export default (status = defaultState, action)=>{
console.log(status, action)
if(action.type === 'input_value_change'){
const newState = JSON.parse(JSON.stringify(status));
newState.inputValue = action.value;
return newState;
}
if(action.type === 'add_item'){
const newState = JSON.parse(JSON.stringify(status));
newState.list = [...newState.list, action.value];
newState.inputValue = '';
return newState;
}
if(action.type === 'del_item'){
const newState = JSON.parse(JSON.stringify(status));
newState.list.splice(action.value, 1);
return newState;
}
return status;
}
14. react 基础 redux 的编写 TodoList 功能的更多相关文章
- 4. react 基础 - 编写 todoList 功能
编写 TodoList 功能 react 入口 js #src/index.js import React from 'react'; import ReactDOM from 'react-dom' ...
- 13. react 基础 redux 的基本介绍 及 用 antd 编写 TodoList 的样式
1. redux 简述 当 store 内的 数据进行变更的时候 多个组件感知到 store 内的数据变化 将会被自动更新 2. redux 工作流 Store 代表数据存储 (例如: 图书馆管理 ...
- react native redux saga增加日志功能
redux-logger地址:https://github.com/evgenyrodionov/redux-logger 目前Reac native项目中已经使用redux功能,异步中间件使用red ...
- react用redux 做的todolist
### 1. 创建项目 create - react - app 项目名(shop) ### 2. 进入项目,下载redux cnpm install redux --save ### 3. ...
- (三)React基础
3-1 使用React编写TodoList功能 import { Fragment} from ‘react’ Fragment是占位符 用于替代最外层div元素, 防止生成的元素会有两层div嵌套这 ...
- 18 react react-redux 的编写 TodoList
1. 安装 react-redux yarn add react-redux 2. react-redux 编写 TodoList 使所有子组件 都能使用 store #index.js import ...
- 从0到1用react+antd+redux搭建一个开箱即用的企业级管理后台系列(基础篇)
背景 最近因为要做一个新的管理后台项目,新公司大部分是用vue写的,技术栈这块也是想切到react上面来,所以,这次从0到1重新搭建一个react项目架子,需要考虑的东西的很多,包括目录结构.代码 ...
- 最新的chart 聊天功能( webpack2 + react + router + redux + scss + nodejs + express + mysql + es6/7)
请表明转载链接: 我是一个喜欢捣腾的人,没事总喜欢学点新东西,可能现在用不到,但是不保证下一刻用不到. 我一直从事的是依赖angular.js 的web开发,但是我怎么能一直用它呢?看看最近火的一塌糊 ...
- 详解 Node + Redux + MongoDB 实现 Todolist
前言 为什么要使用 Redux? 组件化的开发思想解放了繁琐低效的 DOM 操作,以 React 来说,一切皆为状态,通过状态可以控制视图的变化,然后随着应用项目的规模的不断扩大和应用功能的不断丰富, ...
随机推荐
- Vue - 定义使用组件
import Card from './components/Card.vue' Vue.component('m-card',Card) // component是注册全局组件,在实例化VUE前 ...
- shell-Startup-Files
shell-Startup-Files 1. 相关阅读 2. 主流shell 3. shell实例类型 4. Shell启动文件的必要元素 4.1 路径: 命令路径, 4.2 提示符 5. 主流she ...
- 项目启动报错:Communications link failure
2017-12-29 10:43:19,776 ERROR [com.alibaba.druid.pool.DruidDataSource] - <init datasource error, ...
- c# copydata 消息
using PublicCode; using System; using System.Collections.Generic; using System.ComponentModel; using ...
- Django(六)实战2:向数据库添加,删除数据、重定向写法、重定向简写
一.向数据库添加图书数据 [上接]https://blog.csdn.net/u010132177/article/details/103831173 1)首先开启mysql服务,并运行项目 启动my ...
- POJ 3614:Sunscreen 贪心+优先队列
Sunscreen Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5378 Accepted: 1864 Descrip ...
- windows 禁用中文输入法(转)
源博客地址:http://blog.csdn.net/xie1xiao1jun/article/details/17913967 windows 程序禁用中文输入法方法:1.添加windows头文件及 ...
- Rails 第一课:环境配置 Ruby Rails RVM Heroku
安装 上传专案到 Heroku 安装 Ruby 2.3.1 Rails 5.0.0.1 RVM 1.27.0 比较幸运一个问题都未碰到 MacOS 10.12.1 配置完成系统顺利升级到10.12.2 ...
- 01-JAVA语言基础——课后动手动脑
1.一个java类文件中真的只能有一个公有类吗? 请使用Eclipse或javac检测一下以下代码,有错吗? public class Test{ public static void main ...
- 第四篇:Vue的项目开发
安装Vue的脚手架cli环境 1)官网下载并安装node,附带npm https://nodejs.org/zh-cn/ node环境: 可以解释执行js语言 提供了npm应用商城,可以为node环境 ...