如何评价 Vue 的 Function-based Component?
作者:匿名用户
链接:https://www.zhihu.com/question/325397290/answer/708418099
来源:知乎
事实性错误:
那 vue 呢?它连 HOC 都没有,render props 更不现实(jsx自带)
- HOC
const DefaultButton = {
props: {
text: String
},
template: `<button>{{text}}</button>`
}
function wrap(comp) {
return {
components: {
comp
},
template: `<comp text=""/>`
}
}
new Vue({
components: {
TextButton: wrap(DefaultButton)
},
template: `<text-button />`
})
2. HOC + render props
const DefaultButton = {
props: {
renderText: Function
},
render(h) {
return h('button', this.renderText())
}
}
function wrap(comp) {
return {
render(h) {
return h(comp, {
attrs: {
renderText: () => ""
}
})
}
}
}
const textButton = wrap(DefaultButton)
new Vue({
render(h) {
return h(textButton)
}
})
react 的不可变,纯函数。直接导致 hooks 必须使用 const 关键字,不能是 let,这也是 hooks 的奇迹之一
const keyword 和 "不可变,纯函数" 有什么关系, 若使用 let、var, 是否不能实现hook?
请问:
- Hooks对Fiber更好 -> Hooks是Fiber的产物 -> 没有Fiber就不是Hooks
请问怎么用逻辑推理出这条链?
2. 对于你回答中的事实性错误, 你持什么看法?
不知道有没有正确理解你说的“移除一个属性”:
onst DefaultButton = {
props: {
renderText: Function
},
render(h) {
return h('button', this.renderText())
}
}
function omitRenderText(comp, render) {
return {
render(h) {
const { renderText, ...others } = this.$attrs
return h(comp, {
attrs: {
...others,
renderText: render || renderText
}
})
}
}
}
const textButton = omitRenderText(DefaultButton, () => "")
new Vue({
render(h) {
return h(textButton, {
attrs: {
renderText: () => ""
}
})
}
})
如果你觉得这篇文章对你还是有很大帮助的话,不介意的话可以加下我刚刚建立的一个学习交流群,有很多相关资料和学习视频:907694362
如何评价 Vue 的 Function-based Component?的更多相关文章
- vue render function
vue render function https://vuejs.org/v2/guide/render-function.html { // Same API as `v-bind:class`, ...
- vue render function & dataset
vue render function & dataset https://vuejs.org/v2/guide/render-function.html#The-Data-Object-In ...
- vue & arrow function error
vue & arrow function error <template> <div class="home"> <img alt=" ...
- vue组件大集合 component
vue组件分为全局组件.局部组件和父子组件,其中局部组件只能在el定义的范围内使用, 全局组件可以在随意地方使用,父子组件之间的传值问题等. Vue.extend 创建一个组件构造器 template ...
- Vue内置的Component标签用于动态切换组件
html <div id="app"> <component :is="cut"></component> <butt ...
- vue.js组件(component)
简介: 组件(Component)是 Vue.js 最强大的功能之一. 组件可以扩展 HTML 元素,封装可重用的代码. 组件系统让我们可以用独立可复用的小组件来构建大型应用,几乎任意类型的应用的界面 ...
- Vue教程:组件Component详解(六)
一.什么是组件? 组件 (Component) 是 Vue.js 最强大的功能之一.组件可以扩展 HTML 元素,封装可重用的代码.在较高层面上,组件是自定义元素,Vue.js 的编译器为它添加特殊功 ...
- [Vue] Parent and Child component communcation
By building components, you can extend basic HTML elements and reuse encapsulated code. Most options ...
- vue学习之组件(component)(二)
自定义事件 父组件使用 prop 传递数据给子组件.但子组件怎么跟父组件通信呢?这个时候 Vue 的自定义事件系统就派得上用场了. 1. 使用 v-on 绑定自定义事件 每个vue实例都实现了事件接口 ...
随机推荐
- Java描述设计模式(16):代理模式
本文源码:GitHub·点这里 || GitEE·点这里 一.生活场景 1.场景描述 在电商高速发展的今天,快递的数量十分庞大,甚至出现了快递代理行业,简单的说就是快递的主人没有时间收快递,会指定一个 ...
- python-Debug、函数装饰器
Debug操作: 程序出问题的时候可以用debug来看一下代码运行轨迹,然后找找问题在哪里 1.先给即将debug的代码打上断点: 2.打完断点之后右键点击debug: 3.然后依次点击开始按钮让 ...
- 项目spring boot 写es hbase 运行内存溢出
本地项目运行正常.服务器上运行内存溢出. 项目内部同时做插入oracle,es,hbase 经过测试发现 同时插入es,hbase是服务器上就会出现内存溢出 如果只插入oracle+es 或oracl ...
- Java面向对象的三大特征和五大原则
Java面向对象的三大特征 封装 封装(Encapsulation)是指属性私有化,根据需要提供setter和getter方法来访问属性.即隐藏具体属性和实现细节,仅对外开放接口,控制程序中属性的访问 ...
- 使用promise封装ajax
直接上代码: function Ajax(method, headers, url, data, progress = null) { return new Promise(function (res ...
- 使用Windows Powershell卸载和安装Win10 原生应用的方法
新装的Win10带有大量的实际工作和生活中不怎么常用的APP,如果觉得这些APP占用磁盘空间或者想要卸载这些应用(APP),Win10下并不能使用Windows 应用管理器直接图形化地卸载这些应用,而 ...
- 根据多个成对的cron表达式生成的时间段,合并
场景:数据库一张表,有个startcron 和endcron 两个字段,根据表达式计算今天的所有时间段. 例:startcron :0 30 20 ? * * endcron :0 30 21 ? * ...
- 第三十九章 POSIX信号量与互斥锁
POSIX信号量相关函数 sem_open 功能: initialize and open a named semaphore 原型: sem_t *sem_open(const char *name ...
- 【长期维护】C++休闲(修仙)躲方块小游戏
左右键控制小球左右移动,上键加速,Esc退出. 一个‘@’20分 #include <windows.h> #include <bits/stdc++.h> #include ...
- 题解 【洛谷】AT654
题解 AT654 [役人[错题已隐藏]] 此题题面没搬过来, 会日语的dalao可以自行去ATCoder查看. 给出此题的JavaAC代码: public class Main { public st ...