首页 > 解决方案 > Redux actions callback

问题描述

I am stuck in a situation wherein I call a redux action which updates state in store which in turn updates one component. I want to receive a callback when that component is updated. How can I receive that callback ?

标签: reactjsreduxredux-thunk

解决方案


如果需要接收回调,则需要使用 callbackable-actions 标准化动作。 actions-creator包具有回调能力,您可以使用它来创建具有回调能力的动作对象。

npm install actions-creator
import actionsCreator from "actions-creator"
const my_callback = () => {
    console.log("Hello, I am callback!!!")
}
const callbackable_action = actionsCreator.CALLBACKABLE.EXAMPLE(1, 2, 3, my_callback)
console.log(callbackable_action)
//      {
//          type: "CALLBACKABLE/EXAMPLE",
//          args: [1, 2, 3],
//          cb: f() my_callback,
//          _index: 1
//      }
callbackable_action.cb()
//      "Hello, I am callback!!!"

有关详细信息,请参阅:Actions Creator


推荐阅读