首页 > 解决方案 > React 导航:如何调试

问题描述

我有一个应用程序,我在其中配置了多条路线,并且一切正常,直到我配置的最新路线不起作用(显示错误的屏幕)。

我的问题是如何进行调试?没有打印错误日志,我找不到如何获取更多关于正在发生的事情的日志。我也不知道在哪里停止调试器以获取一些有用的信息。

标签: debuggingreact-nativereact-navigation

解决方案


在哪里记录

您可以使用这 2 个侦听器来记录某些内容:

willFocus- 屏幕将聚焦

componentDidMount() {
  this.props.navigation.addListener("willFocus", () => console.log(""));
}

willBlur- 屏幕将不聚焦

componentDidMount() {
  this.props.navigation.addListener("willBlur", () => console.log(""));
}

另外,请查看文档以查看更多听众。

记录什么

获取navigator事件发生时的状态:

let state = this.props.navigation.dangerouslyGetState();

从这里,您可以看到可用的路线state.routeNames。您还可以goBack使用state.routes.


推荐阅读