• store/index.js ```jsx import { createStore, applyMiddleware } from ‘redux’; import reducer from ‘./reducer’; import thunk from ‘redux-thunk’;

    //创建一个公共存储仓库 const store = createStore( reducer, //初始化数据 applyMiddleware(thunk)//使用redux中间件 ),

    export default store;

    1. - store/reducer.js
    2. ```jsx
    3. import { CHANGE_INPUT_VALUE, CHANGE_LIST_VALUE, DELETE_LIST_ITEM, INIT_LIST_ACTION } from './actionTypes';
    4. const defaultState = {
    5. inputValue: '',
    6. list: []
    7. }
    8. //默认数据
    9. export default (state = defaultState, action) => {
    10. //state是整个仓库存储的数据,action是store传递来的一个对象
    11. if (action.type === CHANGE_INPUT_VALUE) {
    12. return [...state, inputValue:action.value]
    13. }
    14. if (action.type === CHANGE_LIST_VALUE) {
    15. const newState = JSON.parse(JSON.stringify(state))
    16. newState.list.push(newState.inputValue)
    17. newState.inputValue = ''
    18. return newState
    19. }
    20. if (action.type === DELETE_LIST_ITEM) {
    21. const newState = JSON.parse(JSON.stringify(state))
    22. newState.list.splice(action.index, 1)
    23. return newState
    24. }
    25. if (action.type === INIT_LIST_ACTION) {
    26. return action.data
    27. }
    28. return state
    29. }
    30. //reducer可以接受state,但是绝对不能改变state
    31. //reducer.js返回一个数据和方法
    • store/actionTypes.js

      1. export const CHANGE_INPUT_VALUE = 'change_input_value';
      2. export const CHANGE_LIST_VALUE = 'change_list_value';
      3. export const DELETE_LIST_ITEM = 'delete_list_item'
      4. export const INIT_LIST_ACTION = 'init_list_action'
    • store/actionCreators.js ```jsx import { CHANGE_INPUT_VALUE, CHANGE_LIST_VALUE, DELETE_LIST_ITEM, INIT_LIST_ACTION} from ‘./actionTypes’; import axios from ‘axios’

    export const getInputChangeAction = (value) =>({ type:CHANGE_INPUT_VALUE, value:value })

    export const getButClickAction = ()=>({ type:CHANGE_LIST_VALUE, })

    export const getDeleteItem = (index) =>({ type:DELETE_LIST_ITEM, index })

    export const initListAction = (data) =>({ type:INIT_LIST_ACTION, data }) export const getTodoList = () =>({ return (dispatch)=>{ axios.get(‘/list.json’).then(res=>{ const data = res.data; const action = initListAction(data) dispatch(action) }) } })

    1. - ToDoList.js(用于做页面逻辑的处理)
    2. ```jsx
    3. import React, { Component } from 'react';
    4. import { getInputChangeAction, getButClickAction, getDeleteItem, getTodoList } from './store/actionCreators';
    5. import ToDoListUI from './ToDoListUI';
    6. import 'antd/dist/antd.css';
    7. import store from './store'
    8. class ToDoList extends Component {
    9. constructor(props) {
    10. super(props)
    11. this.state = store.getState()
    12. //.getState()是获取stroe值的一个方法
    13. this.inputChange = this.inputChange.bind(this)
    14. this.storeChange = this.storeChange.bind(this)
    15. this.butClick = this.butClick.bind(this)
    16. store.subscribe(this.storeChange)
    17. //订阅store的改变,只要store一改变,就执行StoreChange这个方法
    18. }
    19. componentDidMount(){
    20. const action = getTodoList()
    21. //使用了redux-thunk之后就可以dispatch一个函数给store了,该函数会立即自动执行并会接收一个dispatch方法
    22. store.dispatch(action)
    23. }
    24. inputChange = (e) => {
    25. //与store通信通过action
    26. const action = getInputChangeAction(e.target.value)
    27. store.dispatch(action)
    28. //把action传递给store
    29. }
    30. storeChange = () => {
    31. this.setState(store.getState())
    32. //把新的store.getState()数据赋值给state
    33. }
    34. butClick = () => {
    35. const action = getButClickAction()
    36. store.dispatch(action)
    37. }
    38. deleteItem = (index) => {
    39. const action = getDeleteItem(index)
    40. store.dispatch(action)
    41. }
    42. render() {
    43. return (
    44. <ToDoListUI
    45. inputValue={this.state.inputValue}
    46. list={this.state.list}
    47. inputChange={this.inputChange}
    48. butClick={this.butClick}
    49. deleteItem={this.deleteItem}
    50. />
    51. )
    52. }
    53. }
    54. export default ToDoList;
    • ToDoListUI.js (只用做页面渲染,不做任何逻辑处理) ```jsx import React, { Component } from ‘react’; import { Input, Button, List } from ‘antd’;

    class ToDoListUI extends Component { render() { return (

    <Input placeholder=”请输入内容” value={this.props.inputValue} style={{ width: ‘300px’, marginRight: ‘10px’ }} onChange={this.props.inputChange}

    1. ></Input>
    2. <Button type="primary" onClick={this.props.butClick}>提交</Button>
    3. <List
    4. style={{ width: '300px', marginTop: '10px' }}
    5. bordered
    6. dataSource={this.props.list}
    7. renderItem={(item, index) => (
    8. <List.Item onClick={() => this.props.deleteItem(index)}>
    9. {item}
    10. </List.Item>
    11. )}
    12. />
    13. </div>
    14. )
    15. }

    } export default ToDoListUI ```