简体   繁体   English

当它在设备上运行时,你如何调试 react-native?

[英]How do you debug react-native when it is running on device?

react-native 在设备上运行时如何调试?

Figured our React Native debugging for Android.为 Android 设计了我们的 React Native 调试。 Steps to for someone struggling with this -解决这个问题的人的步骤 -

  1. Launch your RN App启动您的 RN 应用程序
  2. Shake device震动装置
  3. Select "Dev Settings"选择“开发设置”
  4. Select "Debug server host & port for device" Put in your system's IP address.选择“Debug server host & port for device”输入系统的IP地址。
  5. Shake device震动装置
  6. Select "Debug JS remotely"选择“远程调试JS”
  7. Chrome will open a new tab with the address " http://localhost:8081/debugger-ui " Chrome 将打开一个地址为“ http://localhost:8081/debugger-ui ”的新标签页
  8. If this doesn't happen, check your port and open a new tab and enter the above address with your port number.如果没有发生这种情况,请检查您的端口并打开一个新选项卡并输入带有您的端口号的上述地址。
  9. Open #Chrome DevTools (Cmd+Alt+I on #MacOSX)打开 #Chrome DevTools(#MacOSX 上的 Cmd+Alt+I)
  10. Select the Console Tab选择控制台选项卡
  11. Shake Device震动装置
  12. Select "Reload"选择“重新加载”

View all your console logs or use "debugger;"查看所有控制台日志或使用“调试器”; in your JS for breakpoint debugging.在您的 JS 中进行断点调试。

You have two options:您有两个选择:

  1. Debug remotely远程调试

iOS: Cmd + ctrl + z to open menu and select "Debug remotely" iOS:Cmd + ctrl + z 打开菜单并选择“远程调试”

Android: Cmd + M to open menu and select "Debug remotely" Android:Cmd + M 打开菜单并选择“远程调试”

  1. Or run one of these commands:或运行以下命令之一:

     react-native log-ios react-native log-android

There are little hack in here for IOS.对于 IOS,这里几乎没有什么 hack。 In XCode open AppDelegate.m from project folder, comment the line with jsCodeLocation declaration and add near it something like this:在 XCode 中,从项目文件夹中打开 AppDelegate.m,用 jsCodeLocation 声明注释该行并在它附近添加如下内容:

jsCodeLocation = [NSURL URLWithString:@"http://%YOUR_PC_IP%:8081/index.ios.bundle?platform=ios&dev=true"];

, where %YOUR_PC_IP% is IP of your dev machine on local net. ,其中 %YOUR_PC_IP% 是您的开发机器在本地网络上的 IP。

Then open project named RCTWebSocket wich located in Libraries folder and then open from this project file RCTWebSocketProjectExecutor.m.然后打开位于 Libraries 文件夹中名为 RCTWebSocket 的项目,然后从此项目文件 RCTWebSocketProjectExecutor.m 打开。 Next, comment the line with host declaration, then add near it something like that:接下来,用主机声明注释该行,然后在它附近添加如下内容:

host = @"%YOUR_PC_IP%";

Almost done.快完成了。 Run app on your device with XCode and make sure that your IPhone can see your develop machin via internet.使用 XCode 在您的设备上运行应用程序,并确保您的 iPhone 可以通过互联网看到您的开发机器。

After it launches make sure that node server is running (command "react-native -- --start" in root project folder) and open via chrome this page: http://localhost:8081/debugger-ui .启动后确保节点服务器正在运行(命令“react-native -- --start”在根项目文件夹中)并通过chrome打开这个页面: http://localhost:8081/debugger-ui Plus, option Debug JS Remotely must be enabled in your app on your IPhone.另外,必须在 iPhone 上的应用程序中启用选项 Debug JS Remotely。 (Shake action, then tap in opened menu Debug JS Remotely, that is it.) (摇动动作,然后点击打开的菜单“远程调试 JS”,就是这样。)

Now open dev console and have happy debugging.现在打开开发控制台并进行愉快的调试。

2 options exist:存在2 个选项:

Remote debug: this would go over network, so it can be laggy.远程调试:这将通过网络进行,因此可能会滞后。

  1. Run app on device (install it whilst its plugged in)在设备上运行应用程序(在插入时安装它)
  2. Shake device (Android devices need to be vigorously shaken )摇晃设备(安卓设备需要大力摇晃
  3. Press settings on the dev menu, and under Debugging , press Debug server host and port for device按 dev 菜单上的settings ,然后在Debugging下,按Debug server host and port for device
  4. Enter your debug server's IP address and port (usually 8081 unless you changed your settings of the debug server), eg 255.255.255.255:8081.输入您的调试服务器的 IP 地址和端口(通常为 8081,除非您更改了调试服务器的设置),例如 255.255.255.255:8081。 Your ip address is the local IP address that your computer has.您的 IP 地址是您的计算机拥有的本地 IP 地址。 Ensure the device is on the same network.确保设备在同一网络上。 For mac users, open System PreferencesNetworkWifi → Under Status: connected , there is an IP address, use that.对于 mac 用户,打开System PreferencesNetworkWifi → 在Status: connected ,有一个 IP 地址,使用它。
  5. You can unplug the device你可以拔掉设备
  6. Ensure debug mode is on (Shake device vigorously again, press Debug)确保调试模式打开(再次用力摇晃设备,按调试)

Wired debug: relatively better for slow connections有线调试:对于慢速连接相对更好

  1. Install app on phone在手机上安装应用
  2. If android, set up port forwarding with adb reverse tcp:8081 tcp:8081 and if iOS, follow the steps from RN docs )如果是 android,请使用adb reverse tcp:8081 tcp:8081设置端口转发,如果是 iOS,请按照RN docs 中的步骤操作)
  3. Enable debug on device在设备上启用调试

If you are working on Visual Studio Code then you can install React Native Tools extension (provided by Microsoft ) and debug your app like any other IDE.如果您正在使用Visual Studio Code,那么您可以安装React Native Tools扩展(由Microsoft提供)并像任何其他 IDE 一样调试您的应用程序。

Read all steps in detail in my this answer .在我的这个答案中详细阅读所有步骤。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM