【问题标题】:Can we use redux actions inside a function that is not a functional component or a class component in reactJS我们可以在不是函数组件或 reactJS 中的类组件的函数中使用 redux 操作吗
【发布时间】:2020-04-26 17:26:09
【问题描述】:

您好,我想知道是否以及如何使用和实现不在类或功能组件内的 redux 操作。

我想实现这样的东西。

import { setPerson } from ./actions.js

export const registerPerson = (data) => {
  /* here we do whatever we need to set register a person */

   ... 
   .then((res) => {
     setPerson(res.data). <--- Here is the instance that I want to do.
   }).catch(err => console.warn(err);

}

我们的目标是将 registerPerson 函数导入到 React 组件中,redux 操作将在导入的函数中触发。

【问题讨论】:

  • 是的..可能。您可以将要调度的引用存储在全局 obj 或 Class 上作为静态属性,并在您想要的任何地方使用它。但除非您别无选择,否则不推荐。
  • 好的方法是将调度作为参数从调用此registerPerson 的组件或函数传递,然后使用该参数调度它。

标签: javascript reactjs redux react-redux redux-actions


【解决方案1】:

是的,您可以在 then 方法中调用您的操作来请求后端。但请确保您应该派遣您的行动。

【讨论】:

  • 派遣是什么意思? @瓦伦丁
  • @jaffer_syed disptach 是一个用于运行你的动作的函数,以便 reducer 可以根据你的动作更新存储
猜你喜欢
  • 2016-08-03
  • 1970-01-01
  • 1970-01-01
  • 2021-05-16
  • 2017-09-21
  • 1970-01-01
  • 2018-08-07
  • 2019-10-28
相关资源
最近更新 更多