.babelrc

{

"presets": [ "react-native" ],

"sourceMaps": true

}

Many JavaScript developers are used to simply log stuff into the console when debugging their software. This works kinda okay. Sadly it seems the console feature on React-Native isn't that sophisticated as in state of the art browsers. Often, big outputs are simply swallowed or you simply get the beloved  [object Object] which hasn't much information to provide either. Sometimes you can't even use  JSON.stringify() to get around this.

But as you may expected, there is a better way, a  debugger .

What

Simply speaking, a debugger is a program that lets you put  breakpoints at lines of your source code and watch the values of specific variables. While I have the feeling that logging still seems the way to go for most JS developers, most browsers and Node.js provide you with debug interfaces or fully integrated graphical interfaces. So it shouldn't be a foreign concept.

Why

In my opinion, debugging brings the most fun when used with the tool you wrote your code in the first place. For me this is currently  VSCode .

VSCode comes with a huge  extension eco-system , one of these happens to be  specifically for react-native and is even created by Microsoft itself, probably because  they also build things with RN .

While it comes with a whole bunch of features, the one I care about the most is a nice debugger integration.

How

I expect you to already have VSCode and the React-Native CLI installed.

First, you need to install the extension into your VSCode via the  Extensions icon on the left menu. The extension is called  React Native Tools .

Then you need to create (or update) your  .babelrc . These are the basic settings that have to be included:


After that you need to create a debug environment by clicking on the  Debug icon on the left menu and then on the gear. This will present you a few debug environments to choose from, here you choose  React Native .

This will generate a  launch.json inside a  .vscode directory for you, which has a few configurations that will be used, when you start a debugging session.

After this, you simply need to set a breakpoint somewhere in your code, select one of the configurations in the select box besides the green run arrow in the debug view and click the arrow. You'll notice that the names in the select box correspond to the names in the launch.json .

Pitfalls

While this seems all straight forward, I encountered 2 problems.

One was the fact, that you need to explicitly  enable debugging in your app via the  React-Native Debug Menu , on the iOS simulator it's opened with  cmd+d . Otherwise your breakpoints will be gray and the debugger won't stop.

The other one is that breakpoints only worked inside React's component methods. When I set a breakpoint at a place where I defined a variable, class or function in the top scope of a .js file/module it would not be hit. I didn't get around this problem at the moment.

Conclusion

Debugging is much nicer than logging. It gives you direct insight of your runtime code, while letting you stay in your beloved editor, it also doesn't require you to write additional logging code you would have to delete later.

https://www.tuicool.com/articles/mYniMjn

react native断点调试--Debug React-Native with VSCode的更多相关文章

  1. React Native 断点调试 跨域资源加载出错问题的原因分析

    写在前面 ————如果从头开始看还没解决,试试文章最后的绝招 闲来无事,折腾了一下React Native,相比之前,开发体验好了不少.但在真机断点调试那里遇到了跨域资源加载出错的问题,一番探索总算解 ...

  2. Visual Studio 2015 MFC之Button颜色变化-断点调试(Debug)

    软件开发,对自己的程序进行调试很重要,本次文章在上一边随笔的基础上,介绍一下Button控件做显示灯的用法,Button控件的添加和变量设置等可以参考下面的的链接:Visaul Studio 2015 ...

  3. Spring Boot项目使用Eclipse进行断点调试Debug

    1.在命令行下定位到项目根目录,启动Spring Boot项目,命令如下: java -Xdebug -Xrunjdwp:server=y,transport=dt_socket,address=80 ...

  4. AS 断点调试 debug

    debug面板 点击下图工具栏开启调试会话 此种调试方式是通过冻结应用运行的状态,仿佛时间停止了一般,然后我们逐一观察此时程序的各个参数是否符合我们的预期. 这种调试方法适用于对时间不敏感的程序.也就 ...

  5. Tomcatd断点调试Debug

    ideaDebug设置

  6. React Native:真机断点调试+跨域资源加载出错问题解决

    写在前面 闲来无事,折腾了一下React Native,相比之前,开发体验好了不少.但在真机断点调试那里遇到了跨域资源加载出错的问题,一番探索总算解决,目测是RN新版本调试服务的bug. 遇到类似问题 ...

  7. React Native 入门 调试项目

    不管时用哪种语言,哪种框架,调试永远都是一个避不开的话题 为我们提供了远程调试的功能,而这个功能需要Chrome浏览器的配合. 1. 首先浏览器一定要安装好React Developer Tool 插 ...

  8. Hybrid APP基础篇(二)->Native、Hybrid、React Native、Web App方案的分析比较

    说明 Native.Hybrid.React.Web App方案的分析比较 目录 前言 参考来源 前置技术要求 楔子 几种APP开发模式 概述 Native App Web App Hybrid Ap ...

  9. 利用 Create React Native App 快速创建 React Native 应用

    本文介绍的 Create-React-Native-App 是非常 Awesome 的工具,而其背后的 Expo 整个平台也让笔者感觉非常的不错.笔者目前公司是采用 APICloud 进行移动应用开发 ...

