I am quite new to JavaScript and react-native and I have existing project that I need to add functionality to. It is using redux
and redux-thunk
with redux-saga
to send API requests. Currently it supports only 1 dispatch
function per component and I need to dispatch
several types of requests to the saga. I am trying to bindActionCreators
to add the dispatch
to the stores but to no avail.. I am totally lost on the mapDispatchToProps
part and how do I "fire the action" afterwards..
in single dispatch to props, I did this:
let sdtp = (arg) => {
return (dispatch) => {
dispatch({
type: 'GET_TEST_HASHMAP_SAGA',
hashmap: arg
})
}
}
export default MainPage = connect(
mapStateToProps,
{ sdtp }
)(MainPage);
and I can "access the function" (is this the right term? at least my saga gets called) inside the MainPage.render()
component :
`this.props.sdtp({'hello':'world'});`
but when I change to use bindActionCreators
, I cannot access it in the props anymore (I have tried so many different experiments I almost give up)
Here is how I construct my multiple dispatches:
let action1 = (args) => {
return (dispatch) => {
dispatch({
type: 'GET_TEST_HASHMAP_SAGA',
hashmap: arg
});
}
}
let action2 = (args) => {
return (dispatch) => {
dispatch({
type: 'GET_TEST_HASHMAP_SAGA2',
params: arg
});
}
}
let action3 = (args) => {
return (dispatch) => {
dispatch({
type: 'GET_TEST_HASHMAP_SAGA3',
args: arg
});
}
}
let mdtp = (dispatch) => {
return {
actions: bindActionCreators(action1, action2, action3, dispatch)
}
}
export default MainPage = connect(
mapStateToProps,
{ mdtp }
)(MainPage);
I am trying to access the actions
like this:
this.props.mdtp.action1({arg: 'hello'});
Thanks in advance!
bindActionCreators
and I thought it is the only way, lol.. btw, I tried yourbindActionCreators
way, it doesn't work, the one from @magneticz which return the result ofbindActionCreators
directly works though – Announce