How do I open react-native's dev menu on real device

后端 未结 6 2016
萌比男神i
萌比男神i 2020-12-13 01:38

I have seen already a number of border cases and strange developer interface.

From the \"shake your device\", which is really impractical, specially with a tablet

相关标签:
6条回答
  • 2020-12-13 02:18

    I add a simple absolutely positioned button and this onClick handler to it

    imoprt { NativeModules } from 'react-native';
    ...
    onMenuButtonClick(){
     NativeModules.DevMenu.show();
    }
    
    0 讨论(0)
  • 2020-12-13 02:22

    In the case you have a Xiaomi phone, the given answer will not work, because there is a security option prevented popup window to open and you need to allow it for your app :

    Go to Settings > Installed apps > [Your App Name] > Permission manager, and enable “Display pop-up window”.

    Shake again. The developer menu should pop up as expected with just a little shaking.

    Source: https://matthewphiong.com/debugging-react-native-app-on-a-xiaomi-phone

    0 讨论(0)
  • 2020-12-13 02:26

    Shaking phone in Android is annoying sometimes. Making below changes will open the Developer Menu by pressing the Volume down / Up key.

    Insert the following code in android/../MainActivity.java

      @Override
      public boolean onKeyUp(int keyCode, KeyEvent event) {
          if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN && this.getReactInstanceManager() != null) {
              this.getReactInstanceManager().showDevOptionsDialog();
              return true;
          }
          return super.onKeyUp(keyCode, event);
      }
    

    With React Native Navigation V3 following code will work in MainActivity.java.

      @Override
      public boolean onKeyUp(int keyCode, KeyEvent event) {
          if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN && this.getReactGateway().getReactNativeHost().getReactInstanceManager() != null) {
              this.getReactGateway().getReactNativeHost().getReactInstanceManager().showDevOptionsDialog();
              return true;
          }
          return super.onKeyUp(keyCode, event);
      }
    

    After this change, run the app again to deploy the above code by react-native run-android.

    Tested with RN 0.59+.

    Reference: https://facebook.github.io/react-native/docs/integration-with-existing-apps

    Edit: Include KeyEvent import as well with other imports in the file.

    import android.view.KeyEvent;

    0 讨论(0)
  • 2020-12-13 02:27

    If you are on a mac, there is a handy tool called Frappe. https://github.com/niftylettuce/frappe

    You can use this command from the shell

    adb shell input keyevent 82
    

    if haven't run react-native run-android or if the device gets disconnected after you have run the react-native run-android. You need to re-enable the development server port. You can run this command and try again with the previous command

    adb reverse tcp:8081 tcp:8081

    EDIT: this solution only works for android devices and is among the hacks proposed in the question above. So it is improvable. However It is selected as valid answer until this happens.

    0 讨论(0)
  • 2020-12-13 02:27

    Here is what I do:

    Android:

    Add a script to your package.json:

    "android-shake": "adb shell input keyevent 82"
    

    You will then be able to call yarn android-shake to have the menu popup on Android (as long as the device is connected to the computer that is).

    iOS

    Settings -> Accessibility -> AssistiveTouch

    1. Turn it on.
    2. Tap on Customize Top Level Menu...
    3. Remove all icons but one, and set it to shake.

    That will give you a button that you can tap instead of shaking the device.

    I hope this can help others.

    0 讨论(0)
  • 2020-12-13 02:29

    I've created a lib that allows you to use 3 fingers touch instead of shake to open dev menu, when in development mode

    https://github.com/pie6k/react-native-dev-menu-on-touch

    You only have to wrap your app inside:

    import DevMenuOnTouch from 'react-native-dev-menu-on-touch';
    // or:  import { DevMenuOnTouch } from 'react-native-dev-menu-on-touch'
    
    class YourRootApp extends Component {
      render() {
        return (
          <DevMenuOnTouch>
            <YourApp />
          </DevMenuOnTouch>
        );
      }
    }
    

    It's really useful when you have to debug on real device and you have co-workers sitting next to you.

    0 讨论(0)
提交回复
热议问题