随机推荐

  1. Python3第三方组件最新版本追踪实现

    一.说明 在安全基线中有一项要求就是注意软件版本是否是最新版本,检查是否是最新版本有两方面的工作一是查看当前使用的软件版本二是当前使用软件的最新版本.在之前的“安全基线自动化扫描.生成报告.加固的实现 ...

  2. 【C++】C++中基类的析构函数为什么要用virtual虚析构函数?

    正面回答: 当基类的析构函数不是虚函数,并且基类指针指向一个派生类对象,然后通过基类指针来删除这个派生类对象时,如果基类的析构函数不是虚析构函数,那么派生类的析构函数就不会被调用,从而产生内存泄漏 # ...

  3. 文本分类(TextCNN,Keras)

    数据集是网上找的,已上传至我的 Github,项目完整地址:https://github.com/cyandn/practice/tree/master/text-classification 流程: ...

  4. [转帖]美团在Redis上踩过的一些坑-5.redis cluster遇到的一些问题

    美团在Redis上踩过的一些坑-5.redis cluster遇到的一些问题 博客分类: redis 运维 redis clustercluster-node-timeoutfailover  转载请 ...

  5. FMC与FPGA双口ram通讯

    硬件环境:ARM+FPGA通过FMC互联,STM32F767和 EP4CE15F23I7 FMC设置,STM的系统时钟HCLK为216MHz /* FMC initialization functio ...

  6. go 学习笔记---chan

    如果说 goroutine 是 Go语言程序的并发体的话,那么 channels 就是它们之间的通信机制.一个 channels 是一个通信机制,它可以让一个 goroutine 通过它给另一个 go ...

  7. SQL Server 新增自动执行任务

    第一步右击SQL Server代理,新建作业 第二步选择常规,给你要执行的计划命名 第三步选择步骤,然后给步骤命名,选择类型,数据库,输入你要执行的语句. 第四步设置要执行的频率,根据业务需要,一般建 ...

  8. rsyslog详解实战和避坑

    目标是要把线上环境的debug日志及集中化收集起来,一方面是方便开发调试:一方面是避免直接到线上环境查看,存在安全隐患. 常用可选方案: rsyslog发送端 + rsyslog接收端: 直接存在接收 ...

  9. python --- Socketserver N部曲(1)

    曲一 socketserver 是为了简化服务器端开发而产生的,是一个高级的标准库.(背景介绍完毕,开始干) 一些概念 来自源码的一张图片,简洁又FengSao +------------+ | Ba ...

  10. 阿里sentinel源码研究深入

    1. 阿里sentinel源码研究深入 1.1. 前言 昨天已经把sentinel成功部署到线上环境,可参考我上篇博文,该走的坑也都走了一遍,已经可以初步使用它的限流和降级功能,根据我目前的实践,限流 ...