-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
Dynamic Lifecycle Injection #25
Comments
I could totally see a babel plugin compiling |
Love it as well as the other part of this. A nice side effect of this approach would be that it solves the problem of naming conflicts when we would introduce new lifecycle hooks in later versions, right? Right now, in the public discussion about the class API, we are in a back and forth how to namespace lifecycle hooks when using the class API. If we declare lifecycle hooks deprecated, or committed to introducing new hooks as these dynamic lifecylce hooks only, risks of breaking our user's code in the future disappear. |
I don't get why using Everything else looks good! Regarding replacing lifecycle hooks in class declarations, I don't think we should replace them because it's a less intuitive api |
Yes, the fetch example can use For the mouse example, if you return a plain object, data() {
const mouse = useMousePosition()
return {
mouse
}
} Then access it as
Lifecycle hooks don't need to be explicitly removed if the component is already unmounted (because they would never fire again) |
Published: vuejs/rfcs#23 |
in vue2
parent components can listen to its children's’ lifecycle hooks. especially for third-party components. What should we do in VUE3? |
Summary
Introduce APIs for dynamically injecting component lifecycle hooks.
Basic example
Motivation
In advanced use cases, we sometimes need to dynamically hook into a component's lifecycle events after the component instance has been created. In Vue 2.x there is an undocumented API via custom events:
This API has some drawbacks because it relies on the event emitter API with string event names and a reference of the target component instance:
Event emitter APIs with string event names are prone to typos and is hard to notice when a typo is made because it fails silently.
If we were to extract complex logic into external functions, the target instance has to be passed to it via an argument. This can get cumbersome when there are additional arguments, and when trying to further split the function into smaller functions. When called inside a component's
data()
or lifecycle hooks, the target instance can already be inferred by the framework, so ideally the instance reference should be made optional.This proposal addresses both problems.
Detailed design
For each existing lifecycle hook (except
beforeCreate
), there will be an equivalentonXXX
API:When called inside a component's
data()
or lifecycle hooks, the current instance is automatically inferred. The instance is also passed into the callback as the argument:Explicit Target Instance
When used outside lifecycle hooks, the target instance can be explicitly passed in via the second argument:
If the target instance cannot be inferred and no explicit target instance is passed, an error will be thrown.
Injection Removal
onXXX
calls return a removal function that removes the injected hook:Appendix: More Examples
Pre-requisite: please read the Advanced Reactivity API RFC first.
When combined with the ability to create and observe state via standalone APIs, it's possible to encapsulate arbitrarily complex logic in an external function, (with capabilities similar to React hooks):
Data Fetching
This is the equivalent of what we can currently achieve via scoped slots with libs like vue-promised. This is just an example showing that this new set of API is capable of achieving similar results.
Use the Platform
Hypothetical examples of exposing state to the component from external sources. This is more explicit than using mixins regarding where the state comes from.
The text was updated successfully, but these errors were encountered: