question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

this.$refs.someUserDefinedRef.$el becomes null the second time it is used

See original GitHub issue

Hi 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:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
raphael-bressoncommented, Feb 10, 2021

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

0reactions
FranckFreiburgercommented, Feb 10, 2021

what news?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found