Function dispatched while component initialization was incomplete

Currently, I am learning Svelte 3 from an amazing tutorial that can be found here. In light of this, I decided to create a tab component as shown below:

<script>
  import { createEventDispatcher } from "svelte";
  const dispatch = createEventDispatcher;
  export let items;
  export let activeItem;
</script>

<ul class="tab tab-block">
  {#each items as item}
    <li on:click={() => dispatch("tabChange", item)}>
      <div class:active={item === activeItem}>
        {item}
      </div>
    </li>
  {/each}
</ul>

Furthermore, I proceeded to import this tab component into my parent component, Projects.svelte:

<script>
import Tabs from "./Tabs.svelte";
  let items = ["Projects", "Users"];
  let activeItem = "Projects";
//...
  const tabChange = (e) => {
    activeItem = e.detail;
  };
</script>

<Tabs {activeItem} {items} on:tabChange={tabChange} />

Upon compiling Svelte without any errors, both the active and non-active tabs rendered correctly. However, upon clicking a tab, an error message appeared:

index.mjs:938 Uncaught Error: Function called outside component initialization
    at get_current_component (index.mjs:938)
    at createEventDispatcher (index.mjs:954)
    at Array.click_handler (Tabs.svelte:10)
    at HTMLLIElement.click_handler (Tabs.svelte:12)

At this point, I am curious as to what could possibly be causing this error and how I can go about resolving it?

Answer №1

Remember to include the createEventDispatcher function:

<script>
  import { createEventDispatcher } from "svelte";
  const dispatch = createEventDispatcher(); // <-- Don't forget the parenthesis
  // ..
</script>

...

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Creating, editing, and deleting data in Ng2 smart table is a seamless process that can greatly enhance

While working on my Angular 2 project, I utilized [ng2 smart table]. My goal was to send an API request using the http.post() method. However, upon clicking the button to confirm the data, I encountered the following error in the console: ERROR TypeErro ...

Combine an array of objects that are dynamically created into a single object

Having trouble transforming the JSON below into the desired JSON format using JavaScript. Current JSON: { "furniture": { "matter": [ { "matter1": "Matter 1 value" }, { "matter2": "Matter 2 value" }, { ...

Issue with React rendering numbers without displaying div

In my user interface, I am attempting to display each box with a 1-second delay (Box1 after 1 second, Box2 after another 1 second, and so on). https://i.sstatic.net/FdTkY.png However, instead of the desired result, I am seeing something different: https ...

In Chrome Inspector console, a never-ending JavaScript loop is triggered by a modification in the htaccess file to create pretty URLs, specifically when

Brand new to the world of .htaccess modifications and handling clean URLs. I currently have a php/bootstrap/mysql/javascript page at: http://localhost/modules/posts/questions_all.php. I am interested in redirecting pages such as http://localhost/modules/ ...

The click event in an Angular 6 loop is not being activated

When the Test is clicked, the function should be invoked. However, nothing happens when I click on it. Here is the HTML component code: <div class="row m-0 justify-content-between" *ngFor="let i of k[currentk]?.details | keys"> <div (click ...

Issue with using Ajax form in conjunction with jquery append method

This particular code snippet appears to be functioning, however, it seems to be missing the invocation of the ajax form chat_process.php. I require the ajax form to be called and display an alert message. var html = ""; for(...) { html += '<f ...

JavaScript / Ajax / PHP - A Minor Bug That's Bugging Me

I'm in need of some help, as I feel like I'm losing my mind right now. I've developed a code using php/js/ajax to verify if an email address exists when it's submitted. After testing the ajax, I can confirm that the correct values are ...

Verify the presence of the input post in the mongo database

Currently, I am utilizing the following node js code to store data in a database... //connect to database db.on('error', console.error); db.once('open', function() { router.post('/register', function(request, response) { ...

React - the elusive nature of variable mutation

In my React app, there is a requirement to choose a specific version of a phone and then confirm the selection. Below is an excerpt from the React component's code: const App = () => { const [selectedVersion, setSelectedVersion] = useState(""); ...

Ways to extract the returned AJAX success object from a function

Attempting to extract attribute values, I have assigned an ajax GET request to a variable. The console.log displays the ajax object, but I am encountering difficulty returning the object within the success function. I have tested with: ajaxObj.d ajaxObj.r ...

The if-else condition is creating issues with the functionality of the buttons

Update #1 - Enhanced query, fresh functional link. Live site: The challenge at hand revolves around an if-else statement related to the form buttons on the right-hand column of the webpage. Scroll past the header and navigation to witness the issue in ...

Show CKEditor on screen by clicking a button

Greetings! I typically find the answers to my questions here, but this time I need to ask one myself... ;) I am currently working on a webpage and I would like to make a text editable using ckeditor. So far, I have managed to enable editing by clicking on ...

Determine the percentage-based width of a grid layout cell

My attempt to create a responsive grid layout is lacking in the mathematical department... Seriously, I'm not a math genius. This is my goal: Determine the width of the container holding the grid elements. Apply a 20px right and bottom margin to al ...

What is the best way to retrieve an ng-model parameter within a controller?

I'm working on implementing a PUT method in my controller and need to bind the new value back to it. I've tried: <div ng-app="myApp" ng-controller="personCtrl"> <form> First Name: <input type="text" ng-mod ...

Adequate parameters are necessary for an express callback function beyond just (req, res)

Working on my express app, I've come across a requirement where only (req, res, next, err) can be passed into the callbacks. This is what I had that worked. function listEvents(auth, req, res, email, authURL = ""){ ... var calendar = google.calendar ...

Uniting the front-end of a React application with the back-end of

Everything is running smoothly with the development build - my React app on port 3000 and server on port 8080. Requests from the frontend to the backend are working perfectly. However, once I deploy the production build, the application starts on port 50 ...

Activating Ionic6 Stack Modal through JavaScript or TypeScript

Is it possible to trigger the modal using script code instead of a button? I have searched through various examples in the tutorial, but all of them rely on the modal trigger mechanism. <ion-button id="open-modal" expand="block">O ...

What is the best way to reset a CSS background GIF after hovering over it?

Is there a way to create a texture animation with text using CSS background GIFs that reload when hovered again? I've tried some JavaScript/jQuery but can't seem to make it work. Any suggestions? I attempted the following approach, but it's ...

What is the reason behind TypeScript's decision not to raise an error in case of a mismatched function argument type?

Here's a simple illustration to showcase my point: type Info = { id: number; } type ImportantInfo = { id: number; value: 5; } type Task = (data: Info) => void; const task: Task = data => null; const data: ImportantInfo = { i ...

What is the best way to divide a JavaScript scope into multiple files when working with Ubuntu JS scopes?

As I work on developing a simple web search scope, I realized that the main JavaScript file has become large and complex. Is it possible to split this main file into multiple files? I attempted using Node.js require, but encountered an error stating that i ...