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
Copy file name to clipboardExpand all lines: packages/docs-reanimated/docs/guides/worklets.mdx
+46-24Lines changed: 46 additions & 24 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -8,6 +8,8 @@ sidebar_position: 1
8
8
9
9
Worklets are short-running JavaScript functions that can run on the [UI thread](/docs/fundamentals/glossary#ui-thread). Reanimated uses worklets to calculate view styles and react to events on the UI thread.
10
10
11
+
## Defining worklets
12
+
11
13
You can create your own worklets using the `'worklet';` directive at the top of a function.
12
14
13
15
```javascript
@@ -17,6 +19,8 @@ function myWorklet() {
17
19
}
18
20
```
19
21
22
+
## Workletization
23
+
20
24
The [Reanimated Babel Plugin](https://github.com/software-mansion/react-native-reanimated/blob/main/packages/react-native-reanimated/plugin/README-dev.md#basics) looks for functions marked with the `'worklet'` directive and converts them into serializable objects. We call this process [workletization](/docs/fundamentals/glossary#to-workletize). These objects can then be copied and run over on the UI thread.
21
25
22
26
Most of the time when working with Reanimated and [Gesture Handler](https://docs.swmansion.com/react-native-gesture-handler/) the code is automatically workletized and run on the UI thread by default.
@@ -32,7 +36,7 @@ function App() {
32
36
}
33
37
```
34
38
35
-
Functions marked with `'worklet';` aren't [hoisted](https://developer.mozilla.org/en-US/docs/Glossary/Hoisting). Besides affecting hoisting, the `'worklet';` directive has no effect on the [JavaScript thread](/docs/fundamentals/glossary#javascript-thread).
39
+
## Running worklets on the UI thread
36
40
37
41
You can use [`runOnUI`](/docs/threading/runOnUI) to manually schedule worklet execution on the UI thread:
38
42
@@ -60,6 +64,41 @@ function onPress() {
60
64
}
61
65
```
62
66
67
+
## Running functions from worklets
68
+
69
+
You can run functions on the JS thread from the UI thread with [`runOnJS`](/docs/threading/runOnJS). Most frequently used to call functions that aren't marked with a `'worklet';` directive (i.e. most third-party libraries) or to update the React state.
Functions passed to `runOnJS` must be defined in the [JavaScript thread](/docs/fundamentals/glossary#javascript-thread) scope, i.e. in the component body or the global scope. This code won't work because `myFunction` is defined in the `withTiming` callback, which is only executed in the [UI thread](/docs/fundamentals/glossary#ui-thread):
85
+
86
+
```javascript
87
+
functionApp() {
88
+
consttap=Gesture.Tap().onEnd(() => {
89
+
// myFunction is defined on the UI thread 🚨
90
+
constmyFunction= () => {};
91
+
runOnJS(myFunction)(); // 💥
92
+
});
93
+
}
94
+
```
95
+
96
+
## Hoisting
97
+
98
+
Functions marked with `'worklet';` aren't [hoisted](https://developer.mozilla.org/en-US/docs/Glossary/Hoisting). Besides affecting hoisting, the `'worklet';` directive has no effect on the [JavaScript thread](/docs/fundamentals/glossary#javascript-thread).
99
+
100
+
## Capturing closure
101
+
63
102
Worklets are [closures](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Closures). They can access variables declared outside of their own scope. Only variables referenced in the worklet body will be captured inside the worklet scope.
64
103
65
104
```javascript
@@ -94,6 +133,8 @@ function myWorklet() {
94
133
}
95
134
```
96
135
136
+
## Passing data to worklets
137
+
97
138
Worklets can return data within the same thread.
98
139
99
140
```javascript
@@ -128,32 +169,13 @@ function App() {
128
169
}
129
170
```
130
171
131
-
You can run functions on the JS thread from the UI thread with [`runOnJS`](/docs/threading/runOnJS). Most frequently used to call functions that aren't marked with a `'worklet';` directive (i.e. most third-party libraries) or to update the React state.
There's no separate UI thread available on the Web. Because of that, when Reanimated runs in the browser, worklets are resolved to plain JavaScript functions.
136
175
137
-
functionApp() {
138
-
consttap=Gesture.Tap().onEnd(() => {
139
-
// i'm a worklet too!
140
-
// highlight-next-line
141
-
runOnJS(router.back)();
142
-
});
143
-
}
144
-
```
176
+
However, the `'worklet';` directive is still necessary on the Web, because Reanimated relies on the Babel plugin to capture dependencies inside worklet functions.
145
177
146
-
Functions passed to `runOnJS` must be defined in the [JavaScript thread](/docs/fundamentals/glossary#javascript-thread) scope, i.e. in the component body or the global scope. This code won't work because `myFunction` is defined in the `withTiming` callback, which is only executed in the [UI thread](/docs/fundamentals/glossary#ui-thread):
147
-
148
-
```javascript
149
-
functionApp() {
150
-
consttap=Gesture.Tap().onEnd(() => {
151
-
// myFunction is defined on the UI thread 🚨
152
-
constmyFunction= () => {};
153
-
runOnJS(myFunction)(); // 💥
154
-
});
155
-
}
156
-
```
178
+
## Other worklet runtimes
157
179
158
180
Worklets can run in other runtimes than the one provided by Reanimated. For example [VisionCamera](https://github.com/mrousavy/react-native-vision-camera) and [LiveMarkdown](https://github.com/Expensify/react-native-live-markdown) create their own worklet runtimes.
Copy file name to clipboardExpand all lines: packages/docs-reanimated/docs/threading/runOnUI.mdx
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -71,6 +71,8 @@ import RunOnUISrc from '!!raw-loader!@site/src/examples/RunOnUI';
71
71
72
72
- Make sure not to execute `runOnUI` on the UI thread as this will result in an error.
73
73
74
+
- In browsers there's no separate UI thread available. Because of that, on the Web, `runOnUI` behaves similarly to `requestAnimationFrame`. It creates a function that, when called, will be scheduled to run with given arguments on next animation frame.
0 commit comments