Module constants
Constants declaration
- JS Spec
- iOS Spec
- Android Spec
src/NativeMyAwesomeModule.ts
import type { TurboModule } from 'react-native';
import { TurboModuleRegistry } from 'react-native';
// ...
export interface Spec extends TurboModule {
getConstants(): { ALONSO: string, HAMILTON: string, LATIFI: string, VERSTAPPEN: string }
}
export default TurboModuleRegistry.getEnforcing<Spec>('MyAwesomeModule');
ios/NativeMyAwesomeModule.mm
- (facebook::react::ModuleConstants<JS::NativeMyAwesomeModule::Constants::Builder>)constantsToExport
{
return [self getConstants];
}
- (facebook::react::ModuleConstants<JS::NativeMyAwesomeModule::Constants::Builder>)getConstants {
facebook::react::ModuleConstants<JS::NativeMyAwesomeModule::Constants::Builder> constants;
constants = facebook::react::typedConstants<JS::NativeMyAwesomeModule::Constants::Builder>({
.ALONSO = @"You always have to leave the space!",
.HAMILTON = @"Dream the impossible!",
.LATIFI = @"I don't know what happened there... I'm fine"
.VERSTAPPEN = @"Don't forget to sanitise!"
});
return constants;
}
- Kotlin
- Java
android/src/main/newarch/com/myawesomeapp/NativeMyAwesomeModule.kt
override fun getTypedExportedConstants(): MutableMap<String, Any> {
return mutableMapOf(
"ALONSO" to "You always have to leave the space!",
"HAMILTON" to "Dream the impossible!",
"LATIFI" to "I don't know what happened there... I'm fine",
"VERSTAPPEN" to "Don't forget to sanitise!"
)
}
android/src/main/newarch/com/myawesomeapp/NativeMyAwesomeModule.java
import java.util.HashMap;
import java.util.Map;
// ...
@Override
protected Map<String, Object> getTypedExportedConstants() {
final Map<String, Object> map = new HashMap<>();
map.put("ALONSO", "You always have to leave the space!");
map.put("HAMILTON", "Dream the impossible!");
map.put("LATIFI", "I don't know what happened there... I'm fine");
map.put("VERSTAPPEN", "Don't forget to sanitise!");
return map;
}