setSerializableMapping
Available from 0.14.0
When Worklets serializes an object, for example when a worklet captures it in its closure, it sends a serialized copy of that object to the other Runtime and remembers the mapping from the object to that copy. setSerializableMapping lets you set the mapping manually.
Reference
import { scheduleOnUI, setSerializableMapping } from 'react-native-worklets';
const flag = { current: false };
setSerializableMapping(flag, { current: true });
scheduleOnUI(() => {
console.log(flag.current); // logs `true`
});


Type definitions
function setSerializableMapping(value: object, mapping: unknown): void;
Arguments
value
The object to set the mapping for. It's compared by reference.
mapping
The value to send in place of value. It's serialized once, when setSerializableMapping is called. It can also be a SerializableRef, which is used as is.
Remarks
- The new mapping overwrites the existing one for
value, including one that Worklets created when it serializedvalueearlier. - Use hasSerializableMapping to check whether
valuealready has a mapping. - Use getSerializableMapping to read the mapping.
- Use removeSerializableMapping to remove the mapping.
- Each Runtime keeps its own mappings, so
setSerializableMappingaffects only the values that the calling Runtime serializes. - On the Web
setSerializableMappingdoes nothing, because values aren't serialized there.