55
loading...
This website collects cookies to deliver better user experience
ReactBrowserEventEmitter
event handling:--> Top-level delegation is used to trap most native browser events. This may only occur in the main thread and is the responsibility of ReactDOMEventListener
, which is injected and can therefore support pluggable event sources. This is the only work that occurs in the main thread.
button
with an onClick
handler is rendered<button onClick={() => console.log('button was clicked')}>Click here</button>
button
node. Instead, it gets a reference to the document root where the application is rendered and mounts an event listener there. React uses a single event listener per event type to invoke all submitted handlers within the virtual DOM. Whenever a DOM event is fired, those top-level listeners initiate the actual event dispatching through the React source code — it re-dispatched the event for each and every handler. This can be seen in the source code of EventPluginHub.--> We normalize and de-duplicate events to account for browser quirks. This may be done in the worker thread.
click
event will have arguments like this--> Forward these native events (with the associated top-level type used to trap it) to EventPluginHub
, which in turn will ask plugins if they want to extract any synthetic events.
SimpleEventPlugin
will handle events implemented in common browsers such as mouse and key press events (source) and ChangeEventPlugin
will handle onChange
events (source). The final piece that unifies all the plugins into a single place and re-directs events to each individual plugin is the EventPluginHub
.SyntheticEvents
, which React defines as "implementation of the DOM Level 3 Events API by normalizing browser quirks". Basically, it is a wrapper around the browser's native event object with the same interface — and that it works in identical fashion across all browsers.--> The EventPluginHub
will then process each event by annotating them with "dispatches", a sequence of listeners and IDs that care about that event.
--> The EventPluginHub
then dispatches the events.
.vue
file that contains a script
tag to execute javascript and a template
tag that wraps all markup (both DOM and custom elements). This is a self contained instance of a Vue component that could also contain a style
tag to house the CSS.--> Simple DOM events handling
v-on:<event-name>
or in short, @<event-name
directive, and to store the state of application in a data
prop. All the event handlers are stored similarly in a methods
prop on the same object.// App.vue
<template>
<div id="app">
<HelloWorld :msg="msg" />
<button @click="greet('World', $event)">
click here
</button>
</div>
</template>
<script>
import HelloWorld from "./components/HelloWorld";
export default {
name: "App",
components: { HelloWorld },
data: function () {
return { msg: "Vue" };
},
methods: {
greet: function (message, $event) { this.msg = message; }
}
}
</script>
--> Event Modifiers
.stop
.prevent
.capture
.self
.once
.passive
/* this will trigger the handler method only once */
<button v-on:click.stop.once="clickHandler">Click me</button>
--> Key Modifiers
enter
and tab
keys. The full list of aliases is given below:.enter
.tab
.delete
(captures both the "Delete" and "Backspace" keys).esc
.up
.down
.left
.right
.space
<!-- only call `vm.submit()` when the `key` is `Enter` -->
<input v-on:keyup.enter="submit">
--> Custom Events
// emit event
this.$emit('myEvent')
// bind to the event
<my-component v-on:myevent="doSomething"></my-component>
v-on
event listeners inside DOM templates will be automatically transformed to lowercase (due to HTML’s case-insensitivity), so v-on:myEvent
would become v-on:myevent
-- making myEvent
impossible to listen to. Vue JS as a framework recommends using kebab-casing for event names.<button (click)="onSave()">Save</button>
$event
object is determined by the target event
. If the event is a native DOM element event, then the $event
object is a DOM event object. Lets look at a simple example (source)<input
[value]="currentItem.name"
(input)="currentItem.name=$event.target.val"
/>
input
event of the <input>
element, which allows the code to listen for changes.input
event.$event
.$event.target.vaue
and updates the name
property.$event
has the shape that the directive or component produces..svelte
file that is meant to self contain a component instance with its CSS, JS and HTML, along with any custom elements that are needed.--> Simple DOM events handling
<script>
let name = 'world';
function update() { name = 'Svelte'; }
</script>
<span on:click={update}>Hello { name }</span>
Hello World
on load, but will update and print Hello Svelte
when user clicks on h1
-> REPL. This is the general pattern in which DOM events such as click
, mousemove
, etc are implemented in Svelte (it supports inline handlers as well).--> Event Modifiers
preventDefault
and stopPropagation
. The handler function is able to accept an event
argument that also has access to these modifiers, but Svelte offers an improvement in developer experience by offering these shorthands. An example would look like the following:<script>
function handleClick() { alert('This alert will trigger only once!'); }
</script>
<button on:click|once={ handleClick }>Click here</button>
preventDefault
- calls event.preventDefault()
before running the handler. Useful for client-side form handlingstopPropagation
- calls event.stopPropagation()
, preventing the event from reaching the next elementpassive
- improves scrolling performance on touch/wheel events (Svelte will add it automatically where its safe to do so)nonpassive
- explicitly set passive: false
capture
- fires the handler during the capture phase instead of the bubbling phase (MDN docs)once
- remove the handler after the first time it runsself
- only trigger handler if event.target
is the element itself--> Dispatching events
createEventDispatcher
. The function allows the child component to emit a data object at a user defined key. The parent component can then do as it pleases with it -> REPL (open console to see dispatched data object).--> Event forwarding
--> Actions
actions
. Actions are element level functions that are useful for adding custom event handlers. Similar to transition functions, an action function receives a node
and some optional parameters and returns an action object. That object can have a destroy
function, which is called when the element is unmounted -> REPL (borrowed from Svelte official resources).--> Browser Events
--> Document Loading
jQuery.holdReady()
: Holds or releases the execution of jQuery's ready event (source)jQuery.ready()
: A Promise-like object that resolves when the document is ready (source).load()
: Bind an event handler to the "load" JS event (source).ready()
: Specify a function to execute when the DOM is fully loaded (source).unload()
: Bind an event handler to the "unload" JS event (source)--> Form Events
.blur()
: Bind an event handler to the “blur” JS event, or trigger that event on an element (source).change()
: Bind an event handler to the “change” JS event, or trigger that event on an element (source).focus()
: Bind an event handler to the “focus” JS event, or trigger that event on an element (source).focusin()
: Bind an event handler to the “focusin” JS event (source).focusout()
: Bind an event handler to the “focusout” JS event (source).select()
: Bind an event handler to the “select” JS event, or trigger that event on an element (source).submit()
: Bind an event handler to the “submit” JS event, or trigger that event on an element (source)--> Keyboard Events
.keydown()
: Bind an event handler to the "keydown" JS event, or trigger that event on an an element (source).keypress()
: Bind an event handler to the "keypress" JS event, or trigger that event on an an element (source).keyup()
: Bind an event handler to the "keyup" JS event, or trigger that event on an an element (source)--> Mouse Events
.click()
: Bind an event handler to the "click" JS event, or trigger that event on an an element (source).dblclick()
: Bind an event handler to the "dblclick" JS event, or trigger that event on an an element (source).contextmenu()
: Bind an event handler to the "contextmenu" JS event, or trigger that event on an an element (source).mousemove()
: Bind an event handler to the "mousemove" JS event, or trigger that event on an an element (source).mouseout()
: Bind an event handler to the "mouseout" JS event, or trigger that event on an an element (source).mouseover()
: Bind an event handler to the "mouseover" JS event, or trigger that event on an an element (source).mouseup()
: Bind an event handler to the "mouseup" JS event, or trigger that event on an an element (source).toggle()
: Bind an event handler to the "toggle" JS event, or trigger that event on an an element (source).hover()
: Bind an event handler to the "hover" JS event, or trigger that event on an an element (source).mousedown()
: Bind an event handler to the "mousedown" JS event, or trigger that event on an an element (source).mouseenter()
: Bind an event handler to the "mouseenter" JS event, or trigger that event on an an element (source).mouseleave()
: Bind an event handler to the "mouseleave" JS event, or trigger that event on an an element (source)--> The Event Object
event.currentTarget()
: The current DOM element within the event handling bubbling phase (source)event.target()
: The DOM element that initiated the event (source)event.data()
: Optional data object passed to the handler when the current executing handler is bound (source)event.preventDefault()
: If this method is called, the default action of the event will not be triggered (source)event.stopPropagation()
: Prevents the event from bubbling up the DOM tree, preventing any parent handlers from being notified of the event (source)1.6.4
--> The .on()
Event Handler Attachment API
.on()
. This API is designed to bind almost all of the events listed above with one single stroke. It is the recommended way to bind events (according to official documentation) from jQuery - 1.7 version and onwards. A few syntax examples can be seen below:// Markup to be used for all examples that follow
<div class='outer'>
<span class='inner'>Any content</span>
</div>
// Exhibit A: the simple click handler, targeting the inner span
$('.outer .inner').on('click', function(event) {
console.log(event);
alert( 'inner span was clicked!!' );
});
// Exhibit B: attaching separate handlers to different event types
$('.outer .inner').on({
mouseenter: function() {
console.log( 'hovered over a span' );
},
mouseleave: function() {
console.log( 'mouse left a span' );
},
click: function() {
console.log( 'clicked a span' );
}
});
// Exhibit C: attaching the same handler to different event types
$('.outer .inner').on('click', function() {
console.log( 'The span was either clicked or hovered on' );
});
// Exhibit D: Event delegation --> binding events to elements that don't exist yet
$('.outer .inner').on('click', '<selector-of-element-that-dont-exist-yet>', function() {
console.log( 'The element was clicked' );
});
--> Other Event Handler Attachment APIs
.on()
API is arguably the most popular API offered by jQuery. Apart from it, there are other interfaces jQuery has out of the box that provides a useful suite of functionality. The following is a list of the most commonly occurring ones: