Skip to content

Commit

Permalink
Merge pull request #7101 from QwikDev/shairez-patch-1
Browse files Browse the repository at this point in the history
added missing `stoppropagation` documentation
  • Loading branch information
shairez authored Nov 27, 2024
2 parents 2a6a959 + 19b2eed commit c8aa847
Showing 1 changed file with 5 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ contributors:
- maiieul
- Balastrong
- Jemsco
- shairez
updated_at: '2024-01-09T20:55:11Z'
created_at: '2023-03-20T23:45:13Z'
---
Expand Down Expand Up @@ -140,11 +141,12 @@ export default component$(() => {
Due to Qwik's asynchronous nature, the execution of an event handler might be delayed if the implementation has not yet been loaded into the JavaScript VM. Consequently, the following APIs on an Event object will not work:

- `event.preventDefault()`
- `event.stopPropagation()`
- `event.currentTarget`

### Prevent Default
### preventDefault & stopPropagation

Because event handling is asynchronous, you can't use `event.preventDefault()`. To solve this, Qwik introduces a declarative way to prevent default through `preventdefault:{eventName}` attribute.
Because event handling is asynchronous, you can't use `event.preventDefault()` or `event.stopPropagation()`. To solve this, Qwik introduces a declarative way to prevent default through `preventdefault:{eventName}` and `stoppropagation:{eventName} attributes.

<CodeSandbox src="/src/routes/demo/events/preventdefault/index.tsx" style={{ height: '6em' }}>
```tsx {7}
Expand All @@ -155,6 +157,7 @@ export default component$(() => {
<a
href="/docs"
preventdefault:click // This will prevent the default behavior of the "click" event.
stoppropagation:click // This will stop the propagation of the "click" event.
onClick$={() => {
// event.PreventDefault() will not work here, because handler is dispatched asynchronously.
alert('Do something else to simulate navigation...');
Expand Down

0 comments on commit c8aa847

Please sign in to comment.