-
Notifications
You must be signed in to change notification settings - Fork 342
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
how can I pass ref to child component #317
Comments
First off, it's not recommended to mutate props, so if you follow general and don't mutate props (emitting events instead), you should be fine with only the value instead of the ref. If you think that you have an edge case where you need to mutate a prop and therefore need the ref, wrap it in a function that returns the ref, then you can call it in the child to get the ref. setup() {
// ...
const imgWrapperRefFn = () => imageWrapperRef
// ...
} <Canvas :imgWrapperRef="imgWrapperRefFn" v-bind="$props" /> |
As a side-note, you cannot embed the function as shown here, because Vue will still unwrap the ref. <Canvas :imgWrapperRef="() => imageWrapperRef" v-bind="$props" /> |
@LinusBorg As much as I like to follow the 'do not mutate props' mindset, I have found myself more and more passing around reactive properties. Would a directive like this be possible, to instruct Vue to not unwrap? <Canvas :imgWrapperRef.ref="imageWrapperRef" v-bind="$props" /> |
Not sure what you mean. Works fine here
I don't think that will get support. |
Yes, @LinusBorg , it works, but the syntax is verbose, cumbersome, difficult to manage, and requires the reviewer to inspect the function to see if it does more than just pass in a ref to the component. The suggested directive property would make it very clear as the intent when searching source code and debugging. It would be nice to have the same succinct syntax in Vue as demonstrated by other directive properties such as As I have fully embraced Vue Composition API, almost all of my functions now take MaybeRef arguments, and more often than not I unwrap parameters, even if not refs, future proofing my code if I switch over to refs. Anyway I just wanted to open that option for discussion, as it relates 100% to this closed issue, and won't pursue it further if convinced that the suggestion has no merit. |
If you want to start the discussion, there's the RFCs repo for that. |
@LinusBorg Take another look at the side-note method I posted. I was pointing out that you cannot embed a callback function returning the ref in the template. You need to define the function in your script... Even if you call the function from the child component, Vue has already unwrapped your return value. Not expected, but fact. <Canvas :imgWrapperRefFn="() => imageWrapperRef" v-bind="$props" /> In child
I was hoping this would be a simple syntax, as the ref was not accessed until called, but I found out the hard way it does not work. |
I too am running into this issue. I need to pass a ref for a form element down to the child which is where the validate method is used to validate the form. The issue is that the ref is present but the value is undefined. |
What I did was putting the ref inside the father component, then on the onMounted hook, i get the first children on the father component and assign it to a ref onMounted(() => {
if this is a wrapper component i could
|
I notice in the docs:
https://vue-composition-api-rfc.netlify.app/api.html#ref
but sometimes I need to get deep parent's ref like this:
what I wish is I can pass the original
imgWrapperRef
to theCanvas
component。Since
ref
is automateunwrap
, I can only the theHtmlElement
, instead of the originalRef
Object。do you have any
hack
solution for this?The text was updated successfully, but these errors were encountered: