ecmascript-6 - Javascript中连续出现许多粗箭头?
问题描述
我在我正在考虑的 NPM 库的文档中遇到了这段代码。
有人试图向我解释至少第一行在做什么?看起来这 3=>
位意味着这些函数都返回函数。
我认为自己(也许是错误的)擅长 javascript。thisis 放在一起的方式似乎过于复杂,至少对我来说很明显。我想如果我足够努力的话,如果我能多一点,但关于这件事的感觉过于混乱,所以我搬到了另一个图书馆。
第一行和分配顺序,函数开始和停止的地方对我来说是最少的。
const withTimer = timerProps => WrappedComponent => wrappedComponentProps => (
<Timer {...timerProps}>
{timerRenderProps =>
<WrappedComponent {...wrappedComponentProps} timer={timerRenderProps} />}
</Timer>
);
class TimerWrapper extends React.Component {
shouldComponentUpdate() {
return false;
}
render() {
return (
<div>
<div>Simple text</div>
<Timer.Consumer>
{() => this.props.timer.getTime()}
</Timer.Consumer>
</div>
);
}
}
const TimerHOC = withTimer({
initialTime: 5000,
})(TimerWrapper);
<TimerHOC />
解决方案
本质上是这样
function withTimer(timerProps) {
function withWrappedComponent(WrappedComponent) {
function withWrappedComponentProps(wrappedComponentProps) {
return (
<Timer {...timerProps}>
{(timerRenderProps) => (
<WrappedComponent {...wrappedComponentProps} timer={timerRenderProps} />
)}
</Timer>
);
}
return withWrappedComponentProps;
}
return withWrappedComponent;
}
如果这有帮助
或者作为两种形式之间的一种中介,就像这样
function withTimer(timerProps) {
return function (WrappedComponent) {
return function (wrappedComponentProps) {
return (
<Timer {...timerProps}>
{(timerRenderProps) => (
<WrappedComponent {...wrappedComponentProps} timer={timerRenderProps} />
)}
</Timer>
);
};
};
}
推荐阅读
- r - 如何使用 purrr::map() 使用 plotly subplot() 创建函数而不会出错?
- css - 使用 Sass 自定义标题样式
- php - 当我添加中继器时,PyroCms 抛出错误 500
- android - 如何使用两个固定视图实现“双重”折叠工具栏
- python - 以 CSV 格式输出时更改日期格式
- java - 使用多个模式拆分字符串,其中第二个模式匹配第一个模式的较小部分
- c - readelf -s 输出中的“值”字段是什么
- python - Subprocess Popen 命令运行但实际上并没有做任何事情
- javascript - 如何防止 Darkmode.js 使背景图像为负
- powershell - 在 Powershell 中搜索用户。允许重复用户的选择