You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
While not a common use case, you may occasionally need to process events synchronously.
17
17
18
-
Since Qwik processes asynchronously by default, your code must be explicitly configured for synchronous calls.
18
+
The two most common use cases are to call `event.preventDefault()` or `event.stopPropagation()` on a DOM event. Those are handled by adding the `preventdefault:eventname` or `stoppropagation:eventname` in addition to your event handler.
19
19
20
-
There are two ways of processing events synchronously:
21
-
1. preferred way: use `sync$()` to load code synchronously. Fast, resumable but with **significant restrictions** on event handler size.
22
-
2. eager registration: use `useVisibleTask$()` to load code synchronously. No restrictions, but requires eager code execution, which goes against resumability.
20
+
```tsx
21
+
<ahref="/"preventdefault:clickonClick$={() => {
22
+
console.log('clicked');
23
+
}}>
24
+
link
25
+
</a>
26
+
```
23
27
24
-
This example shows how to use synchronous code blocks using `sync$()`.
28
+
When you have other use cases that require synchronous event processing, you can use either `sync$()` or `useVisibleTask$()`.
29
+
1. preferred way: use `sync$()` to embed the code in the HTML. Fast, resumable, but with some restrictions (see below).
30
+
2. eager registration: use `useVisibleTask$()` to register a handler. No restrictions, but it will not handle events until the visible task has executed, and it will cause of course run extra javaScript while the page is loading.
25
31
26
32
```tsx
27
33
<ahref="/"
28
34
onClick$={sync$((event, target) => {
35
+
// Only prevent default if the ctrl key is pressed.
29
36
if (event.ctrlKey) {
30
37
event.preventDefault();
31
38
}
@@ -34,7 +41,7 @@ This example shows how to use synchronous code blocks using `sync$()`.
34
41
</a>
35
42
```
36
43
37
-
## `sync$()` Restrictions (BETA)
44
+
## `sync$()` Restrictions
38
45
39
46
The `sync$()` function is a resumable way to process events synchronously. However, it has some significant restrictions. The `sync$()`**can't close over anything**. The implications are that you can't:
40
47
- access any state: The recommended way to get the state into function is to place on element attributes.
@@ -63,6 +70,23 @@ For this reason, we recommended breaking up the event handlers into two parts:
63
70
</a>
64
71
```
65
72
73
+
You can also dispatch a custom event from the `sync$()` handler and handle it asynchronously:
66
74
75
+
```tsx
76
+
<ahref="/"
77
+
onClick$={sync$((event, target) => {
78
+
if (event.ctrlKey) {
79
+
event.preventDefault();
80
+
}
81
+
// Dispatch a custom event to be handled asynchronously.
// This part can be asynchronous and can close over anything.
86
+
console.log('clicked');
87
+
}}>
88
+
link
89
+
</a>
90
+
```
67
91
68
-
**Your task:** Convert the `onClick$` from asynchronous event to synchronous event by using [`useVisibleTask$`](/docs/(qwik)/core/tasks/index.mdx#usevisibletask) lifecycle and [normal event registration](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener).
92
+
**Your task:** Convert the `onClick$` from asynchronous event to synchronous event by using [`sync$`](/docs/(qwik)/core/tasks/index.mdx#usevisibletask).
Fallback content allows the child component to display fallback content in case the parent component did not provide content. The fallback content can be done with CSS, it is necessary to add a CSS selector (and the appropriate CSS) on the named Slot in case it is empty.
13
13
14
+
Note that in the upcoming Qwik 2.0, a Slot can embed fallback content directly, without needing CSS.
15
+
14
16
### Example
15
17
16
18
In this example, we show three cards, but some of them are not filled with content. Use the right CSS selector to specify the fallback content.
0 commit comments