Skip to content
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

Identifier 'root' has already been declared error #10

Open
thdoan opened this issue Oct 1, 2022 · 0 comments
Open

Identifier 'root' has already been declared error #10

thdoan opened this issue Oct 1, 2022 · 0 comments

Comments

@thdoan
Copy link

thdoan commented Oct 1, 2022

In some cases we need to dynamically write out the script tag for grid-gallery.js (e.g., when used in modals). In this case, if the container is destroyed and recreated, it will throw an "Uncaught SyntaxError: Identifier 'root' has already been declared" error because the constant root is still in memory.

Steps to reproduce:

  1. Go to https://codepen.io/thdoan/pen/yLjjLPw
  2. Launch the modal
  3. Close the modal (click on empty space below)
  4. Launch the modal
  5. Observe the JS error in the console

Fix: wrap everything in an IIFE to preserve variable scope.

(() => {
  const root = document.querySelector("body, html");
  ...
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant