I've used spread syntax before, but not in this way. I am confused over the jump between (...fns)
to (...args)
. I understand that fns
is the functions that are passed in (internalOnLoad and onLoad), and that the args
are the arguments which belongs to the corresponding function. But how would it look like when each function are passing their arguments to the function (...args) => fns.forEach(...) ?
const callAll = (...fns) => (...args) => fns.forEach(fn => fn && fn(...args));
const internalOnLoad = () => console.log("loaded");
const Avatar = ({ className, style, onLoad, ...props }) => (
<img
className={`avatar ${className}`}
style={{ borderRadius: "50%", ...style }}
onLoad={callAll(internalOnLoad, onLoad)}
{...props}
/>
);
Avatar.propTypes = {
src: PropTypes.string.isRequired,
alt: PropTypes.string.isRequired,
className: PropTypes.string,
style: PropTypes.object,
onLoad: PropTypes.func
};
Can you give me a visual description of how this would look like? E.g. that calling callAll = (...fns)
like this: callAll(internalOnLoad, onLoad)
is the same as callAll will receive the arguments like this callAll = (internalOnLoad, onLoad)
Thank you in advance