【问题标题】:redux returning only initial state?redux 只返回初始状态?
【发布时间】:2021-08-17 16:33:33
【问题描述】:

不知道为什么这不起作用。 reducer 只返回初始状态值。 减速机:


import { Actions, actionTypes, todoData } from "../actions/actionTypes";

interface interfaceState {
  loading: boolean;
  todos?: todoData[];
  err?: String;
}

let initialState: interfaceState = {
  loading: false,
  todos: [],
};
export const reducer = (
  state: interfaceState = initialState,
  action: Actions
): interfaceState => {
  switch (action.type) {
    case actionTypes.FETCHING_LOADING:
      return {
        ...state,
        loading: true,
      };

    case actionTypes.FETCHING_SUCCESS:
      return {
        ...state,
        todos: action.payload,
      };
    case actionTypes.FETCHING_FAIL:
      return {
        ...state,
        err: action.payload,
      };
    default:
      return state;
  }
};

动作

import { Dispatch } from "redux";
import axios from "axios";

import { actionTypes, Actions } from "./actionTypes";
export const fetchData = () => async (dispatch: Dispatch<Actions>) => {
  dispatch({ type: actionTypes.FETCHING_LOADING });
  try {
    const data = await axios.get("http://localhost:5000/read");
    dispatch({
      type: actionTypes.FETCHING_SUCCESS,
      payload: data.data,
    });
    console.log(data);
  } catch (err) {
    dispatch({
      type: actionTypes.FETCHING_FAIL,
      payload: err,
    });
  }
};

动作类型

export enum actionTypes {
  FETCHING_LOADING = "FETCHING_LOADING",
  FETCHING_SUCCESS = "FETCHING_SUCCESS",
  FETCHING_FAIL = "FETCHING_FAIL",
}

type actionLoading = {
  type: actionTypes.FETCHING_LOADING;
};

type actionSuccess = {
  type: actionTypes.FETCHING_SUCCESS;
  payload: todoData[];
};

type actionFail = {
  type: actionTypes.FETCHING_FAIL;
  payload: string;
};

export interface todoData {
  todo_items: string;
  _id: string;
}

export type Actions = actionLoading | actionSuccess | actionFail;

【问题讨论】:

    标签: reactjs typescript redux react-redux react-typescript


    【解决方案1】:

    尝试删除“:操作”

    最终代码:

    import { Actions, actionTypes, todoData } from "../actions/actionTypes";
    
    interface interfaceState {
      loading: boolean;
      todos?: todoData[];
      err?: String;
    }
    
    let initialState: interfaceState = {
      loading: false,
      todos: [],
    };
    export const reducer = (
      state: interfaceState = initialState,
      action
    ): interfaceState => {
      switch (action.type) {
        case actionTypes.FETCHING_LOADING:
          return {
            ...state,
            loading: true,
          };
    
        case actionTypes.FETCHING_SUCCESS:
          return {
            ...state,
            todos: action.payload,
          };
        case actionTypes.FETCHING_FAIL:
          return {
            ...state,
            err: action.payload,
          };
        default:
          return state;
    

    【讨论】:

    • 动作是动作的“类型”。如果我删除 Actions,那么 typescript 会自动给我一个错误。我尝试了“action: any”,但还是不行。
    猜你喜欢
    • 2019-07-04
    • 1970-01-01
    • 2021-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多