Interview frequency: …
Short answer
Create a class that extends ReactContextBaseJavaModule, annotate exported methods with @ReactMethod, and register the class in a custom ReactPackage added to getPackages() in MainApplication.
Explanation
Set the module name with getName(). Methods annotated with @ReactMethod can receive basic arguments (Strings, Numbers, ReadableMap) and use a Promise to return results or errors asynchronously to JavaScript. This ReactContextBaseJavaModule approach belongs to the old architecture and works through the compatibility layer; new projects should prefer Turbo Native Modules with Codegen.
Code example
// Kotlin code under android/
class BatteryModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
override fun getName() = "BatteryModule"
@ReactMethod
fun getBatteryLevel(promise: Promise) {
try {
val level = 85 // read from the Android system (BatteryManager)
promise.resolve(level)
} catch (e: Exception) {
promise.reject("ERROR_BATTERY", e.message)
}
}
}Common mistakes
- Trying to return complex custom objects directly without converting them to WritableMap or types supported by the Bridge.
Follow-up questions
- What is Codegen in the new React Native architecture, and why is it required?
- How do you build a custom native module in iOS (Swift/Objective-C)?