this.$refs.someUserDefinedRef.$el becomes null the second time it is used
See original GitHub issueHi Franck,
I’m still enjoying your library !
I noticed that if I define a ref for a child component. For example:
<template>
...
<hiddenFileInput
ref="filesInput"
@loadedFiles="storeAttachments"
></hiddenFileInput>
...
<template>
and use it in a method, for example:
<scripts>
...
methods: {
selectAttachments() {
this.$refs.filesInput.$el.click();
},
},
...
</scripts>
Then the second time the method is called, I get this error:
Uncaught TypeError: this.$refs.filesInput is null
selectAttachments https://cdn.jsdelivr.net/npm/vue3-sfc-loader@0.2.21/dist/vue3-sfc-loader.js line 22 > Function:63
...
It seems like I lose the reference, although the child component still exists. So I ended up storing the ref in a data on the first call like so:
<scripts>
...
data() {
return {
refFileInput: null,
};
},
...
methods: {
selectAttachments() {
if (!this.refFileInput) {
this.refFileInput = this.$refs.filesInput.$el;
}
this.refFileInput.click();
},
},
...
</scripts>
But isn’t it a bug ? Or is there something I don’t understand or do right ? As a side note is using a ref the right way to reference a child component ?
Thank you for your help,
Merci
Raphaël
Issue Analytics
- State:
- Created 3 years ago
- Comments:8 (4 by maintainers)
Top Results From Across the Web
this.refs.something returns "undefined" - Stack Overflow
Check that you are not accessing ref before the child component has been mounted. E.g. it doesn't work in componentWillMount .
Read more >ref.current returns null when ref returns current.div. - GitHub
When accessing ref.current using the useRef hook, it returns null even ... when refs are set, so the console.log will not run a...
Read more >Ref returns undefined or null in React [Solved] | bobbyhadz
A React ref most commonly returns undefined or null when we try to access its current property before its corresponding DOM element is...
Read more >[SOLVED] This.$refs.key returns undefined when it really is
I am using Vue 2.0 with Webpack. When I say console.log(this.$refs) (inside created() method inside a low level component) it retu…
Read more >Refs and the DOM - React
When the ref attribute is used on an HTML element, the ref created in the constructor with React.createRef() receives the underlying DOM element...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Sorry, I totally forgot to answer you. I was indeed wondering a few days ago if I had done it. Thank you for your answer. Indeed using loadModule only when mounting my App, on my main component, and using the standard import statement worked and solved my problem. I don’t know why I was repeatedly using loadModule. I must have got confused somewhere.
Anyway thanks again and you can close this issue.
Raphael
what news?