What is "let x= something1 => something2 => something3" ?
I have this code and I'm failing to understand what does it do.
const myReducers = {person, hoursWorked};
const combineReducers = reducers => (state = {}, action) => {
return Object.keys(reducers).reduce((nextState, key) => {
nextState[key] = reducers[key](state[key], action);
return nextState;
}, {});
};
The full code incase you need:
//Redux-Style Reducer
const person = (state = {}, action) => {
switch(action.type){
case 'ADD_INFO':
return Object.assign({}, state, action.payload)
default:
return state;
}
}
const infoAction = {type: 'ADD_INFO', payload: {name: 'Brian', framework: 'Angular'}}
const anotherPersonInfo = person(undefined, infoAction);
console.log('***REDUX STYLE PERSON***: ', anotherPersonInfo);
//Add another reducer
const hoursWorked = (state = 0, action) => {
switch(action.type){
case 'ADD_HOUR':
return state + 1;
case 'SUBTRACT_HOUR':
return state - 1;
default:
return state;
}
}
//Combine Reducers Refresher
****HERE****
****HERE****
****HERE****
const myReducers = {person, hoursWorked};
const combineReducers = reducers => (state = {}, action) => {
return Object.keys(reducers).reduce((nextState, key) => {
nextState[key] = reducers[key](state[key], action);
return nextState;
}, {});
};
****
****
/*
This gets us most of the way there, but really want we want is for the value of firstState and secondState to accumulate
as actions are dispatched over time. Luckily, RxJS offers the perfect operator for this scenario., to be discussed in next lesson.
*/
const rootReducer = combineReducers(myReducers);
const firstState = rootReducer(undefined, {type: 'ADD_INFO', payload: {name: 'Brian'}});
const secondState = rootReducer({hoursWorked: 10, person: {name: 'Joe'}}, {type: 'ADD_HOUR'});
console.log('***FIRST STATE***:', firstState);
console.log('***SECOND STATE***:', secondState);
From: https://gist.github.com/btroncone/a6e4347326749f938510
let x= something1 => something2 => something3 is almost same as the following:
let x = function (something) {
return function (something2) {
return something3
}
}
The only difference is, arrows have lexical binding of this
, i.e. binding in compile time.
An arrow function is
someParameters => someExpression
So, what is
someParameters => someThing => someThingElse
???
Well, by simple stupid "pattern matching", it's an arrow function, whose body (someExpression
) is
someThing => someThingElse
In other words, it's
someParameters => someOtherParameters => someExpression
There's nothing special about this. Functions are objects, they can be returned from functions, no matter if those functions are written using arrows or the function
keyword.
The only thing you really need to know to read this properly is that the arrow is right-associative, IOW that
a => b => c === a => (b => c)
Note: An arrow function can also have a body consisting of statements as well as a single expression. I was specifically referring to the form the OP is confused about.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With