result from oneAction
{type: "ONE_ACTION", payload: "(value from oneAction some useless value)"}
result from otherAction
{type: "OTHER_ACTION", payload: "(value from otherAction (value from oneAction some useless value))"}
Last active
February 3, 2018 00:45
-
-
Save thiagoh/87e46855fbe8bf57aaab615cfb93d2a1 to your computer and use it in GitHub Desktop.
redux asynchronous actions dispatching one after the other
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// oneAction could be setCountry | |
export const oneAction = params => { | |
return { | |
type: "ONE_ACTION", | |
payload: Promise.resolve(`(value from oneAction ${params})`) | |
}; | |
}; | |
// otherAction could be fetchRegions | |
export const otherAction = params => { | |
return { | |
type: "OTHER_ACTION", | |
payload: Promise.resolve(`(value from otherAction ${params})`) | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from "react"; | |
import { connect } from "react-redux"; | |
import { oneAction, otherAction } from "../actions"; | |
export class AppImpl extends Component { | |
handleSomething(someValue) { | |
this.props.oneAction(someValue).then( | |
oneActionResult => { | |
console.log("result from oneAction\n", oneActionResult); | |
return this.props | |
.otherAction(oneActionResult.payload) | |
.then(otherActionResult => { | |
console.log("result from otherAction\n", otherActionResult); | |
}); | |
}, | |
error => { | |
// handle error | |
} | |
); | |
} | |
render() { | |
return ( | |
<div> | |
<h3>Asynchronouse Actions dispatch</h3> | |
<button | |
onClick={() => this.handleSomething.bind(this)("some useless value")} | |
> | |
Click me | |
</button> | |
</div> | |
); | |
} | |
} | |
export const App = connect( | |
state => ({ | |
state | |
}), | |
{ oneAction, otherAction } | |
)(AppImpl); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment