当应用程序在应用模拟器中运行时,如何用React Native调试他们的React代码?


当前回答

假设你想在Android模拟器上显示这个菜单

然后,尝试⌘+m在Mac上的Android模拟器上弹出这个开发设置对话框。 如果没有显示,那么转到AVD >(点击笔编辑模拟器配置)>高级设置>检查启用键盘输入框。 然后重试⌘+m。 如果它没有显示静止,那么去运行模拟器设置和发送键盘快捷键到组合框/下拉菜单,然后选择模拟器控件(默认)选项。

然后重试⌘+m。 我希望这对你有帮助,它对我有用。

其他回答

在模拟器- react-native Run -ios中运行你的应用程序 按ctrl + d,然后点击Debug JS remote

网页应该在http://localhost:8081/debugger-ui打开,如果没有输入URL,在Chrome中进入这个链接 右键点击页面,点击检查,它应该打开chrome开发工具

转到顶部菜单中的源代码,在右侧的文件资源管理器中找到你的js类文件 您可以将断点放到视图中,并在其中调试代码,如图所示。

我使用这个库来调试react-native项目

https://github.com/jhen0409/react-native-debugger 它包括

包括React -devtools-core中的React Inspector。 包括回家的 DevTools,用redux-devtools-extension做了相同的API。

或者你可以用

For IOS $ react-native log-ios
For Android $ react-native log-android

你也可以使用自定义库,如果你不想摇你的真实手机每2分钟

我创建了一个库,允许你在开发模式下使用3个手指触摸而不是摇晃来打开开发菜单

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

你只需要在里面包装你的应用程序:

从react-native-dev-menu-on-touch导入DevMenuOnTouch; //或:import {DevMenuOnTouch} from 'react-native-dev-menu-on-touch'

class YourRootApp extends Component {
  render() {
    return (
      <DevMenuOnTouch>
        <YourApp />
      </DevMenuOnTouch>
    );
  }
}

当你必须在真正的设备上调试并且你有同事坐在你旁边时,它真的很有用。

我没有足够的声誉来评论之前的答案,这些答案都很好。:) 以下是我在开发react-native应用程序时调试的一些方法。

Live reloading react-native makes it super easy to see your changes with the ⌘ + R keys or even just enable live reload and watchman will "refresh" the simulator with the latest changes. If you get an error, you can get a clue from the line number from that red screen. A couple of undo will get you back to working state and start again. console.log('yeah, seriously.') I find myself prefer letting the program run and logging some informations than adding a debugger break point. (tough debugger is useful when trying to work with external packages/libraries and it comes with autocompletion, so you know what other methods you can utilise.) Enable Chrome Debugging with debugger; break point in your program.

这取决于您遇到的错误类型以及您对如何调试的偏好。对于大多数未定义的不是一个对象(评估'something.something'),方法1和2对我来说已经足够好了。

然而,处理由其他开发人员编写的外部库或包将需要更多的努力来调试,因此有一个像Chrome Debugging这样的好工具

有时它来自react-native平台本身,所以谷歌一下react-native问题肯定会有帮助。

希望这能帮助到一些人。

假设你想在Android模拟器上显示这个菜单

然后,尝试⌘+m在Mac上的Android模拟器上弹出这个开发设置对话框。 如果没有显示,那么转到AVD >(点击笔编辑模拟器配置)>高级设置>检查启用键盘输入框。 然后重试⌘+m。 如果它没有显示静止,那么去运行模拟器设置和发送键盘快捷键到组合框/下拉菜单,然后选择模拟器控件(默认)选项。

然后重试⌘+m。 我希望这对你有帮助,它对我有用。