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
"content": "```typescript\nexport interface TaskOptions \n```\n\n\n<table><thead><tr><th>\n\nProperty\n\n\n</th><th>\n\nModifiers\n\n\n</th><th>\n\nType\n\n\n</th><th>\n\nDescription\n\n\n</th></tr></thead>\n<tbody><tr><td>\n\n[deferUpdates?](#)\n\n\n</td><td>\n\n\n</td><td>\n\nboolean\n\n\n</td><td>\n\n_(Optional)_ Block the rendering of the component until the task completes. Default is `true`\n\n\n</td></tr>\n</tbody></table>",
"content": "Reruns the `taskFn` when the observed inputs change.\n\nUse `useTask` to observe changes on a set of inputs, and then re-execute the `taskFn` when those inputs change.\n\nThe `taskFn` only executes if the observed inputs change. To observe the inputs, use the `obs` function to wrap property reads. This creates subscriptions that will trigger the `taskFn` to rerun.\n\n\n```typescript\nuseTask$: (fn: TaskFn) => void\n```\n\n\n<table><thead><tr><th>\n\nParameter\n\n\n</th><th>\n\nType\n\n\n</th><th>\n\nDescription\n\n\n</th></tr></thead>\n<tbody><tr><td>\n\nfn\n\n\n</td><td>\n\n[TaskFn](#taskfn)\n\n\n</td><td>\n\n\n</td></tr>\n</tbody></table>\n\n**Returns:**\n\nvoid",
2643
+
"content": "Reruns the `taskFn` when the observed inputs change.\n\nUse `useTask` to observe changes on a set of inputs, and then re-execute the `taskFn` when those inputs change.\n\nThe `taskFn` only executes if the observed inputs change. To observe the inputs, use the `obs` function to wrap property reads. This creates subscriptions that will trigger the `taskFn` to rerun.\n\n\n```typescript\nuseTask$: (fn: TaskFn, opts?: TaskOptions) => void\n```\n\n\n<table><thead><tr><th>\n\nParameter\n\n\n</th><th>\n\nType\n\n\n</th><th>\n\nDescription\n\n\n</th></tr></thead>\n<tbody><tr><td>\n\nfn\n\n\n</td><td>\n\n[TaskFn](#taskfn)\n\n\n</td><td>\n\n\n</td></tr>\n<tr><td>\n\nopts\n\n\n</td><td>\n\n[TaskOptions](#taskoptions)\n\n\n</td><td>\n\n_(Optional)_\n\n\n</td></tr>\n</tbody></table>\n\n**Returns:**\n\nvoid",
[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/core/use/use-task.ts)
8827
8827
8828
+
## TaskOptions
8829
+
8830
+
```typescript
8831
+
exportinterfaceTaskOptions
8832
+
```
8833
+
8834
+
<table><thead><tr><th>
8835
+
8836
+
Property
8837
+
8838
+
</th><th>
8839
+
8840
+
Modifiers
8841
+
8842
+
</th><th>
8843
+
8844
+
Type
8845
+
8846
+
</th><th>
8847
+
8848
+
Description
8849
+
8850
+
</th></tr></thead>
8851
+
<tbody><tr><td>
8852
+
8853
+
[deferUpdates?](#)
8854
+
8855
+
</td><td>
8856
+
8857
+
</td><td>
8858
+
8859
+
boolean
8860
+
8861
+
</td><td>
8862
+
8863
+
_(Optional)_ Block the rendering of the component until the task completes. Default is `true`
8864
+
8865
+
</td></tr>
8866
+
</tbody></table>
8867
+
8868
+
[Edit this section](https://github.com/QwikDev/qwik/tree/main/packages/qwik/src/core/use/use-task.ts)
8869
+
8828
8870
## Tracker
8829
8871
8830
8872
Used to signal to Qwik which state should be watched for changes.
@@ -9955,7 +9997,7 @@ Use `useTask` to observe changes on a set of inputs, and then re-execute the `ta
9955
9997
The `taskFn` only executes if the observed inputs change. To observe the inputs, use the `obs` function to wrap property reads. This creates subscriptions that will trigger the `taskFn` to rerun.
Copy file name to clipboardExpand all lines: packages/docs/src/routes/docs/(qwik)/core/tasks/index.mdx
+31-6Lines changed: 31 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,8 @@ contributors:
19
19
- adamdbradley
20
20
- aendel
21
21
- jemsco
22
-
updated_at: '2023-10-18T07:33:22Z'
22
+
- varixo
23
+
updated_at: '2025-11-08T03:33:22Z'
23
24
created_at: '2023-03-31T02:40:50Z'
24
25
---
25
26
@@ -33,15 +34,17 @@ Tasks are meant for running asynchronous operations as part of component initial
33
34
> **Note**: Tasks are similar to `useEffect()` in React, but there are enough differences that we did not want to call them the same so as not to bring preexisting expectations about how they work. The main differences are:
34
35
>
35
36
> - Tasks are asynchronous.
36
-
> -Task run on server and browser.
37
+
> -Tasks run on server and browser.
37
38
> - Tasks run before rendering and can block rendering.
39
+
> - Subsequent task re-executions (when tracked state changes) block rendering by default, but can be configured to not block DOM updates with `deferUpdates: false`.
38
40
39
41
`useTask$()` should be your default go-to API for running either synchronous or asynchronous work as part of component initialization or state change. It is only when you can't achieve what you need with `useTask$()` that you should consider using `useVisibleTask$()` or `useResource$()`.
40
42
41
43
The basic use case for `useTask$()` is to perform work on component initialization. `useTask$()` has these properties:
42
44
- It can run on either the server or in the browser.
43
-
- It runs before rendering and blocks rendering.
45
+
- It runs before the initial rendering and blocks the initial render.
44
46
- If multiple tasks are running then they are run sequentially in the order they were registered. An asynchronous task will block the next task from running until it completes.
47
+
- By default, subsequent re-executions (when tracked state changes) do block DOM updates unless `deferUpdates: false` is specified.
45
48
46
49
Tasks can also be used to perform work when a component state changes. In this case, the task will rerun every time the tracked state changes. See: [`track()`](#track).
47
50
@@ -59,7 +62,7 @@ When the user interacts with the application, it resumes on the client-side, con
59
62
60
63
**In Qwik, there are only 3 lifecycle stages:**
61
64
62
-
-`Task` - run before rendering and when tracked state changes. `Tasks` run sequentially, and block rendering.
65
+
-`Task` - run before rendering and when tracked state changes. `Tasks` run sequentially, and block each other. They also can block rendering.
63
66
-`Render` - runs after `TASK` and before `VisibleTask`
64
67
-`VisibleTask` - runs after `Render` and when the component becomes visible
65
68
@@ -93,6 +96,28 @@ When the user interacts with the application, it resumes on the client-side, con
93
96
94
97
`useTask$()` registers a hook to be executed upon component creation, it will run at least once either in the server or in the browser, depending on where the component is initially rendered.
95
98
99
+
### Task options
100
+
101
+
`useTask$()` accepts an optional second parameter of type `TaskOptions` to configure the task behavior.
102
+
103
+
```typescript
104
+
interfaceTaskOptions {
105
+
deferUpdates?:boolean;
106
+
}
107
+
```
108
+
109
+
#### `deferUpdates`
110
+
111
+
The `deferUpdates` option controls whether subsequent task re-executions (when tracked state changes) should block DOM updates.
112
+
113
+
**Default behavior (`deferUpdates: false` or not specified):**
114
+
-**Initial render**: The task blocks rendering (same as default)
115
+
-**Subsequent runs**: The task blocks DOM updates until it completes - the journal flush is deferred until the task finishes
116
+
117
+
**With `deferUpdates: true`:**
118
+
-**Initial render**: The task blocks rendering (runs before the component renders for the first time)
119
+
-**Subsequent runs**: The task runs asynchronously without blocking DOM updates
120
+
96
121
Additionally, this task can be reactive and will re-execute when **tracked**[state](/docs/(qwik)/core/state/index.mdx) changes.
97
122
98
123
**Notice that any subsequent re-execution of the task will always happen in the browser**, because reactivity is a browser-only thing.
@@ -110,7 +135,7 @@ Additionally, this task can be reactive and will re-execute when **tracked** [st
110
135
111
136
> If `useTask$()` does not track any state, it will run **exactly once**, either in the server **or** in the browser (**not both**), depending where the component is initially rendered. Effectively behaving like an "on-mount" hook.
112
137
113
-
`useTask$()` will block the rendering of the component until after its async callback resolves, in other words, tasks execute sequentially even if they are asynchronous. (Only one task executes at a time).
138
+
`useTask$()` will block the rendering of the component until after its async callback resolves. Tasks execute sequentially even if they are asynchronous (only one task executes at a time within a component). Subsequent re-executions (when tracking state changes) run asynchronously by default and block rendering unless `deferUpdates: false` is set.
114
139
115
140
Take a look at the simplest use case of the task to run some asynchronous work on component initialization:
- Run code only once before the component is first rendered
179
+
- Run code only once before the component is first rendered
155
180
- Programmatically run side-effect code when state changes
156
181
157
182
> Note, if you're thinking about loading data using `fetch()` inside of `useTask$`, consider using [`useResource$()`](/docs/core/state/#useresource) instead. This API is more efficient in terms of leveraging SSR streaming and parallel data fetching.
// Warning: (ae-internal-missing-underscore) The name "useTaskQrl" should be prefixed with an underscore because the declaration is marked as @internal
0 commit comments