-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
Alternative pattern to component composition using forwarding technique #1671
Comments
In Vue 3 we strongly recommend against mixins and will not be investing more efforts into facilitating composition patterns that revolve around inheritance/mixins/HOCs. Of course, you are welcome to use whatever patterns you like, but you likely won't receive much feedback from us. Composition API is designed exactly to solve component logic composition. I would recommend looking into it and try a bit harder beyond "it looks verbose" and "looks like React hooks" - neither are reasonable arguments to dismiss it outright. Also - the repo issue list is for bugs and feature requests only, and this seems more like a general design discussion, which should be posted either to the forum, or to the RFCs repo. |
@yyx990803 Completely understand your situation with Vue 3. The same transition is happening in the React ecosystem to move away from HOCs and to hooks instead in a lot of packages. Thank you for letting me know where I should post these types of proposals! I was not certain as there was no option from the new issue builder for the And thank you for the time that you took to briefly review and respond to this issue! I'll be sure to post in the correct place next time. |
What problem does this feature solve?
This solves the problem of re-using common side effect or hydration logic for components. I haven't even used Vue for a week yet and quickly ran into the problem of trying to compose Higher Order Components around a Base Component. I was redirected to the Composition API RFC and npm package by a friend. But I noticed that it was very verbose and similar to using React Hooks. So I was hoping to use a more simpler solution to my problem.
What does the proposed API look like?
I decided to come up with my own pattern/solution to the problem I faced. I know this solution might be missing the forwarding of some options like $listeners, but it demonstrates a very simple way to compose HOCs around a base component.
Simple Pattern Example Demo: https://codesandbox.io/s/simple-vue-container-composition-sk7zb?fbclid=IwAR08Zck3exsxKV6iSxwSP5HLJqo-4nPTLvAb0YEd92r4iP8QIH7PR4ae_3w&file=/src/components/HelloWorld/index.js:0-296
Pattern overview:
Forwarding
HOCcontainer
HOF1.
Forwarding
HOCThe basic concept is that commonly used
Forwarding
HOC function receives the following two arguments:BaseComponent
Mixin
The goal of this HOC is to use the following
computed
property from theMixin
to facilitate passing along additionalprops
to theBaseComponent
:enhancingProps
: acomputed
property that returns an object containing the additionalprops
that should be passed along to theBaseComponent
. This could also be adata
property.The
Mixin
itself should not mix in anyprops
. This is to avoidprops
collisions. And because theBaseComponent
'sprops
are spread into theForwarding
component'sprops
in order to continue the reactive chain.EXAMPLE
2.
container
HOFNow in order for someone to create their own custom HOC, they need to make a
container
which is simply just a Higher Order Function that receives any number of parameters for supporting the customMixin
. And then the returned function receives the following parameter:BaseComponent
The
container
HOF's returned function is responsible for calling theForwarding
HOC function passing theBaseComponent
and a customMixin
that will provide thecomputed
propertyenhancingProps
.EXAMPLE
3. Simple Client Code Example
So in the client code a call to the
container
HOF from anindex.js
file of acomponent
's folder could be made in order to compose the HOC around theBaseComponent
. Example:NOTE - That the
BaseComponent
which is in this case isHelloWorld
, should declare the expectedenhancingProps
in it'sprops
option in order for reactivity to occur if they were to change:4. Complex Client Code Demo
A more complex example of this can be found here where I basically made a Vue version of the
@nozbe/withObservables
React HOC in order to hyrdate the root App component withblogs
that are inserted into the WatermelonDB managed indexeddb database. NOTE: TheReset Database
button doesn't reactively cause the listedblogs
to disappear yet. Just focus on tryingGenerate 100 More
button:https://codesandbox.io/s/34cjw?file=/src/components/HelloWorld/index.js
This is loosely based on the demo code for WatermelonDB's web example which is in React: https://watermelondb.now.sh/
The text was updated successfully, but these errors were encountered: