Answer the question
In order to leave comments, you need to log in
Three questions about Redux-React code?
There is a Redux application. I just started learning Redux, so I still don't understand everything. I have three short questions about this code.
I commented out the lines that have questions:
This is the reducer:
import { searchFilter } from "../containers/app";
export function reducer(state = {}, action) {
switch (action.type) {
case "SET_SHIFT":
return Object.assign({}, state, {
shift: action.shift
});
case "SET_SEARCH":
return Object.assign({}, state, {
search: action.search.toLowerCase()
});
case "RUN_FILTER":
var newData = state.data[action.shift || state.shift].filter(x => {
return (
x["planeTypeID.code"].toLowerCase().includes(action.search || state.search)
);
});
return Object.assign({}, state, {
shift: action.shift || state.shift, // строка1
search: action.search || state.search, // строка2
filteredData: searchFilter(state.search, newData)
});
case "LOAD_DATA_START":
return Object.assign({}, state, {
day: action.day
});
case "LOAD_DATA_END":
var newData = action.payload.data[state.shift].filter(x => {
return (
x["planeTypeID.code"] &&
x["planeTypeID.code"].toLowerCase().includes(action.search || state.search) //строка3
);
});
return Object.assign({}, state, {
data: action.payload.data,
shift: Object.keys(action.payload.data)[0],
filteredData: searchFilter(state.search, newData) //строка4
});
default:
return state;
}
}
action.shift || state.shift
? not just action.shift
: Line 2 is the same: why do we write action.search || state.search
, and not just: action.search
? x["planeTypeID.code"] && x["planeTypeID.code"].toLowerCase().includes(action.search || state.search)
x["planeTypeID.code"].toLowerCase().includes(action.search || state.search)
? planeTypeID.code
is a property of the API object. searchFilter
is it a function call? And why are we passing such parameters (state.search, newData)
to this call? The third question may be difficult (or maybe not) due to lack of information, then answer at least the first two questions
Answer the question
In order to leave comments, you need to log in
1. action.shift || state.shift
this is for default values. Or if action.shift
it didn’t come, then they leave the old one which is now in state.shift
so as not to overwrite it with undefined.
2. Same story. If not present, then false is returned. If there is a value, then methods begin to be applied to it, and so on. And finally a string is returned.
3. This is some kind of custom function. It just does some sort of transformation. x["planeTypeID.code"]
.toLowerCase
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question