--- sidebar_position: 42 # Use alphabetical order --- import * as SerializableCopySnippet from './_examples/serializableCopy'; import SerializableCopySource from '!!raw-loader!./_examples/serializableCopy'; # createSerializable Recursively converts JavaScript values into [Serializable](./serializable.mdx) references that can be passed to different JavaScript Runtimes. The returned reference cannot be manipulated as it doesn't represent any standard JavaScript object. Changes to the original value don't affect the `Serializable`. **Functions like `scheduleOnUI`, `runOnUISync`, `scheduleOnRuntime` etc. automatically convert values to Serializable references.** See the [passing a copy simulation](./serializable.mdx#passing-a-copy) to follow an object from RN to UI and see why changing the received copy leaves the original unchanged. ## Reference The `createSerializable` call creates the immutable value shown in **Shared native memory**. The reference is then passed to UI, where it is deserialized into a separate JavaScript object. Changing UI's copy leaves both the Serializable and RN's original unchanged.
Type definitions ```typescript function createSerializable(value: TValue): SerializableRef; ```
## Arguments ### value The JavaScript value to be converted into a Serializable reference. It must be one of the supported types listed below. :::note In development builds, the `value` argument is [frozen](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/freeze) after serialization. This prevents misconceptions that changes to the original value would update the already created Serializable. ```typescript import { createSerializable, scheduleOnUI } from 'react-native-worklets'; const object = { a: 1, b: 2, c: 4, }; object.c = 3; // <-- Correct: you can modify the object before serializing it scheduleOnUI(() => { // `object` was implicitly serialized with `createSerializable` when passed to `scheduleOnUI`. console.log(object); // { a: 1, b: 2, c: 3 } }); object.a = 10; // <-- Warning: You can't mutate the object after serializing it ``` ::: ### Returns `SerializableRef` - a reference to the Serializable representation of the input value. ## Remarks - This function automatically detects the type of the input value and applies appropriate serialization strategies. - For objects and arrays, it recursively processes nested properties. - This function is rarely used as in almost all cases the Worklets library handles the creation of Serializables for you. - This function includes cycle detection to prevent infinite recursion. - `jsi::NativeState` of a passed value is preserved during serialization. {/* TODO: Rewrite this remark for 1.0, which has no Legacy Eval Mode. */} - In [Legacy Eval Mode](../bundleMode/overview.mdx#legacy-eval-mode), `createSerializable` can be called only on the [RN Runtime](../concepts/runtimeKinds.mdx#rn-runtime). In Bundle Mode, it can be called from any Runtime. If you need to create a Serializable on a [Worklet Runtime](../concepts/runtimeKinds.mdx#worklet-runtime) in Legacy Eval Mode, use deprecated makeShareableCloneOnUIRecursive instead.