-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Render Function API Change #29
Comments
What impact can this have on the devtools?
|
We can expose anything the devtool needs, since it doesn't have to be part of the public API.
Yes.
Yes. |
I enjoy destructuring arguments in the render function, it makes it easier to ignore unused arguments. Is it really better to have multiple parameters? It's to avoid allocating an extra object every time we call render, isn't it? |
Concerning Event liisteners:
So what does that mean for events in templates on their own? Will all of these work? <button v-on:click="handleClick">
<button v-bind:on-click="handleClick">
<button v-bind:onClick="handleClick">
|
|
Published: vuejs/rfcs#28 |
Summary
h
is now globally imported instead of passed to render functions as argumentBasic example
Motivation
In 2.x, VNodes are context-specific - which means every VNode created is bound to the component instance that created it (the "context"). This is because we need to support the following use cases:
In order to look up locally/globally registered components and directives, we need to know the context component instance that "owns" the VNode. This is why in 2.x
h
is passed in as an argument, because theh
passed into each render function is a curried version that is pre-bound to the context instance.This has created a number of inconveniences, for example when trying to extract part of the render logic into a separate function,
h
needs to be passed along:When using JSX, this is especially cumbersome since
h
is used implicitly and isn't needed in user code. Our JSX plugin has to perform automatich
injection in order to alleviate this, but the logic is complex and fragile.In 3.0 we have found ways to make VNodes context-free. They can now be created anywhere using the globally imported
h
function, so it only needs to be imported once in any file.Another issue with 2.x's render function API is the nested VNode data structure:
This structure was inherited from Snabbdom, the original virtual dom implementation Vue 2.x was based on. The reason for this design was so that the diffing logic can be modular: an individual module (e.g. the
class
module) would only need to work on theclass
property. It is also more explicit what each binding will be processed as.However, over time we have noticed there are a number of drawbacks of the nested structure compared to a flat structure:
class
andstyle
special cases are somewhat inconsistentIn 3.x, we are moving towards a flat VNode data structure to address these problems.
Detailed design
Globally imported
h
functionh
is now globally imported:Render Function Arguments Change
With
h
no longer needed as an argument, therender
function now receives a new set of arguments:props
andattrs
will be equivalent tothis.$props
andthis.$attrs
- also see Optional Props Declaration and Attribute Fallthroughslots
will be equivalent tothis.$slots
- also see Slots Unificationvnode
will be equivalent tothis.$vnode
, which is the raw vnode that represents this component in parent scope, i.e. the return value ofh(MyComponent, { ... })
.Note that the render function for a functional component will now also have the same signature, which makes it consistent in both stateful and functional components:
The new list of arguments should provide the ability to fully replace the current functional render context:
props
andslots
have equivalent valuesdata
andchildren
can be accessed directly onvnode
listeners
will be included inattrs
injections
will have a dedicated new API:parent
access will be removed. This was an escape hatch for some internal use cases - in userland code, props and injections should be preferred.Flat VNode Data Format
With the flat structure, the VNode data props are handled using the following rules:
key
,ref
andslots
are reserved special propertiesclass
andstyle
have the same API as 2.xon
are handled asv-on
bindingsDue to the flat structure,
this.$attrs
inside a component now also contains any raw props that are not explicitly declared by the component, includingonXXX
listeners. This makes it much easier to write wrapper components - simply passthis.$attrs
down withv-bind="$attrs"
(as a result,this.$listeners
will also be removed).Context-free VNodes
With VNodes being context-free, we can no longer use a string ID (e.g.
h('some-component')
) to implicitly lookup globally registered components. Same for looking up directives. Instead, we need to use an imported API:This will mostly be used in compiler-generated output, since manually written render function code typically directly import the components and use them by value, and use rarely have to use directives.
Drawbacks
Reliance on Vue Core
h
being globally imported means any library that contains Vue components will includeimport { h } from 'vue'
somewhere (this is implicitly included in render functions compiled from templates as well). This creates a bit of overhead since it requires library authors to properly configure the externalization of Vue in their build setup:Vue.h
first and fallback torequire
calls.This is common practice for React libs and possible with both webpack and Rollup. A decent number of Vue libs also already does this. We just need to provide proper documentation and tooling support.
Alternatives
N/A
Adoption strategy
For template users this will not affect them at all.
For JSX users the impact will also be minimal, but we do need to rewrite our JSX plugin.
Users who manually write render functions using
h
will be subject to major migration cost. This should be a very small percentage of our user base, but we do need to provide a decent migration path.It's possible to provide a compat plugin that patches render functions and make them expose a 2.x compatible arguments, and can be turned off in each component for a one-at-a-time migration process.
It's also possible to provide a codemod that auto-converts
h
calls to use the new VNode data format, since the mapping is pretty mechanical.Functional components using context will likely have to be manually migrated, but a smilar adaptor can be provided.
The text was updated successfully, but these errors were encountered: