-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
Hooks integration #12
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
Comments
Even as a traditional |
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? |
@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. |
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.
|
@chrisvfritz yeah, I think we would introduce hooks as an advanced feature for code reuse. Re We can provide hooks like |
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. 😄 |
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. |
Updated |
Could there be a setter for |
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". |
@Jinjiang I think it would make sense for const double = useComputed({
get: () => data.count * 2
set: newValue => { data.count = newValue / 2 }
}) What do you think?
I think the idea is a little different from what React is doing. Instead of a new
@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? |
@anthonygore @chrisvfritz Actually, "injection," or "inject," has already been used for a very different feature :)
@Jinjiang I'm a bit lost here – why is it a constraint? The only other option for |
@phanan I mean And via: I think Thanks. |
Closing in favor of the newer RFCs (vuejs/rfcs#22, vuejs/rfcs#23) |
Rationale
https://twitter.com/youyuxi/status/1056673771376050176
Hooks provides the ability to:
However, it is quite different from the intuitions of idiomatic JS, and has a number of issues that can be confusing to beginners. This is why we should integrate it in a way that complements Vue's existing API, and primarily use it as a composition mechanism (replacement of mixins, HOCs and scoped-slot components).
Proposed usage
Directly usable inside class render functions (can be mixed with normal class usage):
For template usage:
In SFC w/ object syntax:
Note: counter is a super contrived example mainly to illustrate how the API works. A more practical example would be this
useAPI
custom hook, which is similar to libs likevue-promised
.Implementation Notes
Proposed usage for
useState
anduseEffect
are already implemented.Update: Mapping w/ Vue's existing API
To ease the learning curve for Vue users, we can implement hooks that mimic Vue's current API:
The text was updated successfully, but these errors were encountered: