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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This PR implements Worklets. Worklets are small JavaScript functions that can be executed on a separate JavaScript Thread so they don't clog the main JS Thread. Perfect for heavy computations and processing.
Screenshots/Recordings
Before
Before.-.worklets.disabled.mov
After
After.-.worklets.enabled.mov
How to use RN Worklets (Threads)
runAsync
Purpose: runAsync is used to execute code on a different thread without blocking the main JS thread.
When to use: Use it to offload functions to a background thread.
Example:
import { Worklets } from 'react-native-worklets-core';
...
const fibonacci = (num: number): number => {
'worklet'
if (num <= 1) return num;
let prev = 0, curr = 1;
for (let i = 2; i <= num; i++) {
let next = prev + curr;
prev = curr;
curr = next;
}
return curr;
}
const context = Worklets.defaultContext
const result = await context.runAsync(() => {
'worklet'
return fibonacci(50)
})
console.log(`Fibonacci of 50 is ${result}`)
runOnJS
Purpose: runOnJS is used to call a JavaScript function from within a worklet. Since worklets run on a separate thread, they cannot directly call JavaScript functions. runOnJS bridges this gap by allowing you to invoke JavaScript functions on the JavaScript thread.
When to use: Use it when you need to communicate from the worklet thread back to the JavaScript thread.
Example:
import { runOnJS } from 'react-native-worklets-core';
...
const [age, setAge] = useState(30)
function something() {
'worklet'
Worklets.runOnJS(() => setAge(50))
}
useWorklet
Purpose: useWorklet is a hook that allows you to define a worklet function directly within your React component. It ensures that the function is properly marked as a worklet and can run on the worklet thread.
When to use: Use this hook when you need to define a function that will execute on the worklet thread.
Example:
import { useWorklet } from 'react-native-worklets-core';
function MyComponent() {
const myWorklet = useWorklet(() => {
'worklet';
console.log('This is running on the worklet thread');
}, []);
return (
<Button title="Run Worklet" onPress={() => myWorklet()} />
);
}
useRunOnJS
Purpose: useRunOnJS is a hook that allows you to define a JavaScript function that can be safely called from a worklet. It ensures that the function is properly wrapped to run on the JavaScript thread when invoked from the worklet thread.
When to use: Use this hook when you need to call a JavaScript function from a worklet.
Purpose:
useSharedValue is a hook that creates a SharedValue instance, which can be read from and written to by both the JavaScript thread and the worklet thread simultaneously.
For arrays and objects, useSharedValue creates a C++-based proxy implementation, ensuring that all read and write operations on these data structures are thread-safe.
When to use:
Use useSharedValue when you need to share state between the JavaScript thread and the worklet thread.
I've completed the PR template to the best of my ability
I’ve included tests if applicable
I’ve documented my code using JSDoc format if applicable
I’ve applied the right labels on the PR (see labeling guidelines). Not required for external contributors.
Pre-merge reviewer checklist
I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR implements Worklets. Worklets are small JavaScript functions that can be executed on a separate JavaScript Thread so they don't clog the main JS Thread. Perfect for heavy computations and processing.
Screenshots/Recordings
Before
Before.-.worklets.disabled.mov
After
After.-.worklets.enabled.mov
How to use RN Worklets (Threads)
runAsync
Purpose: runAsync is used to execute code on a different thread without blocking the main JS thread.
When to use: Use it to offload functions to a background thread.
Example:
runOnJS
Purpose: runOnJS is used to call a JavaScript function from within a worklet. Since worklets run on a separate thread, they cannot directly call JavaScript functions. runOnJS bridges this gap by allowing you to invoke JavaScript functions on the JavaScript thread.
When to use: Use it when you need to communicate from the worklet thread back to the JavaScript thread.
Example:
useWorklet
Purpose: useWorklet is a hook that allows you to define a worklet function directly within your React component. It ensures that the function is properly marked as a worklet and can run on the worklet thread.
When to use: Use this hook when you need to define a function that will execute on the worklet thread.
Example:
useRunOnJS
Purpose: useRunOnJS is a hook that allows you to define a JavaScript function that can be safely called from a worklet. It ensures that the function is properly wrapped to run on the JavaScript thread when invoked from the worklet thread.
When to use: Use this hook when you need to call a JavaScript function from a worklet.
useSharedValue
Purpose:
useSharedValue is a hook that creates a SharedValue instance, which can be read from and written to by both the JavaScript thread and the worklet thread simultaneously.
For arrays and objects, useSharedValue creates a C++-based proxy implementation, ensuring that all read and write operations on these data structures are thread-safe.
When to use:
Use useSharedValue when you need to share state between the JavaScript thread and the worklet thread.
Example:
Separate Threads/Contexts (Worklets.createContext)
Purpose:
Worklets.createContext is a method that allows you to create a separate thread (context) for running worklets.
Each thread created with createContext operates in isolation, ensuring that tasks in one thread do not interfere with tasks in another.
When to use:
Use Worklets.createContext when you need to offload heavy computations or background tasks to multiple separate threads.
Worklet Examples
WorkletExampleComponent.tsx
WorkletExampleFunctions.ts
Pre-merge author checklist
Pre-merge reviewer checklist