Comments (14)
I'd also like to propose that we always call these "render hooks" to avoid confusion with lifecycle hooks, then start using some other term for lifecycle hooks starting Vue 3 (e.g. "lifecycle functions"). If a Vue user ever has to say the words "lifecycle hook hook" or even "lifecycle hook render hook", I'll cry a little. 😄
from core.
Updated vue-hooks
to support Vue-style hooks + hooks()
in 2.x: https://github.com/yyx990803/vue-hooks#usage-in-normal-vue-components
from core.
@chrisvfritz yeah, I think we would introduce hooks as an advanced feature for code reuse.
Re count/setCount
: you are right, if you create an object with useState()
, you can still directly mutate it and it will work. I added a new paragraph showing how we can provide hooks that maps Vue's current API (and I think it's promising!)
We can provide hooks like useWatch
and useComputed
, although with one extra rule: you need to access state as a property (i.e. always with a dot) inside the getter of computed and watch so you can't use destructuring when you create the state.
from core.
@HerringtonDarkholme Hooks are stateful under the hood. A functional component with hooks is no longer a stateless functional component, it is internally represented as a stateful instance. It actually works fine in a class. On the other hand, not making it available in the idiomatic API makes it useless to a large group of Vue users.
@posva options like that misses a critical feature of hooks: that they can pass values between each other in the same function scope. I thought about making mixins explicitly expose properties, but hooks is still better.
Hooks compress better also due to the function scope - every variable inside can be shortened to single letters.
from core.
Even as a traditional class
fan, I believe hooks are superior and will prevail frontend development.
Great to see it integrated in Vue-next!
My only concern is that should we mix hook with normal class? They are two different mental model and it might be difficult for users to switch between...
from core.
Funny thing, the latest version I released for vue-promised uses named imports so it could also export a hook version.
The pattern seems powerful but as you said it's hard for beginners so we have to be careful not to replace existing patterns that are much easier to grasp for newcomers because that's one of Vue good points
About alternative syntaxes, something more magical could be useful, but I'm not even sure about that. I personally don't like:
<template>
<div @click="setCount(count + 1)">
{{ count }}
</div>
</template>
<script>
import { useState } from 'vue'
export default {
hooks: [
useState(0, ['count', 'setCount'])
]
}
</script>
The thing that tickles me is that hooks are clearly oriented to render functions.
What makes this pattern so good in compression?
from core.
Hook call order
@yyx990803 I agree with pretty much everything you said on Twitter. Another education concern I have with hooks is that they must be called in the same order every time the render function is run, which isn't intuitive unless you understand their magic. To me, this makes them almost unusable outside of an ESLint environment, because we could only prevent people from constantly shooting themselves in the foot with a rule to warn/train them.
count
/setCount
vs proxied getters and setters
I'm sure there are some aspects of hooks I'm still not understanding, so forgive me if this is a naive question, but why would we need to return a setCount
, rather than just count
with a proxied getter and setter?
from core.
Mapping current API to hooks looks fantastic 😍 It feels like authentic Vue in hooks and type checks better! (lest lifecycle renaming fuss 😄 ). I'll try with Vue2 to see if it works well.
@posva About minimizing, if we cannot rename properties on object in most compressor, but hooks expose themselves as local variables and plain function. So minimizers can take advantage of it.
from core.
Could there be a setter for useComputed
? And useData
seems only accept an object or array which is a little constraint for that I think.
from core.
I'd also like to propose that we always call these "render hooks" to avoid confusion with lifecycle hooks, then start using some other term for lifecycle hooks starting Vue 3 (e.g. "lifecycle functions"). If a Vue user ever has to say the words "lifecycle hook hook" or even "lifecycle hook render hook", I'll cry a little. 😄
Yeah, avoiding that ambiguity is important. In fact, "hooks" in web development usually refers to custom code being called after some action or event (Git hooks, WordPress hooks, Vue's lifecycle hooks etc). To me, the word "injection" makes more sense for this feature than "hook".
from core.
Could there be a setter for
useComputed
?
@Jinjiang I think it would make sense for useComputed
to work the same as current computed properties, where they can accept a function or object with get
and set
methods:
const double = useComputed({
get: () => data.count * 2
set: newValue => { data.count = newValue / 2 }
})
What do you think?
useData
seems only accept an object or array which is a little constraint for that I think.
I think the idea is a little different from what React is doing. Instead of a new useState
for each individual piece of state, I think it would be a good practice to have only a single useData
per render function. This way, it'll remain easy to see all the component's internal state at a glance (just like with the object- and class-based syntaxes). Does that make sense?
"hooks" in web development usually refers to custom code being called after some action or event (Git hooks, WordPress hooks, Vue's lifecycle hooks etc). To me, the word "injection" makes more sense for this feature than "hook".
@anthonygore I absolutely agree with you that "hooks" is a confusion name for this feature. Unfortunately, the React team never asked us before introducing the pattern. 😅 And I worry calling it anything else at this point would just cause more confusion, since we have so many developers coming from React. Does that make sense?
from core.
To me, the word "injection" makes more sense for this feature than "hook".
@anthonygore @chrisvfritz Actually, "injection," or "inject," has already been used for a very different feature :)
useData
seems only accept an object or array which is a little constraint for that I think.
@Jinjiang I'm a bit lost here – why is it a constraint? The only other option for data
is a function, which is, as far as my understanding goes, not relevant or necessary anymore in a hook context as the state won't be shared across components. What am I missing?
from core.
useData
seems only (to) accept an object or array which is a little constraint for that I think.@Jinjiang I'm a bit lost here – why is it a constraint? The only other option for
data
is a function, which is, as far as my understanding goes, not relevant or necessary anymore in a hook context as the state won't be shared across components. What am I missing?
@phanan I mean useData()
cannot accept a primitive value like string, number or boolean. It seems like data()
in Vue but not like setState(0)
in React or in the new design above which accepts a primitive value directly.
And via:
I think useData()
is neither data()
in Vue nor useState()
in React. So that's all my concern.
Thanks.
from core.
Closing in favor of the newer RFCs (vuejs/rfcs#22, vuejs/rfcs#23)
from core.
Related Issues (20)
- UModal error when opened HOT 1
- Invalid bidirectional binding of subcomponent ref HOT 2
- Cannot read properties of null (reading 'insertBefore') HOT 2
- Vue compat with Jest: "TypeError: decode_js.EntityDecoder is not a constructor" HOT 2
- MutationObserver in defineCustomElement is disconnected if we move the custom element to another place, then lose attribute observation
- Input with type="number" does not update value after removing leading zeros from v-model binding HOT 1
- Formerly functional project is now broken HOT 2
- KeepAlive + Transition + v-if 同时使用时会导致时会导致内存泄漏的2个bug HOT 6
- Cannot dynamically assign new property to $options object in vue/compat
- Vue defineProps<> can't use imported Interface HOT 6
- Agree with you. For vue2 I am going to have an issue no ? how can I an make both working good ? HOT 2
- Incorrect type error with kebab-cased "aria-*" props HOT 4
- 导入类型时省略.tsx后缀,在defineProps<>中使用导入的类型,会提示Failed to resolve import source "xxxx" HOT 4
- Two-way binding not working when using reactive, must use Ref HOT 6
- vue3中,想用ts类来开发组件,现在遇到个问题,如何定义ref HOT 4
- Computed generic type stripping ruin the type strictness HOT 1
- Using generic component with typed slots (defineSlots) in non-setup component breaks types
- camelCase prop names not converting to kebab-case when using `defineComponent` with function signature HOT 2
- Differences between vue3 click events and vue2.7 click events HOT 1
- template ref with reactive in v-for always empty in production mode HOT 1
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 core.