Skip to content

Identifier 'root' has already been declared error #10

@thdoan

Description

@thdoan

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");
  ...
})();

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions