Is there a way to make v-bind more efficient? #7952
Replies: 3 comments 3 replies
-
<template>
<div class="text" v-for="item in 10" :key="item">hello</div>
</template>
<script>
export default {
data() {
return {
color: 'red'
}
}
}
</script>
<style>
.text {
color: v-bind(color);
}
</style>
refer to css v-bind. |
Beta Was this translation helpful? Give feedback.
-
Specifically in your case, you can assign variables to the parent |
Beta Was this translation helpful? Give feedback.
-
Edit: Wow, just realized this question is 18 months old ^^ You didn't provide the code that generated these styles, but I assume that each div actually represents a component, because these generated CSS variables are injected into component root nodes. That also explains why what you first propose - lifting them up to the parent element - isn't really achievable: The parent element is rendered by the parent component, while each of the divs is rendered by the respective child component. Moving those declarations from the divs up to the section would require the child component to manipulate elements managed by the parent component. While technically not impossible, this is a complex change with all sorts of possible edge cases to cover, and from a principle standpoint, it breaks component encapsulation. But there's another, bigger issue: each child component has its own local state that is being reflected in these CSS variables. And each child component can independently change this state. These different states would overwrite each other when the variables were moved to a single parent element. To work around that, each child component instance would have to dynamically create the variable name at runtime, which isn't possible because the CSS referencing these variables is static after build. Moving them into a class is also not really possible, for the same reason: CSS classes etc become static after rendering, and these variables need to be updated during runtime. |
Beta Was this translation helpful? Give feedback.
-
In my last job, I used React + Styled Components. Now, I fully recognize that the way Styled Components dynamically injects classes is terribly bad for performance (because of constant style recalculation), and technically Vue is doing it a better way.
That said, is there any way to make these declarations more efficient?
All of the inner
<div>
s are in a for loop in a component, so I'm wondering if there's any way to promote a single style declaration to the section (since they are repeated), or if there's been any kind of discussion of Vue making this kind of optimization. Or some kind of optimization where a "set" of style declarations are cached to a CSS class, but maybe pre-computed?Technically, because these properties are all static values at compile time, there's no reason Vue couldn't generate class variants per instance in a much more streamlined way via static analysis. (It wouldn't be trivial / easy to do; I just mean it's perfectly possible.)
As a follow-up question, is there documentation on how one might create a Vue CSS plugin that generates different output?
Beta Was this translation helpful? Give feedback.
All reactions