I am confused by the following code.
Sourceexport const createProject = (project) => {
return (dispatch, getState, {getFirestore}) => {
// make async call to database
const firestore = getFirestore();
firestore.collection('projects').add({
...project,
authorFirstName: 'Net',
authorLastName: 'Ninja',
authorId: 12345,
createdAt: new Date()
}).then(() => {
dispatch({ type: 'CREATE_PROJECT_SUCCESS' });
}).catch(err => {
dispatch({ type: 'CREATE_PROJECT_ERROR' }, err);
});
}
};
My question is about this line.
return (dispatch, getState, {getFirestore}) => {...
What makes this work? Where do the arguments come from? What calls them? Aren't all these arguments already in scope? Why do we need createProject to return a second function? Is createProject returning a second function? Or is it immediately invoking an inline function? What triggers the return function to run?
I'm just generally very confused by this pattern. Can someone please break it down for me?
createProjectis expecting a function returned and will call the returned function with thedispatch, getState, {getFirestore}arguments.createProjectlooks like a function factory (ish).f(a, b, c)into being evaluated into steps where each satisfies the parameter. Some implementations do only allow you to turn the steps intof(a) -> g(b) -> h(c)while others make it also possible to go withf(a) -> g(b, c). Besides, I can't really find a better duplicate than the concept of currying. Surely once one has that down, the arity of the functions is irrelevant to the result.