Skip to main content
Version: Unreleased

createSerializable[Type]

Available from 0.14.0

Typed versions of createSerializable. Each one accepts one type of value. In development builds, it throws if it gets anything else.

import { createSerializableMap, scheduleOnUI } from 'react-native-worklets';

const serializableMap = createSerializableMap(new Map([['answer', 42]]));

scheduleOnUI(() => {
'worklet';
const map = serializableMap as unknown as Map<string, number>;
console.log(map.get('answer')); // 42
});

The worklet function receives the Map itself, but TypeScript still sees the SerializableRef type, so the example type asserts it.

FunctionAccepts
createSerializableStringstring
createSerializableNumbernumber
createSerializableBooleanboolean
createSerializableBigIntbigint
createSerializableNullnull
createSerializableUndefinedundefined
createSerializableArrayArray
createSerializableObjectPlain object
createSerializableMapMap
createSerializableSetSet
createSerializableErrorError
createSerializableRegExpRegExp
createSerializableArrayBufferArrayBuffer
createSerializableArrayBufferViewTyped array, for example Uint8Array, or DataView
createSerializableWorkletWorklet
createSerializableRemoteFunctionFunction that is not a worklet. In the Bundle Mode it can be a function from any runtime, otherwise only from the RN Runtime
createSerializableHostObjectJSI host object

Remarks​

  • Elements of arrays, objects, maps and sets are converted with createSerializable.
  • On the Web these functions return the value unchanged.

We are Software Mansion.