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.

Can't use recursive component

See original GitHub issue

Describe the bug

When I try to use a recursive component (a component that imports itsefl) I get the following error : RecursiveComponent.vue:9 Uncaught ReferenceError: Cannot access 'RecursiveComponent' before initialization

Reproduction

Repo here : https://github.com/nicolas-t/bug-report-vite-recursive-component

Steps to reproduce :

  • yarn install
  • yarn dev
  • Open browser’s console to see the error

System Info

  • vite version: 2
  • Operating System: Ubuntu
  • Node version: 12.18.2
  • Package manager (npm/yarn/pnpm) and version: yarn 1.22

Thanks

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
yyx990803commented, Feb 22, 2021

The self-importing syntax, although works, doesn’t mean you can eagerly access the imported binding:

import foo from '/foo.js'

console.log(foo)

export default {
  msg: 'hello'
}

Here it will result in the same error in the browser over a vanilla http server because foo is accessed before the module itself can be initialized.

However this will work:

import foo from '/foo.js'

export default {
  msg: 'hello'
}

setTimeout(() => {
  console.log(foo)
}, 0);

But again, this is

  1. Not a Vite bug
  2. Unnecessary to get recursive components (just use < RecursiveComponent> and it just works)
0reactions
yyx990803commented, Feb 22, 2021

FYI you don’t need to do this. In Vite Vue SFCs can implicitly reference themselves by file name (without .vue extension).

Read more comments on GitHub >

github_iconTop Results From Across the Web

can't use recursive map in react component - Stack Overflow
To support the nested data with recursion, I've created a function with returns an input for every key, value pair in the data....
Read more >
Recursive components in React: A real-world example
In this article, we will explore the details of recursive components in React and their usage in a real-world application.
Read more >
Shouldn't recursion be prevented in React component?
I know recursion can exist and isn't and shouldn't be logged as an error, but then should recursion exist in React? How does...
Read more >
Recursive React Components: A real world example - YouTube
In this video we go over how to use recursive Reactjs components to display comments on rib.gg! ... Your browser can't play this...
Read more >
How to Use Recursion in React - freeCodeCamp
What will happen is, the function or the component will be called as long as we have the data. So, let's try to...
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