Context: I want to have an action that calls two other actions, so I made an effect. Within this effect, I need to access the rootState. I read that the second argument of an effect must be the rootState, but if there are no arguments, the rootState is still passed to the effect.
This works and will print out rootState (however I have no need for a payload):
effects: {
async doThisAndThat(payload, rootState) {
console.log(rootState); // prints out rootState just fine
rootState.doThis();
rootState.doThat();
},
}
This prints undefined for this.state:
effects: {
async doThisAndThat() {
console.log(this.state); // prints undefined
this.state.doThis();
this.state.doThat();
},
}
ALSO: it is possible that this is difficult because redux is not designed to be used in this way. If so, I am happy to do it another way. Any insights appreciated!
CodePudding user response:
What they mean is that even if your function has no arguments, it will still be called with arguments. I'd imagine internally, redux has something like this:
let rootState = { user: '[email protected]' };
const callAction = (fn) => (payload) => {
fn(payload, rootState)
}
function doThisAndThat() {
const rootState = arguments[1];
console.log('doThisAndThat', {rootState});
}
callAction(doThisAndThat)({ payload: 'ignore this' });
Where you can see doThisAndThat
takes no arguments, but it is called with arguments, which you can still access by using the arguments
magic variable.
All that being said, I'd just do
async doThisAndThat(_, rootState) {
to make it clear you don't care about the payload