Raghvender Kataria
Raghvender Kataria

Reputation: 1485

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 ?

Upvotes: 0

Views: 77

Answers (2)

Armen Sargsyan
Armen Sargsyan

Reputation: 11

If you need to receive a callback, You need to use callbackable-actions standardized actions. actions-creator package has the callback capability, you can use to create callback-capability action objects.

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!!!"

for more info, see: Actions Creator

Upvotes: 1

gianni
gianni

Reputation: 1357

you can pass the callback function as a parameter to your action, for example:

in your component you call your action:

yourFunction(data, callback())

and in your action:

yourFunction(data, callback){
  ... do what you have to ...
  callback()
}

Not a very clear example, but you should put your code in your question if you want t better answer.

Upvotes: 0

Related Questions