本文翻译自:How to do logging in React Native?
如何为Web开发时在React Native中记录变量,例如使用console.log
?
#1楼
参考:https://stackoom.com/question/22MNo/如何在React-Native中记录日志
#2楼
console.log
works. console.log
有效。
By default on iOS, it logs to the debug pane inside Xcode. 默认情况下,在iOS上,它记录到Xcode内的调试窗格中。
From the IOS simulator press ( ⌘ + D ) and press Remote JS Debugging . 在IOS模拟器中,按( ⌘ + D ),然后按Remote JS Debugging 。 This will open a resource, http://localhost:8081/debugger-ui on localhost. 这将在本地主机上打开资源http:// localhost:8081 / debugger-ui 。 From there use Chrome Developer tools javascript console to view console.log
从那里使用Chrome Developer工具javascript控制台查看console.log
#3楼
I had the same issue: console messages were not appearing in XCode's debug area. 我遇到了同样的问题:控制台消息未出现在XCode的调试区域中。 In my app I did cmd-d to bring up the debug menu, and remembered I had set "Debug in Safari" on. 在我的应用程序中,我使用cmd-d调出了调试菜单,并记得我已将“在Safari中调试”设置为打开。
I turned this off, and some messages were printed to the output message, but not my console messages. 我关闭了此功能,并且一些消息被打印到输出消息中,但没有打印到控制台消息中。 However, one of the log messages said: 但是,其中一条日志消息说:
__DEV__ === false, development-level warning are OFF, performance optimizations are ON"
This was because I had previously bundled my project for testing on a real device with the command: 这是因为我以前使用以下命令将项目捆绑在一起,以便在实际设备上进行测试:
react-native bundle --minify
This bundled without "dev-mode" on. 这捆绑了没有“开发模式”。 To allow dev messages,include the --dev flag: 要允许开发人员消息,请包括--dev标志:
react-native bundle --dev
And console.log messages are back! 并且console.log消息又回来了! If you aren't bundling for a real device, don't forget to re-point jsCodeLocation
in AppDelegate.m
to localhost (I did!). 如果您不是要捆绑真实设备,请不要忘记将AppDelegate.m
jsCodeLocation
指向本地主机(我做到了!)。
#4楼
Visual Studio Code has a decent debug console that can show your console.log. Visual Studio Code有一个不错的调试控制台,可以显示您的console.log。
VS Code is, more often than not, React Native friendly. VS Code通常是React Native友好的。
#5楼
Use console.log
, console.warn
etc. 使用console.log
, console.warn
等。
As of React Native 0.29 you can simply run the following to see logs in the console: 从React Native 0.29开始,您只需运行以下命令即可在控制台中查看日志:
$ react-native log-ios
$ react-native log-android
#6楼
Press [command + control + Z] in Xcode Simulator, choose Debug JS Remotely, then press [command + option + J] to open Chrome developer tools. 在Xcode Simulator中按[command + control + Z],选择“远程调试JS”,然后按[command + option + J]打开Chrome开发者工具。
refer: Debugging React Native Apps 参考: 调试React Native Apps