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

淩亂°似流年 2023-07-11 05:49 98阅读 0赞

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

  1. // views/todolist/TodoList.tsx
  2. import * as React from 'react';
  3. import { connect } from 'react-redux';
  4. import { inputValueAction, addListAction, deleteListAction, getListAction, getTodoListThunkAction } from '../../store/action';
  5. import TodoListTemplate from './TodoListTemplate';
  6. import TodoListFunction from './TodoListFunction';
  7. interface IProps {
  8. }
  9. interface IState {
  10. }
  11. // @ts-ignore
  12. @connect(state => ({ state }), { inputValueAction, addListAction, deleteListAction, getListAction, getTodoListThunkAction })
  13. export default class TodoList extends React.Component<IProps, IState> {
  14. // 获取数据
  15. componentDidMount(): void {
  16. const { getTodoListThunkAction }: any = this.props;
  17. getTodoListThunkAction();
  18. }
  19. /** * 监听 input 变化 * @param e */
  20. handleChange = (e: React.FormEvent<HTMLInputElement>) => {
  21. const { value }: any = e.target;
  22. const { inputValueAction }: any = this.props;
  23. inputValueAction(value);
  24. };
  25. /** * 添加 */
  26. handleAdd = () => {
  27. const { addListAction }: any = this.props;
  28. addListAction();
  29. };
  30. /** * 删除 * @param i */
  31. handleDelete = (i: number) => {
  32. const { deleteListAction }: any = this.props;
  33. deleteListAction(i);
  34. };
  35. public render() {
  36. const { state }: any = this.props;
  37. return (
  38. <React.Fragment>
  39. { /* 状态组件 */}
  40. <TodoListTemplate inputValue={ state.inputValue } list={ state.list } handleChange={ this.handleChange } handleAdd={ this.handleAdd } handleDelete={ this.handleDelete } />
  41. { /* 无状态组件 */}
  42. <TodoListFunction inputValue={ state.inputValue } list={ state.list } handleChange={ this.handleChange } handleAdd={ this.handleAdd } handleDelete={ this.handleDelete } />
  43. </React.Fragment>
  44. );
  45. }
  46. }
  47. // views/todolist/TodoListTemplate.tsx
  48. import * as React from 'react';
  49. import { Input, Button, List } from 'antd';
  50. import './todolist.css';
  51. interface IProps {
  52. inputValue: string,
  53. list: any,
  54. handleChange: any,
  55. handleAdd: any,
  56. handleDelete: any
  57. }
  58. interface IState {
  59. }
  60. export default class TodoListTemplate extends React.Component<IProps, IState> {
  61. public render() {
  62. const { inputValue, list, handleChange, handleAdd, handleDelete }: any = this.props;
  63. return (
  64. <React.Fragment>
  65. <div className="box-style">
  66. <Input value={ inputValue } onChange={ handleChange } className="input-style" />
  67. <Button type="primary" onClick={ handleAdd }>增加</Button>
  68. </div>
  69. <div className="box-style list-style">
  70. <List bordered dataSource={ list } renderItem={ (item: string, index: number) => (<List.Item onClick={ () => handleDelete(index) }>{ item }</List.Item>)} />
  71. </div>
  72. </React.Fragment>
  73. );
  74. }
  75. }
  76. // views/todolist/TodoListFunction.tsx
  77. import * as React from 'react';
  78. import { Input, Button, List } from 'antd';
  79. import './todolist.css';
  80. const TodoListFunction = (props) => {
  81. return (
  82. <React.Fragment>
  83. <div className="box-style">
  84. <Input value={ props.inputValue } onChange={ props.handleChange } className="input-style" />
  85. <Button type="primary" onClick={ props.handleAdd }>增加</Button>
  86. </div>
  87. <div className="box-style list-style">
  88. <List bordered dataSource={ props.list } renderItem={ (item: string, index: number) => (<List.Item onClick={ () => props.handleDelete(index) }>{ item }</List.Item>)} />
  89. </div>
  90. </React.Fragment>
  91. );
  92. };
  93. export default TodoListFunction;
  94. /* todolist.css */
  95. .box-style {
  96. margin: 20px;
  97. }
  98. .input-style {
  99. margin-right: 10px;
  100. width: 250px;
  101. }
  102. .list-style {
  103. width: 300px;
  104. }

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,98人围观)

还没有评论,来说两句吧...

相关阅读

    相关 react实现TodoList案例

    说明一下:实现这个案例需要准备的东西实在太多,不可能把所有的代码都贴上来(贴了,您也未必想看啊,哈)所以,css代码,配置文件,无关逻辑的就不往这上面贴了(想必大家既然选择做这