javascript - 我应该在反应中使用带有 HOC 的柯里化吗?
问题描述
进行柯里化是性能较差还是不好的做法?是更好的表现还是更好的实践?或者这一切都只是偏好。我真的找不到关于这个问题的任何具体证据。
class MyClass extends React.component {
...
}
export default HOC(someParam)(MyClass)
对比:
class MyClass extends React.component {
...
}
export default HOC(someParam, MyClass)
示例 HOC(柯里化):
export default function HOC(someParam){
// do something with someParam here
return function(ChildComponent){
return (props) => ( <ChildComponent {...props}/> )
}
}
示例 HOC(不柯里化):
export default function HOC(someParam, childComponent){
// do something with someParam here
return (props) => ( <ChildComponent {...props}/> )
}
解决方案
在性能方面,任何差异都将微不足道,以至于无关紧要。创建函数需要几纳秒,而且你每堂课只需要做一次。
因此,决定应该归结为您是否从中获得好处。如果您使用大量高阶组件,您可能会发现使用组合将多个 HOC 组合在一起很有用。如果是这种情况,则柯里化将非常有用,因为组合通常要求函数是一元的。例如:
class MyClass extends React.Component {
// whatever
}
export default compose(
withI18n,
withTheme,
connect(state => ({ foo: state.foo }),
HOC("someValue") // <-- this would be from your curried example
)(MyClass)
// Without composition, this would be the following... i think.
withI18n(withTheme(connect(state => ({ foo: state.foo }))(HOC("someValue", MyClass))));
推荐阅读
- javascript - 如何通过 @input 属性应用样式,我想增加 mat-autocomplete 的宽度
- caffe - TypeError: self._open() got an unexpected keyword argument 'as_grey' python
- angular - 没有请求返回值时,RxJS 超时无错误
- javascript - 使用 Papa 解析器进行解析时更改列名
- mongodb - 使用高山映像在 docker 中运行代码时找不到 mongoimport 命令
- laravel - Carbon 自动在解析时将时间增加 11 分钟
- list - 浏览 Haskell 中的列表拆分功能
- ios - 当应用程序被杀死时,iOS 13 会跟踪用户位置
- c# - C# 更改不同 winform 类中的变量
- python - 如何使用 pandas 加速加载数据?