React redux配置 使用react-redux redux-saga 实现todolist (二)

使用 redux 进行统一状态管理,实现 todolist 功能

// views/todolist/TodoList.tsx
import * as React from 'react';
import { connect } from 'react-redux';
import { inputValueAction, addListAction, deleteListAction, getListAction, getTodoListThunkAction, getTodoListSagaAction } from '../../store/action';
import TodoListTemplate from './TodoListTemplate';
import TodoListFunction from './TodoListFunction';

interface IProps {
}

interface IState {
}

// @ts-ignore
@connect(state => ({ state }), { inputValueAction, addListAction, deleteListAction, getListAction, getTodoListThunkAction, getTodoListSagaAction })
export default class TodoList extends React.Component<IProps, IState> {

  // 获取数据
  componentDidMount(): void {
    const { getTodoListSagaAction }: any = this.props;
    getTodoListSagaAction();
  }

  /**
   * 监听 input 变化
   * @param e
   */
  handleChange = (e: React.FormEvent<HTMLInputElement>) => {
    const { value }: any = e.target;
    const { inputValueAction }: any = this.props;
    inputValueAction(value);
  };

  /**
   * 添加
   */
  handleAdd = () => {
    const { addListAction }: any = this.props;
    addListAction();
  };

  /**
   * 删除
   * @param i
   */
  handleDelete = (i: number) => {
    const { deleteListAction }: any = this.props;
    deleteListAction(i);
  };

  public render() {
    const { state }: any = this.props;
    return (
      <React.Fragment>
        {/* 状态组件 */}
        <TodoListTemplate inputValue={ state.inputValue } list={ state.list } handleChange={ this.handleChange } handleAdd={ this.handleAdd } handleDelete={ this.handleDelete } />
        {/* 无状态组件 */}
        <TodoListFunction inputValue={ state.inputValue } list={ state.list } handleChange={ this.handleChange } handleAdd={ this.handleAdd } handleDelete={ this.handleDelete } />
      </React.Fragment>
    );
  }
}

// views/todolist/TodoListTemplate.tsx
import * as React from 'react';
import { Input, Button, List } from 'antd';
import './todolist.css';

interface IProps {
  inputValue: string,
  list: any,
  handleChange: any,
  handleAdd: any,
  handleDelete: any
}

interface IState {
}

export default class TodoListTemplate extends React.Component<IProps, IState> {

  public render() {
    const { inputValue, list, handleChange, handleAdd, handleDelete }: any = this.props;
    return (
      <React.Fragment>
        <div className="box-style">
          <Input value={ inputValue } onChange={ handleChange } className="input-style" />
          <Button type="primary" onClick={ handleAdd }>增加</Button>
        </div>
        <div className="box-style list-style">
          <List bordered dataSource={ list } renderItem={(item: string, index: number) => (<List.Item onClick={ () => handleDelete(index) }>{ item }</List.Item>)} />
        </div>
      </React.Fragment>
    );
  }
}
// views/todolist/TodoListFunction.tsx
import * as React from 'react';
import { Input, Button, List } from 'antd';
import './todolist.css';

const TodoListFunction = (props) => {
  return (
    <React.Fragment>
      <div className="box-style">
        <Input value={ props.inputValue } onChange={ props.handleChange } className="input-style" />
        <Button type="primary" onClick={ props.handleAdd }>增加</Button>
      </div>
      <div className="box-style list-style">
        <List bordered dataSource={ props.list } renderItem={(item: string, index: number) => (<List.Item onClick={ () => props.handleDelete(index) }>{ item }</List.Item>)} />
      </div>
    </React.Fragment>
  );
};

export default TodoListFunction;
/* todolist.css */
.box-style {
  margin: 20px;
}
.input-style {
  margin-right: 10px;
  width: 250px;
}
.list-style {
  width: 300px;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值