3
在開發構建時的React Native中,您可以搖動設備或使用菜單按鈕來調出開發者菜單。你如何添加一個額外的自定義項目到這個菜單?我一直無法找到有關添加其他項目的任何文檔,但我認爲在開發菜單中的服務器環境(開發,產品等)之間切換會非常方便,而不是單獨構建以針對每個環境進行測試。將項目添加到Android React Native Developer菜單
在開發構建時的React Native中,您可以搖動設備或使用菜單按鈕來調出開發者菜單。你如何添加一個額外的自定義項目到這個菜單?我一直無法找到有關添加其他項目的任何文檔,但我認爲在開發菜單中的服務器環境(開發,產品等)之間切換會非常方便,而不是單獨構建以針對每個環境進行測試。將項目添加到Android React Native Developer菜單
DevSupportManager
類可以爲你做這個。
ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager();
DevSupportManager devSupportManager = reactInstanceManager.getDevSupportManager();
隨着在手,如下添加自定義開發選項:您可以從您的ReactNativeHost
例如通過其ReactInstanceManager
得到它
devSupportManager.addCustomDevOption("Custom dev option", new DevOptionHandler() {
@Override
public void onOptionSelected() {
Toast.makeText(MainActivity.this, "Hello from custom dev option", Toast.LENGTH_SHORT).show();
}
});
如果你在第一個讓你ReactNativeHost
實例保持取決於你如何構建你的應用程序。如果您創建使用react-native init
的應用程序,你的主應用程序類將包括此:
@Override
public ReactNativeHost getReactNativeHost() {
return mReactNativeHost;
}
...你可以調用。
I have a complete sample here;看MainActivity .OnCreate:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MainApplication application = (MainApplication) getApplication();
ReactNativeHost reactNativeHost = application.getReactNativeHost();
ReactInstanceManager reactInstanceManager = reactNativeHost.getReactInstanceManager();
DevSupportManager devSupportManager = reactInstanceManager.getDevSupportManager();
devSupportManager.addCustomDevOption("Custom dev option", new DevOptionHandler() {
@Override
public void onOptionSelected() {
Toast.makeText(MainActivity.this, "Hello from custom dev option", Toast.LENGTH_SHORT).show();
}
});
}
我不認爲有任何文件,但它看起來像它在這裏:https://github.com/facebook/react-native/blob/master/React/Modules/ RCTDevMenu.m –