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

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

Redux Todolist 应用开发实例

Redux 是一种状态管理的库,能够帮助我们高效地管理应用程序中的数据。接下来,我们将从简单的 Redux 基础开始,逐步构建一个 Todolist 应用。


入口文件:TodoList.js

入口文件通常是应用程序的起点,以下是我们的 TodoList 组件:

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) => {    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);

Redux 核心配置:store.js

Redux 的核心是 Store。通过 createStore 函数创建一个 Redux 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;

Action 类型定义:action-types.js

为了避免硬编码,我们将所有动作类型抽离到一个独立的文件中:

export const CHANGE_INPUTVALUE = 'change_input';export const DELETE_ITEM = 'delete_item';export const ADD_ITEM = 'add_item';

Action 实现:actions.js

定义与状态交互的动作:

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

Redux 纯函数:reducer.js

Redux 的另一个核心是 Reducer。它是一个纯函数,接受当前状态和动作,返回新的状态:

import { combineReducers } from 'redux';import { CHANGE_INPUTVALUE, DELETE_ITEM, ADD_ITEM } from './action-types';const defaultState = {  inputValue: '写 something',  list: ['金泰亨', '朴智旻', '金南俊']};function list(state = defaultState, action) {  if (action.type === CHANGE_INPUTVALUE) {    const newState = { ...state };    newState.inputValue = action.value;    return newState;  }  if (action.type === ADD_ITEM) {    const newState = { ...state };    newState.list.push(newState.inputValue);    newState.inputValue = '';    return newState;  }  if (action.type === DELETE_ITEM) {    const newState = { ...state };    newState.list.splice(action.index, 1);    return newState;  }  return state;}export default combineReducers({  list: list});

UI 组件:TodoListUI.js

最后,UI 组件负责显示实际的 TodoList 界面:

import React, { Component } from 'react';import 'antd/dist/antd.css';import { Input, Button, List } from 'antd';import { ToString } from 'react-redux'; // ROI: 注意此处class TodoListUI extends Component {  render() {    const { inputavigateValue, list, changeInputValue, clickBtn, deleteItem } = this.props;    return (      
(
deleteItem(index)} key={index} > {item}
)} />
); }}export default TodoListUI;

通过以上配置,我们已经完成了一个完整的 Todolist 应用。用这个案例可以快速掌握 Redux 的基本使用方法,同时也了解如何通过组合多个 Redux 库来构建复杂的状态管理方案。

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

你可能感兴趣的文章
Numpy如何使用np.umprod重写range函数中i的python
查看>>
oauth2-shiro 添加 redis 实现版本
查看>>
OAuth2.0_JWT令牌-生成令牌和校验令牌_Spring Security OAuth2.0认证授权---springcloud工作笔记148
查看>>
OAuth2.0_JWT令牌介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记147
查看>>
OAuth2.0_介绍_Spring Security OAuth2.0认证授权---springcloud工作笔记137
查看>>
OAuth2.0_完善环境配置_把资源微服务客户端信息_授权码存入到数据库_Spring Security OAuth2.0认证授权---springcloud工作笔记149
查看>>
OAuth2.0_授权服务配置_Spring Security OAuth2.0认证授权---springcloud工作笔记140
查看>>
OAuth2.0_授权服务配置_令牌服务和令牌端点配置_Spring Security OAuth2.0认证授权---springcloud工作笔记143
查看>>
OAuth2.0_授权服务配置_客户端详情配置_Spring Security OAuth2.0认证授权---springcloud工作笔记142
查看>>
OAuth2.0_授权服务配置_密码模式及其他模式_Spring Security OAuth2.0认证授权---springcloud工作笔记145
查看>>
OAuth2.0_授权服务配置_资源服务测试_Spring Security OAuth2.0认证授权---springcloud工作笔记146
查看>>
OAuth2.0_环境介绍_授权服务和资源服务_Spring Security OAuth2.0认证授权---springcloud工作笔记138
查看>>
OAuth2.0_环境搭建_Spring Security OAuth2.0认证授权---springcloud工作笔记139
查看>>
oauth2.0协议介绍,核心概念和角色,工作流程,概念和用途
查看>>
OAuth2授权码模式详细流程(一)——站在OAuth2设计者的角度来理解code
查看>>
OAuth2:项目演示-模拟微信授权登录京东
查看>>
OA系统多少钱?OA办公系统中的价格选型
查看>>
OA系统选型:选择好的工作流引擎
查看>>
OA项目之我的会议(会议排座&送审)
查看>>
OA项目之我的会议(查询)
查看>>