Creating a unique event binding in Vue 2 with JSX

Instead of binding a custom event in the mounted() method, I want to bind it on the root tag directly. So, I attempted the following code:

render (h) {
  return (
    <div on-custom-event={this.handleCustomEvent}></div>
  )
}

However, when testing it in Chrome, I realized that the custom-event was bound to the DOM and couldn't be triggered using $emit. With VueJS 2's template syntax, this can be easily achieved like so:

<template>
   <div @custom-event="handleCustomEvent"></div>
</template>

If you have any insights or solutions to this issue, your help is greatly appreciated. Thank you!

Answer №1

It may be a little late to join the party, but here's how you can trigger the event:

protected render(h: any) {
    return (
        <a onClick={(e: any) => this.$emit('test')}>
            {this.$slots.default}
        </a>
    );
}

To listen for the event:

protected render(h: any) {
    return (
        <NavLink onTest={() => alert('clicked')}>
            <i class='fa fa-bars'></i>
        </NavLink>
    );
}

Answer №2

Based on the information provided in the documentation, it is recommended to use camel-case for JSX event handlers instead of kebab-case. Here is an example code snippet to follow:

render (h) {
  return (
    <div onCustomEvent={this.handleCustomEvent}></div>,
  )
}

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

Concerns arise with the swal destroy functionality

I have a working code for the Swal function, but when I click cancel without entering any information, it still triggers the AJAX call, which is not desired. $(document).on('click','.addon',function() { Swal.fire({ title: &apo ...

End a loop by triggering a click event in jQuery

I need help finding a solution to halt a loop using a click event (similar to a panic button). Once the button is clicked, the loop should stop instantly. // Here is the basic concept $.each(someArray, function(index, value){ setTimeout(function(){ ...

tips for transforming a javascript string into a function invocation

I am faced with the challenge of turning the string logo.cr.Button into a function call. Here is the code I'm working with: logo.cr.Button = function(){ //something } var strg = 'logo.cr.Button'; strg(); When I attempt to execute strg(); ...

Terminating a client connection in Node.js using socket.io

What is the best way to terminate the socket connection on the client side? I am currently utilizing: socket.io 0.9 node.js 0.10.15 express 3.3.4 For example, when calling localhost/test -- server side var test = io .of('/test') .on(&apos ...

Utilizing an array for assigning values in conditional statements

I have been experimenting with toggling a CSS style on a couple of elements using an if statement. Currently, I have it working with several if statements, but I believe it can be simplified by utilizing an array with the values and just one if statement. ...

Reactjs is currently not integrated with Firebase

I recently completed a project in reactjs and realized it requires authentication. My initial thought was to connect it with Firebase as it seemed like the most straightforward option. Following a tutorial on YouTube, however, I encountered some difficult ...

What is the most effective method for sorting through an array to generate a new array?

Is there a way to filter items from an array based on a specific string and store them in a new array for further manipulation? If so, what would be the most efficient approach to achieve this? Here is the current progress of my code: for (var i = 0; i & ...

Azure Chatbot that logs conversations in Webchat whenever the user selects 'none of the above' option

Recently, I've delved into the world of web chat services and embarked on a journey to craft a chat bot using pure JavaScript code that can seamlessly integrate into any HTML file. Despite consulting Microsoft's documentation, I find myself in a ...

Show a specific item when selecting an option from a dropdown menu

Hey there, I have a question regarding creating a theme for the Genesis Framework. Right now, I'm facing a challenge with one particular element. Here's the situation: I've got two drop-down lists, but I only want the second one to be visib ...

Library for JavaScript that enables incremental testing using regular expressions

Searching for a unique JavaScript library (preferably a node.js package) that has the ability to gradually check if a string meets a regular expression, character by character, and provide uncertain results. For instance, let's consider the following ...

Update the controller to modify the route parameter

I've defined my state like this: stateProvider .state('app', { templateUrl: 'views/layout.html', url: '/:lang/app', resolve: { lang: ['$rootScope', &a ...

Angular with Firebase: How to ignore a field in a query

I am curious to see if my current structure is compatible with Firebase, or if I need to make adjustments. Let's take a look at an example using the "/rooms" endpoint, which contains an array of Room objects: export class Room { id: number; p ...

Display/Conceal content with JQuery on a PHP webpage

I am having trouble with the following code. My intention is to show/hide the content between the #info id when clicking buttons, but nothing seems to be happening. Could you help me identify the issue? echo '<script> $( "#show' . $r ...

passing parameters via routes

I've been working on passing parameters through the URL using this code, but it doesn't seem to be functioning properly. I suspect that the issue lies in the "get(\users:id)" part, but I'm unsure of the correct way to do it: $.ajax ...

Cache path for generated AngularJS templates in Grunt Angular Templates

Here is the file structure I am currently working with: src app ticket template.html index.html app.min.js I have been utilizing grunt-angular-templates for creating a cache, and my configuration looks like this: ngtemplates:{ app:{ ...

Using Javascript to toggle visibility of radio buttons on a PDF form

I've been looking around but haven't come across any information regarding using JavaScript with PDF form applications. If I missed something, I apologize and would appreciate any guidance. Currently, I have 5 radio buttons that are synchronized ...

Encountering a CORS-like error causes issues for Swagger

Unable to retrieve data from the server. The access-control-origin settings may not be configured correctly. I have encountered this issue in the past, but it is crucial for me to get Swagger UI working properly this time. The JSON file I am attempting to ...

How can I access the feed of a single user using the Facebook API

I have yet to experience working with Facebook APIs, but I am interested in developing a basic app that will display posts from a specific Facebook user. I would prefer not to enable login for multiple users, just keep it simple. My goal is to create an a ...

JavaScript Array not displaying correctly as an array

Consider the following array: var data = {"result":"success","ids":["00000","54321","123","22222","11111","55555","33333","abc123","123abc","12345","44444"]} localStorage.ids = data.ids; However, when attempting to iterate through it using AngularJS: an ...

No results found with mongoose.findOne

I'm attempting to fetch a todo based on its ID. The code for the getTodo service method is shown below: const getTodo = async (id, userId) => { const todo = await Todo.findOne({ id: id, userId: userId }); return todo; }; The code from the rout ...