【问题标题】:Can't access action.payload of ActionCreator created using Redux Toolkit无法访问使用 Redux Toolkit 创建的 ActionCreator 的 action.payload
【发布时间】:2020-08-28 07:24:40
【问题描述】:

我似乎无法访问在 redux-observable 管道中使用 RTK 切片创建的操作的有效负载。我想知道 redux-observable 是否被设计为与 RTK 一起工作,以及我是否应该改用 typesafe-actions。

export const signUrlsEpic = (action$: Observable<Action>): Observable<Action> =>
  action$.pipe(
    ofType(actions.getPresignedUrls),
    exhaustMap((action.payload) => // <- Here: Property 'payload' does not exist on type 'Action<any>'
      from(api.getPresignedUrls(action.arguments)).pipe(
        map((response) => {
          console.log(response)
          return actions.setPresignedUrls(response)
        })
      )
    )
  )

动作是从 slice.actions 中导出的。我尝试将动作转换为 ActionCreatorWithPayload 但没有帮助。

【问题讨论】:

  • 不确定您提供的代码的语法是否正确。尝试使用exhaustMap(({payload}) =&gt;
  • 除了 action.payload 行之外,VSCode 中没有语法错误。我通过将其更改为 {payload} 得到类似的错误:属性 'payload' 在类型 'Action' 上不存在

标签: reactjs typescript redux redux-observable redux-toolkit


【解决方案1】:

其实错误很明显!有效载荷不可用的原因是因为我将类型声明为Observable&lt;Action&gt;。它应该被声明为Observable&lt;PayloadAction&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-19
    • 2021-09-29
    • 1970-01-01
    • 2023-01-18
    • 2021-09-01
    • 2020-10-08
    • 2019-01-14
    • 2023-04-09
    相关资源
    最近更新 更多