Skip to main content
Version: 1.0 (beta)

createSerializable[Type]

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 casts 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, from any 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.