Skip to content

Commit 1ff50b6

Browse files
committed
fix(tutorial): some fixes
1 parent 77481be commit 1ff50b6

File tree

9 files changed

+40
-48
lines changed

9 files changed

+40
-48
lines changed

packages/docs/src/routes/tutorial/events/synchronous-sync/index.mdx

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,24 @@ created_at: '2022-08-02T12:07:45Z'
1515

1616
While not a common use case, you may occasionally need to process events synchronously.
1717

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.
1919

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+
<a href="/" preventdefault:click onClick$={() => {
22+
console.log('clicked');
23+
}}>
24+
link
25+
</a>
26+
```
2327

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.
2531

2632
```tsx
2733
<a href="/"
2834
onClick$={sync$((event, target) => {
35+
// Only prevent default if the ctrl key is pressed.
2936
if (event.ctrlKey) {
3037
event.preventDefault();
3138
}
@@ -34,7 +41,7 @@ This example shows how to use synchronous code blocks using `sync$()`.
3441
</a>
3542
```
3643

37-
## `sync$()` Restrictions (BETA)
44+
## `sync$()` Restrictions
3845

3946
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:
4047
- 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:
6370
</a>
6471
```
6572

73+
You can also dispatch a custom event from the `sync$()` handler and handle it asynchronously:
6674

75+
```tsx
76+
<a href="/"
77+
onClick$={sync$((event, target) => {
78+
if (event.ctrlKey) {
79+
event.preventDefault();
80+
}
81+
// Dispatch a custom event to be handled asynchronously.
82+
target.dispatchEvent(new CustomEvent('myclick', { bubbles: true }));
83+
})}
84+
onMyclick$={() => {
85+
// This part can be asynchronous and can close over anything.
86+
console.log('clicked');
87+
}}>
88+
link
89+
</a>
90+
```
6791

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).

packages/docs/src/routes/tutorial/hooks/use-un-mount/index.mdx

Lines changed: 0 additions & 17 deletions
This file was deleted.

packages/docs/src/routes/tutorial/hooks/use-un-mount/problem/app.tsx

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/docs/src/routes/tutorial/hooks/use-un-mount/solution/app.tsx

Lines changed: 0 additions & 5 deletions
This file was deleted.

packages/docs/src/routes/tutorial/projection/fallback/index.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ created_at: '2022-08-02T12:07:45Z'
1111

1212
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.
1313

14+
Note that in the upcoming Qwik 2.0, a Slot can embed fallback content directly, without needing CSS.
15+
1416
### Example
1517

1618
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.
File renamed without changes.
File renamed without changes.
File renamed without changes.

packages/docs/src/routes/tutorial/tutorial-menu.json

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,13 @@
139139
"enableClientOutput": false,
140140
"enableSsrOutput": false
141141
},
142+
{
143+
"id": "use-signal",
144+
"title": "useSignal()",
145+
"enableHtmlOutput": false,
146+
"enableClientOutput": false,
147+
"enableSsrOutput": false
148+
},
142149
{
143150
"id": "recursive",
144151
"title": "Recursive Store",
@@ -233,13 +240,6 @@
233240
"enableClientOutput": false,
234241
"enableSsrOutput": false
235242
},
236-
{
237-
"id": "use-signal",
238-
"title": "useSignal()",
239-
"enableHtmlOutput": false,
240-
"enableClientOutput": false,
241-
"enableSsrOutput": false
242-
},
243243
{
244244
"id": "use-visible-task",
245245
"title": "useVisibleTask$()",
@@ -253,13 +253,6 @@
253253
"enableHtmlOutput": false,
254254
"enableClientOutput": false,
255255
"enableSsrOutput": false
256-
},
257-
{
258-
"id": "use-un-mount",
259-
"title": "useUnMount$()",
260-
"enableHtmlOutput": false,
261-
"enableClientOutput": false,
262-
"enableSsrOutput": false
263256
}
264257
]
265258
},

0 commit comments

Comments
 (0)