1. 常量提取

constans/ActionTypes.js

  1. export const ADD = "ADD";
  2. export const SQUARE = "SQUARE";

2. action 提取

actions/actions.js

  1. import { ADD, SQUARE } from "../constans/ActionTypes";
  2. export const addOne = {
  3. type: ADD,
  4. num: 1,
  5. };
  6. export const addTwo = {
  7. type: ADD,
  8. num: 2,
  9. };
  10. export const square = {
  11. type: SQUARE,
  12. };

修改 index.jsx 对 action 的引用

  1. import React from "react";
  2. import ReactDOM from "react-dom";
  3. import { createStore } from "redux";
  4. import { ADD, SQUARE } from "./constans/ActionTypes";
  5. import { addOne, addTwo, square } from "./actions/actions";
  6. const reducer = (state = 10, action) => {
  7. switch (action.type) {
  8. case ADD:
  9. return state + action.num;
  10. case SQUARE:
  11. return state ** 2;
  12. default:
  13. return state;
  14. }
  15. };
  16. const store = createStore(reducer);
  17. store.dispatch(addOne);
  18. console.log(store.getState());
  19. store.dispatch(addTwo);
  20. console.log(store.getState());
  21. store.dispatch(square);
  22. console.log(store.getState());
  23. function App() {
  24. return <div></div>;
  25. }
  26. ReactDOM.render(<App />, document.getElementById("root"));

使用 Action 创建函数

用来生成 action 的方法,修改 actions/actions.js

  1. import { ADD, SQUARE } from "../constans/ActionTypes";
  2. // export const addOne = {
  3. // type: ADD,
  4. // num: 1,
  5. // };
  6. // export const addTwo = {
  7. // type: ADD,
  8. // num: 2,
  9. // };
  10. // export const square = {
  11. // type: SQUARE,
  12. // };
  13. export const addAction = (num) => {
  14. return {
  15. type: ADD,
  16. num,
  17. };
  18. };
  19. export const squareAction = () => {
  20. return {
  21. type: SQUARE,
  22. };
  23. };

修改 index.jsx 对 action 的引用,改为 action 创建函数

  1. import React from "react";
  2. import ReactDOM from "react-dom";
  3. import { createStore } from "redux";
  4. import { ADD, SQUARE } from "./constans/ActionTypes";
  5. import { addAction, squareAction } from "./actions/actions";
  6. const reducer = (state = 10, action) => {
  7. switch (action.type) {
  8. case ADD:
  9. return state + action.num;
  10. case SQUARE:
  11. return state ** 2;
  12. default:
  13. return state;
  14. }
  15. };
  16. const store = createStore(reducer);
  17. store.dispatch(addAction(1));
  18. console.log(store.getState());
  19. store.dispatch(addAction(2));
  20. console.log(store.getState());
  21. store.dispatch(squareAction());
  22. console.log(store.getState());
  23. function App() {
  24. return <div></div>;
  25. }
  26. ReactDOM.render(<App />, document.getElementById("root"));

3. reducer 提取

reducers/math.js

  1. import { ADD, SQUARE } from "../constans/ActionTypes";
  2. export default (state = 10, action) => {
  3. switch (action.type) {
  4. case ADD:
  5. return state + action.num;
  6. case SQUARE:
  7. return state ** 2;
  8. default:
  9. return state;
  10. }
  11. };

修改 index.jsx

  1. import React from "react";
  2. import ReactDOM from "react-dom";
  3. import { createStore } from "redux";
  4. import { addAction, squareAction } from "./actions/actions";
  5. import math from "./reducers/math";
  6. const store = createStore(math);
  7. store.dispatch(addAction(1));
  8. console.log(store.getState());
  9. store.dispatch(addAction(2));
  10. console.log(store.getState());
  11. store.dispatch(squareAction());
  12. console.log(store.getState());
  13. function App() {
  14. return <div></div>;
  15. }
  16. ReactDOM.render(<App />, document.getElementById("root"));

最终提取效果

image.png
actions/actions.js

  1. import { ADD, SQUARE } from "../constans/ActionTypes";
  2. export const addAction = (num) => {
  3. return {
  4. type: ADD,
  5. num,
  6. };
  7. };
  8. export const squareAction = () => {
  9. return {
  10. type: SQUARE,
  11. };
  12. };

constans/ActionTypes.js

  1. export const ADD = "ADD";
  2. export const SQUARE = "SQUARE";

reducers/math.js

  1. import { ADD, SQUARE } from "../constans/ActionTypes";
  2. export default (state = 10, action) => {
  3. switch (action.type) {
  4. case ADD:
  5. return state + action.num;
  6. case SQUARE:
  7. return state ** 2;
  8. default:
  9. return state;
  10. }
  11. };

index.jsx

  1. import React from "react";
  2. import ReactDOM from "react-dom";
  3. import { createStore } from "redux";
  4. import { addAction, squareAction } from "./actions/actions";
  5. import math from "./reducers/math";
  6. const store = createStore(math);
  7. store.dispatch(addAction(1));
  8. console.log(store.getState());
  9. store.dispatch(addAction(2));
  10. console.log(store.getState());
  11. store.dispatch(squareAction());
  12. console.log(store.getState());
  13. function App() {
  14. return <div></div>;
  15. }
  16. ReactDOM.render(<App />, document.getElementById("root"));