Closed
Description
Version
2.4.2
Reproduction link
https://jsfiddle.net/o7yvL2jd/
Steps to reproduce
I've searching for the right way to implement HoC with vue.js. But couldn't found any suitable example.
The link below is known HoC implementations. But didn't work expected.
https://jsfiddle.net/o7yvL2jd/
How can I implement HoC with vue.js?
I just want to know how to implement HoC in the react.js way.
What is expected?
They are HoCs that simply renders components passed in as parameters.
HoCs containing slots and events will render normally.
What is actually happening?
The element to be rendered is missing, or the rendered order differs from the baseComponent.
Some HoC implementations do not work with event handlers.
Activity
LinusBorg commentedon Jul 25, 2017
Hello @eu81273
Thank your for your interest in this project.
However, your issue is a usage/support question, and the issue tracker is reserved exclusively for bug reports and feature requests (as outlined in our Contributing Guide).
We encourage you to ask it on the forum , Stack Overflow or on our discord chat and are happy to help you out there.
LinusBorg commentedon Jul 25, 2017
FWIW, I took a look out of personal interest - this should work 100% of cases.
I edited HOC04 in your example since it was the closest to the solution:
https://jsfiddle.net/Linusborg/o7yvL2jd/22/
Edit: still an issue with slots, investigating ...
lbogdan commentedon Jul 26, 2017
I might have solved it: https://jsfiddle.net/BogdanL/ucpz8ph4/. Slots are just hardcoded now, but that's trivial to solve.
blocka commentedon Jul 26, 2017
Seems the solution is along the method of @lbogdan but createElement should have a way of taking slots, just like it can take scopedSlots.
However, it is still a lot of effort to create an HoC. There's a lot to remember to pass through, while with react you just render the WrappedComponent with props.
blocka commentedon Jul 26, 2017
I just thought of a very simple solution...let me know if I'm missing something here:
eu81273 commentedon Jul 26, 2017
Based on the examples given by @LinusBorg and @lbogdan, the most minimal HoC implementation that can handle components with slots is:
As @blocka mentioned, it is still a lot of effort to create an HoC with vue.js.
blocka commentedon Jul 26, 2017
@eu81273
This seems to pass your test, no? Of course, you would have to adjust it depending on whether WrappedComponent is a constructor or object, but no need to pass slots, events or props.
LinusBorg commentedon Jul 26, 2017
Apart from the issue with slots, this is just due to the fact that Vue does have a more complex API than React, which in this scenario is a disadvantage. I admire Reacts minimal API in these kinds of use cases - Vue was just designed with slightly different deign goals, so HOCs don't come as easily as in React.
But it should be fairly trivial to create a
createHOC()
helper function that wraps this initial setup for you, shouldn't it?lbogdan commentedon Jul 26, 2017
Well, it really depends what the end goal is. From what I understand, the goal of HoC is to somehow change (decorate) the original component (WrappedComponent) to add (or inject) props, methods, event listeners etc. (much like a mixin, really 😄 ).
HOC06
variant only changes the component definition, it doesn't change the way in which it's instantiated.LinusBorg commentedon Jul 26, 2017
@blocka The goal of HOCs often is to get state (e.g. from redux / vuex) and inject it into the wrapped component's props - that would not work with your approach.
[-]How to implement HoC correctly[/-][+]Discussion: Best way to create a HOC[/+]blocka commentedon Jul 26, 2017
@LinusBorg right. I knew it was too good to be true and that I was forgetting something obvious.
43 remaining items