Display custom elements with the React Native SDK
When a screen with a custom element opens, the Adapty SDK passes the element’s Type and ID to a resolver in your app code. The resolver returns one of the following:
- The view that matches the Type and ID: Users see the native view, such as a Lottie animation, an interactive chart, or a camera preview.
- No view: Users see the fallback, which is the content inside the custom element in the Flow & Paywall Builder.
Users of app versions without the resolver also see the fallback. Therefore, you can publish the flow before you release the app.
Before you start
In an Expo project, custom elements work only if you keep the ios and android folders in Git and don’t run npx expo prebuild --clean. Otherwise, Expo regenerates these folders and deletes the code from this page. New Expo projects keep these folders out of Git, so EAS Build regenerates them for every build.
You need:
- Adapty React Native SDK 4.4 or later: Earlier versions show the fallback for every custom element.
- Native code for iOS and Android: You can’t display a React Native component in a custom element. Write this code in Swift for iOS and in Kotlin for Android. Add the Swift code to your app target in Xcode, and the Kotlin code to the folder of
MainApplication.kt. - A naming scheme for custom elements: Give each native view a Type, such as
lottie_animation. Use only lowercase letters, numbers, and underscores in Types. To distinguish between custom elements of the same Type, give each one an ID, such aswelcome_wave. The Types and IDs in your code must match the ones in the Flow & Paywall Builder.
Use the same Types and IDs in your iOS and Android code. Unless you set up a separate audience for each platform, iOS and Android users see the same flows.
Set up your Android project
Your iOS project needs no extra setup. In your Android project, add the Adapty UI library. To build native views as composables, also set up Jetpack Compose.
Add the Adapty UI library
In android/app/build.gradle, add io.adapty:android-ui to the dependencies block, without a version. react-native-adapty sets the version. A different android-ui version can break flows.
dependencies {
implementation("io.adapty:android-ui")
}
The library needs compileSdkVersion 35 or later: if android/build.gradle sets a lower version, raise it to 35.
Set up Jetpack Compose
The React Native template doesn’t set up Compose in the app module. An Android View needs no Compose setup. To build native views as composables, set up the Compose compiler for the Kotlin version of your app, the kotlinVersion in android/build.gradle:
For the Lottie view on this page, also add com.airbnb.android:lottie-compose.
Create views for custom elements
For each Type, create a native view for iOS and for Android, or reuse one from your native code. The following views of the lottie_animation Type play a Lottie animation. The Android view is a composable that uses the com.airbnb.android:lottie-compose library:
Requirements and best practices
-
Sizing:
- Padding and alignment: Make sure the native view fits the container you allocated for it in the Flow & Paywall Builder, minus the padding. If the view doesn’t take up the entire area, it appears in the top start corner of that area by default. To move it, change the alignment in the Layout section of the custom element.
- Hug height: If the height of the custom element is set to Hug, a view that stretches to fill the available space (like a map or a lazy list) will collapse, or stretch to the height of the screen. Give the custom element a Fixed height instead.
-
Theme: Native views don’t inherit the styles of your React Native components. Set colors and fonts in the native code.
-
Flow actions: The native view can’t run flow actions, such as a transition to the next screen. The element context can send messages to the flow, with
sendMessageon iOS andsendon Android, but they’re reserved for future Flow & Paywall Builder capabilities. Keep navigation buttons, purchase buttons, and other buttons with flow actions outside the custom element. -
Selectable elements: Keep the custom element outside product cards and other selectable elements. When the selection changes, the native view can lose its state.
-
Navigation handling: When the user moves to the next screen, the SDK removes the native view from the screen. Stop what the view started, such as camera sessions, players, and location updates:
- SwiftUI view: Use the
onDisappearaction. For async work, use.task: SwiftUI cancels it when the view disappears. - Composable: Use the
onDisposeblock of aDisposableEffect. For async work, useLaunchedEffect: Compose cancels it when the composable leaves the composition. - Android
View: OverrideonDetachedFromWindowin yourViewsubclass, or add aView.OnAttachStateChangeListenerto theView.
When the user comes back, the SDK calls the resolver again, and the native view starts without the state of the previous one. On Android, in a flow that you open with
present(), a rotation makes the SDK call the resolver again. In an embeddedAdaptyFlowView, the native view keeps its state after a rotation. - SwiftUI view: Use the
-
UIKit views: On iOS, wrap them in
UIViewRepresentable. -
Carousels: On Android, in a carousel, the native views on all slides stay active while the screen is open, not only the view on the visible slide. Keep camera previews and players with sound out of carousels.
Write a resolver to match custom elements and views
Write a resolver for each platform. The resolver receives the Type and ID of a custom element, and returns the matching native view.
If you add a custom element with a new Type after you release the app, users of the released version see its fallback.
The resolver has the following requirements:
-
Network data and players: Keep network data and players in the state of the native view, not in the resolver. The SDK calls the resolver many times while the screen is open. Code in the resolver runs again on each call, so network requests repeat and players start over. The native view keeps its state between these calls, so the data loads once and the players keep playing. Create players, sessions, and network requests inside the native view:
- SwiftUI view: Create players, sessions, and network requests in
.taskor a@StateObject, not in the initial value of a@Stateproperty. Swift evaluates that initial value again each time the resolver creates the view. - Composable: Create players and sessions in
remember, and network requests inLaunchedEffect. - Android
View: Create players, sessions, and network requests inonAttachedToWindow.
- SwiftUI view: Create players, sessions, and network requests in
-
One native view per custom element: Some native views change while the screen is open, for example, when their data loads or when the user taps them. Implement this logic in the native view itself, not in the resolver. If the resolver returns a different view for the same custom element, the view loses its state.
-
A new
Viewfor each appearance: On Android, create a newViewin each call offactory. The SDK callsfactoryeach time the custom element appears, such as when the screen opens, when the user returns to the screen, or, in a flow that you open withpresent(), after a rotation. -
App data: Store an object that holds the data, such as an
ObservableObjector aStateFlowwith a daily streak, in a property of the resolver. Pass the object to the native view, and observe the object there:- SwiftUI view: Observe an
ObservableObjectwith@ObservedObject. - Composable: Collect a
StateFlowwithcollectAsState, or observe aLiveDatawithobserveAsState. - Android
View: Observe the object inonAttachedToWindow, and stop inonDetachedFromWindow.
A value that you copy into the resolver, such as the streak number, stays as it was when you created the resolver. If your JavaScript code has the data, such as a daily streak, send each new value to the native code through a Turbo Native Module, and update the object there.
- SwiftUI view: Observe an
Set conditions for fallback content
To show users the fallback content instead of an empty custom element, return no view in the following situations:
- Nothing to visualize — the necessary animation file or data file is missing.
- Nothing to process — the user skipped a quiz question, and the data is incomplete.
- Access prohibited — a denied permission prevents the view from accessing a resource.
For example, the resolver returns no view when the animation file is missing.
For a permission, return no view only when the user denied it before the screen opened. On Android, ContextCompat.checkSelfPermission returns the same value, PERMISSION_DENIED, in two cases: before your app first asks the user for the permission, and after the user denies it. To tell these cases apart, save the result of the permission request, and read the saved result in the resolver. If your app hasn’t requested the permission yet, return the native view, and request the permission inside the view. For example, request location access when a map view appears, and show the map as soon as the user grants access. If you return no view and the user grants access afterwards, they still see the fallback. A permission change doesn’t make the SDK call the resolver again. The native view appears only after the SDK calls the resolver again, such as when the user returns to the screen.
Pass the resolver to the SDK
Set the resolver once for each platform, at app launch. The SDK calls it in every flow, including a flow embedded with AdaptyFlowView. Users see your native views in new flows and A/B tests without an app release. These views still show each user their own data, such as a daily streak, because they read that data from your app.
iOS
Android
In MainApplication.kt, import AdaptyUI, and call AdaptyUI.setCustomElementsResolver in the onCreate method. The highlighted lines show what to add. Keep the rest of the MainApplication class as it is:
import com.adapty.ui.AdaptyUI
class MainApplication : Application(), ReactApplication {
// the other members of your MainApplication class
override fun onCreate() {
super.onCreate()
AdaptyUI.setCustomElementsResolver(CustomElementsResolver(this))
// the rest of your onCreate code
}
}
To show the same view in every custom element of a Type, pass a map of Types and views instead of a resolver. A map demands less code than a resolver, but you cannot set conditions for fallback content.
You can’t attach a resolver to one flow. To display data that your app only has when it opens a flow, send the data to your native code before the flow opens. Then read the data in the resolver. For example, a paywall displays the weight that the user entered in a previous flow.
Test on a device
The Flow & Paywall Builder can’t run your app code, so its canvas only shows the fallback. An error in the native view can crash the app. Before you release the app, test every screen that has a custom element on an iOS device and on an Android device:
-
To see the latest published version of each flow instead of a cached one, register the device as a test device.
-
To see the warnings of the Adapty SDK in the Xcode console and in Logcat, set the log level to
warnor a more detailed level. -
On the device, run a build that includes the resolver. Open each screen that has a custom element, and verify that the native view appears and fits the custom element. If the view scrolls or responds to drags, test these gestures.
-
If the fallback appears instead of the native view, find this warning in the Xcode console or in Logcat:
custom element <type>:<id> is not served by a resolver. Then verify that:- The Type and ID in the warning match the ones in your code, letter for letter.
- Your app has the file, data, or permission that the view needs.
- Your app sets the resolver at launch: in the
AppDelegateclass on iOS, and in theMainApplicationclass on Android.
If the warning doesn’t appear, verify that the app uses Adapty React Native SDK 4.4 or later.
-
On Android, if the custom element stays empty, find one of these warnings in Logcat:
custom element <type>:<id> is not served by a resolver: The custom element has no fallback content, and no resolver returns a view for it. Verify each item in step 4.content does not report its size: The composable has no height of its own. Give the custom element a Fixed height.
The SDK only logs the size warning for composables. If an Android
Viewstays empty with no warning, give the custom element a Fixed height too. -
To preview the fallback, return no view for the Type, and open the screen again.
Examples
Each recipe shows the setup in the builder and the code for your app:
- Show user progress with a Lottie animation: A ring that fills as a streak grows.
- Show an interactive weight goal chart in a paywall: A weight goal chart, in the paywall flow that follows the onboarding flow.
- Show nearby places on a map in a flow: A map of fitness centers near each user.