我一直在使用本机模块的React Native应用程序.能够将原生端定义的枚举导出到JS端会很好.在iOS上,您可以在本机模块中定义constantsToExport方法,如文档 here所示: - (NSDictionary *)constantsToExpor
- (NSDictionary *)constantsToExport { return @{ @"firstDayOfTheWeek": @"Monday" }; }
但是,在Android上似乎没有这么好的方式.我错过了什么,或者根本没有提供?谢谢!
根据文件: http://facebook.github.io/react-native/releases/0.41/docs/native-modules-android.html#native-modules您不需要将常量放在BaseJavaModule.java中
每个扩展ReactContextBaseJavaModule的类都有一个可选的方法getConstants()
An optional method called getConstants returns the constant values
exposed to JavaScript. Its implementation is not required but is very
useful to key pre-defined values that need to be communicated from
JavaScript to Java in sync.
@Override public Map<String, Object> getConstants() { final Map<String, Object> constants = new HashMap<>(); constants.put(DURATION_SHORT_KEY, Toast.LENGTH_SHORT); constants.put(DURATION_LONG_KEY, Toast.LENGTH_LONG); return constants; }