一、index.js

  1. import React from 'react'
  2. import ReactDOM from 'react-dom'
  3. import TodoList from './TodoList'
  4. import { Provider } from 'react-redux'
  5. import store from './store'
  6. // Provider(生产者)把store提供给内部所有组件
  7. const App = () => {
  8. <Provider store={store}>
  9. <TodoList />
  10. </Provider>
  11. }
  12. ReactDOM.render(App, document.getElementById('root'))

二、TodoList.js

  1. import React, {Component} from 'react'
  2. import { connect } from 'react-redux'
  3. import {
  4. getInputChangeAction,
  5. getButClickAction,
  6. getDeleteItem,
  7. getTodoList,
  8. getInitList
  9. } from './store/actionCreators';
  10. // 此时TodoList只是一个UI组件,同时也是一个无状态组件,只负责页面的渲染,有效提升代码性能
  11. const TodoList = (props) => {
  12. render(){
  13. const { inputValue,
  14. changeInputValue,
  15. hendleClick,
  16. handleDelete,
  17. } = props
  18. return(
  19. <div>
  20. <div>
  21. <input
  22. value={inputValue}
  23. onChange={changeInputValue}
  24. />
  25. <button onClick={hendleClick}>提交</button>
  26. </div>
  27. <ul>
  28. {
  29. this.props.list.map((item,index)=>{
  30. <li
  31. key={index}
  32. onClick={handleDelete(index)}
  33. >
  34. {item}
  35. </li>
  36. })
  37. }
  38. </ul>
  39. </div>
  40. )
  41. }
  42. }
  43. // 让ToDoList和store进行连接
  44. // 第一个参数意思:把store里面的数据映射给组件的props
  45. const mapStateToProps = (state) => {
  46. return {
  47. inputValue:state.inputValue,
  48. list:state.list
  49. }
  50. }
  51. // 第二个参数意思: 把store中的dispatch方法挂载到props上
  52. const mapDispatchProps= (dispatch) => {
  53. return {
  54. changeInputValue(e){
  55. const action = getInputChangeAction(e.target.value)
  56. dispatch(action)
  57. },
  58. hendleClick(){
  59. const action = getButClickAction()
  60. dispatch(action)
  61. },
  62. handleDelete(){
  63. const action = getDeleteItem(index)
  64. dispatch(action)
  65. }
  66. }
  67. }
  68. //数据变页面就会跟着变,不用再做订阅
  69. export default connect(mapStateToProps, mapDispatchProps)(ToDoList);

三、store/index.js

  1. import { createStore } from 'redux';
  2. import reducer from './reducer';
  3. //创建一个公共存储仓库
  4. const store = createStore(reducer),
  5. export default store;

四、store/reducer.js

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

五、store/actionCreators.js

  1. import {
  2. CHANGE_INPUT_VALUE,
  3. CHANGE_LIST_VALUE,
  4. DELETE_LIST_ITEM,
  5. } from './actionTypes';
  6. import axios from 'axios'
  7. export const getInputChangeAction = (value) =>({
  8. type:CHANGE_INPUT_VALUE,
  9. value:value
  10. })
  11. export const getButClickAction = ()=>({
  12. type:CHANGE_LIST_VALUE,
  13. })
  14. export const getDeleteItem = (index) =>({
  15. type:DELETE_LIST_ITEM,
  16. index
  17. })

六、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'