This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
Tutorial: Create a native module
Edit page
A tutorial on creating a native module that persists settings with Expo Modules API.
In this tutorial, you build a module that stores the user's preferred app theme: dark, light, or system. On Android, use SharedPreferences, and on iOS, use UserDefaults. You can implement web support with localStorage, but this tutorial does not cover that.

Build a native module that persists user settings using SharedPreferences on Android and UserDefaults on iOS.
1
Initialize a new module
First, create a new module. For this tutorial, the module is named expo-settings or ExpoSettings. You can choose a different name, but adjust the instructions to match your choice.
Since you aren't going to actually ship this library, you can hit Return for all the prompts to accept the default values.
2
Set up workspace
Clean up the default module to start with a clean slate. Delete the view module since this guide does not use it.
Locate the following files and replace their contents with the provided minimal boilerplate:
3
Run the example project
Start the TypeScript compiler to watch for changes.
In a separate terminal window, run the example app.
You should see the text "Theme: system" in the center of the screen when you launch the example app. The value "system" comes from synchronously calling the getTheme() function in the native module. You will change this value in the next step.
4
Get, set, and persist the theme preference value
Android native module
To read the value, look for a SharedPreferences string under the key "theme". If the key does not exist, default to "system". Use the reactContext (a React Native ContextWrapper) to access the SharedPreferences instance with getSharedPreferences().
To set the value, use the edit() method of SharedPreferences to get an Editor instance. Then, use putString() to set the value. Ensure the setTheme function accepts a value of type String.
iOS native module
To read the value on iOS, look for a UserDefaults string under the key "theme". If the key does not exist, default to "system".
To set the value, use the set(_:forKey:) method of UserDefaults. Ensure the setTheme function accepts a value of type String.
TypeScript module
Update the ExpoSettingsModule.ts to add a TypeScript interface for the ExpoSettingsModule native module to update the theme.
Now, call your native modules from TypeScript.
Example app
You can now use the Settings API in your example app.
When you rebuild and run the app, the "system" theme is still set. Pressing the button does nothing, but when you reload the app, the theme changes. This happens because the app does not fetch the new theme value or re-render. You will fix this in the next step.
5
Emit change events for the theme value
Ensure developers using your API can react to theme value changes by emitting a change event whenever the value updates. Use the Events definition component to describe the events your module emits, sendEvent to emit the event from native code, and the EventEmitter API to subscribe to events in JavaScript. The event payload is { theme: string }.
Android native module
Events payloads are represented as Bundle instances on Android, which you can create using the bundleOf function.
iOS native module
TypeScript module
Example app
6
Improve type safety with Enums
It's easy to make mistakes when using the Settings.setTheme() API in its current form, as it allows any string value. Improve the type safety of this API by using an enum to restrict the possible values to system, light, and dark.
Android native module
iOS native module
TypeScript module
Example app
If you change Settings.setTheme(nextTheme) to Settings.setTheme("not-a-real-theme"), TypeScript will raise an error. If you ignore the error and press the button, you will see the following runtime error:
ERROR Error: FunctionCallException: Calling the 'setTheme' function has failed (at ExpoModulesCore/SyncFunctionComponent.swift:76) → Caused by: ArgumentCastException: Argument at index '0' couldn't be cast to type Enum<Theme> (at ExpoModulesCore/JavaScriptUtils.swift:41) → Caused by: EnumNoSuchValueException: 'not-a-real-theme' is not present in Theme enum, it must be one of: 'light', 'dark', 'system' (at ExpoModulesCore/Enumerable.swift:37)
The last line of the error message shows that not-a-real-theme is not a valid value for the Theme enum. The only valid values are light, dark, and system.
Congratulations! You have created your first Expo Module for Android and iOS.
Next steps
Create native modules using Kotlin and Swift.
A tutorial on creating a native view with Expo Modules API.