reactjs - 如何在 redux-toolkit 中正确使用带有元类型的 PayloadAction?

标签 reactjs typescript redux react-redux redux-toolkit

简化示例

import { createSlice, PayloadAction } from '@reduxjs/toolkit';

    type Cake = {
      flavor: string;
      size: 'S' | 'M' | 'L';
    };

const initialState: { all: Cake[]; meta: { currentPage: number } } = {
  all: [],
  meta: {
    currentPage: 0,
  },
};

const cakeSlice = createSlice({
  name: 'cake',
  initialState,
  reducers: {
    fetchAll(
      state,
      action: PayloadAction<Cake[], string, { currentPage: number }>,
    ) {
      state.all = action.payload;
      state.meta = action.meta;
    },
  },
});

export default cakeSlice;


我收到这些错误。
Type '
(state: {
    all: {
        flavor: string;size: "S" | "M" | "L";
    } [];meta: {
        currentPage: number;
    };
}, action: PayloadAction < Cake[], string, {
    currentPage: number;
}, never > ) => void ' is not assignable to type '
CaseReducer < {
    all: Cake[];meta: {
        currentPage: number;
    };
}, {
    payload: any;type: string;
} > | CaseReducerWithPrepare < {
    all: Cake[];meta: {
        currentPage: number;
    };
}, {
    payload: any;type: string;
} > '
Type '
(state: {
    all: {
        flavor: string;size: "S" | "M" | "L";
    } [];meta: {
        currentPage: number;
    };
}, action: PayloadAction < Cake[], string, {
    currentPage: number;
}, never > ) => void '
is not assignable to type 'CaseReducer<{ all: Cake[]; meta: { currentPage: number; }; }, { payload: any; type: string; }>'.
Types of parameters 'action'
and 'action'
are incompatible.
Type '{ payload: any; type: string; }'
is not assignable to type 'PayloadAction<Cake[], string, { currentPage: number; }, never>'.
Property 'meta'
is missing in type '{ payload: any; type: string; }'
but required in type '{ meta: { currentPage: number; }; }
'.

最佳答案

RTK 的默认行为是创建一个只有一个参数和一个有效载荷属性的 Action 创建者。 TS 注释无法修改此行为,因为 TS 注释对运行时行为没有影响。

您可以使用 the prepare notation定义一个覆盖此默认行为的函数。
此函数具有与 prepare argument for createAction 相同的签名和行为,因此您可以在那里找到大部分相关文档。

在您的特定情况下,您会想要一些类似的东西

const cakeSlice = createSlice({
  name: 'cake',
  initialState,
  reducers: {
    fetchAll: {
      reducer(
        state,
        action: PayloadAction<Cake[], string, { currentPage: number }>
      ) {
        state.all = action.payload
        state.meta = action.meta
      },
      prepare(payload: Cake[], currentPage: number) {
        return { payload, meta: { currentPage } }
      }
    }
  }
})


这是一个 typescript playground

关于reactjs - 如何在 redux-toolkit 中正确使用带有元类型的 PayloadAction?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60320668/

相关文章:

javascript - 使用 concat 更新 react 状态下的数组

javascript - TypeScript 中的 Azure Blob 触发器在部署中不会触发

react-native - 不弹出 Redux 的测试 Jest React-Native Expo CRNA

javascript - 如果我真的必须的话,如何在react-redux状态下存储不可序列化的变量而不破坏它(例如通过WebAPI保存设备连接)

javascript - 使用 Constant 组件在 React JS 中设置主要组件的样式

javascript - ReactJS html渲染为字符串而不是html

reactjs - React TS useContext useReducer 钩子(Hook)

javascript - 如何从对象重命名字段

typescript - ts-node 忽略 d.ts 文件,而 tsc 成功编译项目

javascript - 优化所需文件夹内的图像