博客
关于我
使用react-redux实现一个todolist小案例
阅读量:593 次
发布时间:2019-03-11

本文共 3851 字,大约阅读时间需要 12 分钟。

首先我们得对redux有一定的基础认识。

入口文件

import React from 'react';import ReactDOM from 'react-dom';import {   Provider} from 'react-redux'import TodoList from './TodoList'import store from './store/index'ReactDOM.render(  
, document.getElementById('root'));

1、Store核心文件(store.js)

store就是保存数据的地方,可以把它看成一个容器,整个应用只能有一个store。

Redux 提供createStore这个函数,用来生成 Store

代码块

import {    createStore, applyMiddleware } from 'redux'import thunk from 'redux-thunk'import {    composeWithDevTools } from 'redux-devtools-extension'import reducers from './reducer'const store = createStore(reducers,composeWithDevTools(applyMiddleware(thunk)) )export default store

我把actions文件中的type抽离出来写成了一个新的文件 action-types.js,用常量来定义type防止出错。

2、action-types文件

export const CHANGE_INPUTVALUE = 'change_input' //修改input框值export const DELETE_ITEM = 'delete_item' //删除export const ADD_ITEM = 'add_item' //添加

3、action文件 (actions.js)

state的变化就会导致页面视图view的改变。但是,用户接触不到vstate,只能接触到view。 所以state的改变必须是view导致的,action就是view发出的通知,表示state要发生变化了。

代码

import {       CHANGE_INPUTVALUE,    DELETE_ITEM,    ADD_ITEM} from './action-types'// export const inputChange = (e) => ({ type: CHANGE_INPUTVALUE, value: e.target.value })//定义同步actionexport const inputChange = (e) => dispatch => {       dispatch({    type: CHANGE_INPUTVALUE, value: e.target.value})    }export const clickButton = () => ({    type: ADD_ITEM })export const deleteItem = (index)=>({    type: DELETE_ITEM,index})

4、reducer文件

reducer是一个纯函数。它接受action和当前state作为参数,返回一个新的state。

代码

// reducer里只能接受state,不能改变stateimport {   combineReducers} from 'redux'import {       CHANGE_INPUTVALUE,    DELETE_ITEM,    ADD_ITEM} from './action-types'const defaultState = {       inputValue: 'write something',    list:    [        '金泰亨',        '朴智旻',        '金南俊'    ]}// 产生list状态的reducerfunction list (state = defaultState, action){       if (action.type === CHANGE_INPUTVALUE) {           let newState = JSON.parse(JSON.stringify(state))        newState.inputValue = action.value        return newState    }    if (action.type === ADD_ITEM) {           let newState = JSON.parse(JSON.stringify(state))        newState.list.push(newState.inputValue)        newState.inputValue = ''        return newState    }    if (action.type ===  DELETE_ITEM) {           let newState = JSON.parse(JSON.stringify(state))        newState.list.splice(action.index, 1)        return newState    }    return state}//向外暴露状态的结构export default combineReducers ({      list})

5、父组件TodoList.js

import React, {    Component } from 'react'import {    connect } from 'react-redux'import TodoListUI from './TodoListUI'import {   inputChange,clickButton,deleteItem} from './store/actions' class TodoList extends Component {       changeInputValue = (e) => {           // console.log(e.target.value);      this.props.inputChange(e)    }        clickBtn = () => {           this.props.clickButton()    }    deleteItem = (index) => {           this.props.deleteItem(index)    }    render() {           return (            
) }}export default connect( state =>({ list:state.list}), { inputChange,clickButton,deleteItem})(TodoList)

6、UI组件TodoListUI.js

import React, {    Component } from 'react';import 'antd/dist/antd.css'import {    Input, Button, List } from 'antd'class TodoListUI extends Component {       constructor(props) {           super(props);        this.state = {     }    }    render() {            return (            
(
{ this.props.deleteItem(index) }}> { item}
)} />
); }} export default TodoListUI;

至此,一个完整的todolist小案例就实现了。

转载地址:http://fuitz.baihongyu.com/

你可能感兴趣的文章
Mysql 数据库重置ID排序
查看>>
Mysql 数据类型一日期
查看>>
MySQL 数据类型和属性
查看>>
mysql 敲错命令 想取消怎么办?
查看>>
Mysql 整形列的字节与存储范围
查看>>
mysql 断电数据损坏,无法启动
查看>>
MySQL 日期时间类型的选择
查看>>
Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度)
查看>>
MySQL 是如何加锁的?
查看>>
MySQL 是怎样运行的 - InnoDB数据页结构
查看>>
mysql 更新子表_mysql 在update中实现子查询的方式
查看>>
MySQL 有什么优点?
查看>>
mysql 权限整理记录
查看>>
mysql 权限登录问题:ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)
查看>>
MYSQL 查看最大连接数和修改最大连接数
查看>>
MySQL 查看有哪些表
查看>>
mysql 查看锁_阿里/美团/字节面试官必问的Mysql锁机制,你真的明白吗
查看>>
MySql 查询以逗号分隔的字符串的方法(正则)
查看>>
MySQL 查询优化:提速查询效率的13大秘籍(避免使用SELECT 、分页查询的优化、合理使用连接、子查询的优化)(上)
查看>>
mysql 查询数据库所有表的字段信息
查看>>