I'm new to Flux and I wonder why argument deconstruction is so frequent. It creates a lot of noise for beginner, so I hope it's for a reason.
For example, something like this seems excessive at first glance:
function incrementIfOdd() {
return (
dispatch: (action: actionType) => void,
getState: () => counterStateType
) => {
const { counter } = getState();
if (counter % 2 === 0) {
return;
}
dispatch(increment());
};
}
Also: How should I know if I should return an object or anonymous function like in code above?
Found at: https://github.com/chentsulin/electron-react-boilerplate/blob/master/app/actions/counter.js#L23
I used babeljs.io/repl/ to "translate" it in order to understand what this does .
None of the syntax in there is Flux (or Redux) specific:
dispatch
and getState
are type notations, presumably either for Flow or TypeScript const { counter }
usage is an ES6 "destructuring assignment", and is a shorter way of saying const counter = getState().counter
With Redux, it's common to write "action creator" functions. Normal action creators return an action (a plain object with a type
field). It's also common to write "thunk action creators", which return a function that can contain asynchronous logic or access the store like in the example you pasted.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.