Comments (2)
我一直是 HOC 的忠实拥护者,在业务研发中,HOC 带来了很强的功能抽象能力,让我屡试不爽、着迷其中。不久前有人问我 HOC 有没有什么不好的地方,我回答地并不好,其实我的第一反应是没有什么不好。。。从那时我开始重新审视 HOC,我认为我看问题的角度可能出现了问题,对我一个开发 HOC 的而言,HOC 确实很出色,让我对于很多业务组件共有的功能进行了抽象统一。但对于使用和维护 HOC 而言呢?确实是痛苦的,HOC 很封闭,无法拿到 HOC 包裹组件的 ref,必须由 HOC 进行一次传递,HOC 包裹组件的 props 将变得不在可控,会新增哪些 props?props 是否会发生冲突?不得而知。JSX 中无法直接使用 HOC,多个 HOC compose 在一起,这样对于维护而言简直噩梦,理解成本也会变得很高。
回到精读文章中提到的 renderProps 模式,最近社区对于这种模式似乎十分推崇,与 HOC 相比,其开放性提升明显,原本 HOC 所做的功能抽象可通过 render Props 获取,而 render 也可以访问到父级的一切:
from weekly.
从评论里看出,renderProps
还是存在一些瑕疵:
this.props.children
不该作为函数调用。- 渲染粒度变大,表格等需要性能优化的场景不适合。
- renderProps 渲染的并不是 React 组件,无法为其单独使用
redux
,mobx
dob
等依赖收集粒度也放不下去。
renderProps 为了解耦,让控制权从上到下传递,而底层实现不需要了解上层实现,这是解决 JSX 修改组件模版问题的方法之一,作为优化点之一,可以考虑让传入的 props 自身作为一个组件:
const View = ({title}) => <div>{title}</div>
// ...
render() {
return (
<Component view={View} />
)
}
from weekly.
Related Issues (20)
- 可视化搭建 - 场景实战
- 加班中,请假一次 HOT 1
- 【自荐开源】AI可视化SolidUI HOT 2
- 精读《自由 + 磁贴混合布局》
- 请假一次 🏳️
- 精读《自由布局吸附线的实现》
- 请假一次 HOT 1
- 精读《算法题 - 通配符匹配》
- 这里我想是对应的 '*' 不匹配任何字符? HOT 1
- 精读《算法题 - 统计可以被 K 整除的下标对数目》
- 精读《算法题 - 最小覆盖子串》
- 请假一次
- 精读《算法题 - 地下城游戏》
- 请假一次 HOT 6
- 精读《VisActor 数据可视化工具》
- 精读《算法题 - 编辑距离》
- 请假一次
- 精读《算法题 - 二叉树中的最大路径和》
- 休刊一段时间 HOT 8
- 真的牛逼,竟然检测有病毒
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from weekly.