React调试——visual studio code
原文链接:Using React in Visual Studio Code
原文链接:Live edit and debug your React apps directly from VS Code — without leaving the editor
github源码示例:microsoft/vscode-react-sample
Welcome to React
you'll need Node.js JavaScript runtime and npm (Node.js package manager) installed. npm is included with Node.js which you can download and install from Node.js downloads.
Tip: To test that you have Node.js and npm correctly installed on your machine, you can type node --version and npm --version in a terminal or command prompt.
using the create-react-app generator for this tutorial
To install the create-react-app generator, in a terminal or command prompt type:
npm install -g create-react-app
This may take a few minutes to install. You can now create a new React application by typing:
create-react-app my-app
where my-app is the name of the folder for your application. This may take a few minutes to create the React application and install its dependencies.
Let's quickly run our React application by navigating to the new folder and typing npm start to start the web server and open the application in a browser:
cd my-app
npm start
You should see the React logo and a link to "Learn React" on http://localhost:3000 in your browser. We'll leave the web server running while we look at the application with VS Code.
To open your React application in VS Code, open another terminal or command prompt window, navigate to the my-app folder and type code .:
cd my-app
code .
Markdown preview
In the File Explorer, one file you'll see is the application README.md Markdown file. This has lots of great information about the application and React in general.
A nice way to review the README is by using the VS Code Markdown Preview. You can open the preview in either the current editor group (Markdown: Open Preview Ctrl+Shift+V) or in a new editor group to the side (Markdown: Open Preview to the Side Ctrl+K V). You'll get nice formatting, hyperlink navigation to headers, and syntax highlighting in code blocks.

Syntax highlighting and bracket matching
Now expand the src folder and select the index.js file. You'll notice that VS Code has syntax highlighting for the various source code elements and, if you put the cursor on a parenthesis, the matching bracket is also selected.

After you select a suggestion and type ., you see the types and methods on the object through IntelliSense.

……
Debugging React
To debug the client side React code, we'll need to install the Debugger for Chromeextension.
Note: This tutorial assumes you have the Chrome browser installed. Microsoft also publishes a version of this extension for their Edge browser.
……
Configure the Chrome debugger
We need to initially configure the debugger. To do so, go to the Debug view (Ctrl+Shift+D) and click on the gear button to create a launch.json debugger configuration file. Choose Chrome from the Select Environment drop-down list. This will create a launch.json file in a new .vscode folder in your project which includes a configuration to launch the website.
We need to make one change for our example: change the port of the url from 8080to 3000. Your launch.json should look like this:
{
"version": "0.2.0",
"configurations": [
{
"type": "chrome",
"request": "launch",
"name": "Launch Chrome against localhost",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}"
}
]
}
……
Live editing and debugging
If you are using webpack together with your React app, you can have a more efficient workflow by taking advantage of webpack's HMR mechanism which enables you to have live editing and debugging directly from VS Code. You can learn more in this Live edit and debug your React apps directly from VS Code blog post and the webpack Hot Module Replacement documentation.
Linting
Linters analyze your source code and can warn you about potential problems before you run your application. The JavaScript language services included with VS Code has syntax error checking support by default which you can see in action in the Problemspanel (View > Problems Ctrl+Shift+M).
Try making a small error in your React source code and you'll see a red squiggle and an error in the Problems panel.

Linters can provide more sophisticated analysis, enforcing coding conventions and detecting anti-patterns. A popular JavaScript linter is ESLint. ESLint, when combined with the ESLint VS Code extension, provides a great in-product linting experience.
First, install the ESLint command-line tool:
npm install -g eslint
Then install the ESLint extension by going to the Extensions view and typing 'eslint'.

Once the ESLint extension is installed and VS Code reloaded, you'll want to create an ESLint configuration file .eslintrc.json. You can create one using the extension's ESLint: Create ESLint configuration command from the Command Palette(Ctrl+Shift+P).

The command will create a .eslintrc.json file in your project root:
{
"env": {
"browser": true,
"commonjs": true,
"es6": true,
"node": true
},
"parserOptions": {
"ecmaFeatures": {
"jsx": true
},
"sourceType": "module"
},
"rules": {
"no-const-assign": "warn",
"no-this-before-super": "warn",
"no-undef": "warn",
"no-unreachable": "warn",
"no-unused-vars": "warn",
"constructor-super": "warn",
"valid-typeof": "warn"
}
}
ESLint will now analyze open files and shows a warning in index.js about 'App' being defined but never used.

You can modify the ESLint rules and the ESLint extension provides IntelliSense in .eslintrc.json.

Let's add an error rule for extra semi-colons:
"rules": {
"no-const-assign": "warn",
"no-this-before-super": "warn",
"no-undef": "warn",
"no-unreachable": "warn",
"no-unused-vars": "warn",
"constructor-super": "warn",
"valid-typeof": "warn",
"no-extra-semi":"error"
}
Now when you mistakenly have multiple semicolons on a line, you'll see an error (red squiggle) in the editor and error entry in the Problems panel.

Popular Starter Kits
In this tutorial, we used the create-react-app generator to create a simple React application. There are lots of great samples and starter kits available to help build your first React application
VS Code React Sample
This is a sample React application used for a demo at this year's //Build conference. The sample creates a simple TODO application and includes the source code for a Node.js Express server. It also shows how to use the Babel ES6 transpiler and then use webpackto bundle the site assets
MERN Starter
If you'd like to see a full MERN (MongoDB, Express, React, Node.js) stack example, look at the MERN Starter. You'll need to install and start MongoDB but you'll quickly have a MERN application running. There is helpful VS Code-specific documentation at vscode-recipes which details setting up Node.js server debugging. VS Code also has great MongoDB support through the Azure Cosmos DB extension
TypeScript React
If you're curious about TypeScript and React, you can also create a TypeScript version of the create-react-app application. See the details at TypeScript-React-Starter on the TypeScript Quick Start site.
Angular
Angular is another popular web framework. If you'd like to see an example of Angular working with VS Code, check out the Chrome Debugging with Angular CLI recipe. It will walk you through creating an Angular application and configuring the launch.jsonfile for the Debugger for Chrome extensio
Common questions
Can I get IntelliSense within declarative JSX?
Yes. For example, if you open the create-react-app project's App.js file, you can see IntelliSense within the React JSX in the render() method.

In our most recent release of ourChrome debugger for VS Code, we have landed a bunch of improvements to our sourcemapping-engine, which enables us to support live-editing and debugging out of the box with create-react-app.
This enables you as a developer to write and debug your React code without leaving the editor, and most importantly it enables you to have a continuous development workflow, where context switching is minimal, as you don’t have to switch between tools.

You can now write code, set a breakpoints, make a changes to the code, and debug your newly modified code — all from your editor
How to get started in 6 steps
- Download the latest release of VS Code and install our Chrome debugger
- Create your React app using create-react-app
- Use the following config for your
launch.jsonfile to configure the VS Code debugger and put it inside.vscodein your root folder.
{
"version": "0.2.0",
"configurations": [
{
"name": "Chrome",
"type": "chrome",
"request": "launch",
"url": "http://localhost:3000",
"webRoot": "${workspaceRoot}/src"
}
]
}
4. Start your React app by running npm start in your favorite terminal
5. Start debugging in VS Code by pressing F5or by clicking the green debug icon
Happy debugging!
Details
Our Chrome debugger now supports Webpack’s Hot Module Replacement mechanism, which pushes module changes to the browser by running a local file watcher.
Our debugger is now able to pickup these changes and re-applies the newly generated HMR sourcemap to the loaded source files on the fly. This enables the live editing and debugging experiences, without adding a need for more file watches or background tools.
React调试——visual studio code的更多相关文章
- 剖析并利用Visual Studio Code在Mac上编译、调试c#程序
0x00 前言 一周多以前的微软的Build大会上,微软发布了一个让很多人眼前一亮的工具,也是本文的主角——Visual Studio Code.很多使用Windows的朋友都很高兴,认为又多了一个很 ...
- 基于Visual Studio Code搭建Golang开发调试环境【非转载】
由于对Docker+kubernetes的使用及持续关注,要理解这个平台的原理,势必需要对golang有一定的理解,基于此开始利用业余时间学习go,基础语法看完之后,搭建开发环境肯定是第一步,虽然能g ...
- windows下用visual studio code 调试go代码
http://www.golangtc.com/download下载安装包或压缩包 配置环境变量 配置GOROOT: 配置PATH:在PATH最后添加 配置GOPATH:GOPATH的作用请自行百度, ...
- 在Mac上使用Visual Studio Code开发/调试.NET Core代码
.Net Core 1.0终于发布了,Core的一大卖点就是跨平台.这个跨平台不只是跨平台运行,而且可以跨平台开发.今天抽空研究了下在Mac下如何使用VS Code来开发.NET Core程序,并且调 ...
- visual studio code 里调试运行 Python代码
最近对微软的visual studio code 挺感兴趣的,微软的跨平台开发工具.轻量简洁. 版本迭代的也挺快的,截止16年8月2日已经1.3.1版本了,功能也愈加完善.(16年12月18日 已经, ...
- ubuntu下使用visual studio code来编译和调试C++
最近想在linux上编译c++代码,自己却一直习惯window上的IDE.以前公司要我写Linux代码的时候,我一般都是用eclipse + CDT,而eclipse这东西吧,我个人感觉因为加载组件太 ...
- visual studio code(vscode) 调试php(转)
原文链接:http://www.cnblogs.com/CLR010/p/5276077.html visual studio code(vscode) 调试php 1.下载vscode (vis ...
- 剖析并利用Visual Studio Code在Mac上编译、调试c#程序【转】
0x00 前言 一周多以前的微软的Build大会上,微软发布了一个让很多人眼前一亮的工具,也是本文的主角——Visual Studio Code.很多使用Windows的朋友都很高兴,认为又多了一个很 ...
- Mac上使用Visual Studio Code开发/调试.NET Core代码
Mac上使用Visual Studio Code开发/调试.NET Core代码 .Net Core 1.0终于发布了,Core的一大卖点就是跨平台.这个跨平台不只是跨平台运行,而且可以跨平台开发.今 ...
随机推荐
- 企业级本地yum源配置方案详解
因目前企业生产网络禁止联网,对于使用Linux的我们来说,非常不方便,想要使用yum源都很困难,挂dvd又不能完全满足要求,所以自建一个企业级的yum源,定时从公网同步到本地,然后生产网络直接配置在本 ...
- Makefile:248: /usr/local/otp_src_18.1/make/x86_64-unknown-linux-gnu/otp_ded.mk: No such file
安装erlang的时候,使用make命令一直报这个错 Makefile:248: /usr/local/otp_src_18.1/make/x86_64-unknown-linux-gnu/otp_d ...
- vue app.xxx.js 较大问题
线上build之后发现app.XXX.js 文件特别大. 包我都改为cdn了 其他空间就是路由改为懒加载了. { path: '/a/b', name: 'ab', component: () =&g ...
- 40 | insert语句的锁为什么这么多?
在上一篇文章中,我提到 MySQL 对自增主键锁做了优化,尽量在申请到自增 id 以后,就释放自增锁. 因此,insert 语句是一个很轻量的操作.不过,这个结论对于“普通的 insert 语句”才有 ...
- AcWing P173 矩阵距离 题解
Analysis 就是一个裸的广搜,每次从是1的点开始找就好啦~~~ #include<iostream> #include<cstdio> #include<cstri ...
- bzoj 4319: cerc2008 Suffix reconstruction 贪心
如果字符集无限大的话直接按照 $sa$ 的顺序依次填即可. 由于字符集非常小,所以要尽量填相同的字符. 我们知道 $sa$ 数组,也就知道了 $rank$ 数组. 那么考虑添加排名为 $i$ 的字符: ...
- Centos 7.x 设置Lvs+ Keepalived
[实验环境] Centos 7.2 Nginx 以下为本次试验所使用的地址: VIP:192.168.136.100 LVS-1:192.168.136.170 LVS-2:192.168.136. ...
- 在Ubuntu Server上使用vtk处理体数据,直接得到渲染结果图片避免显示窗口
概述 需要调用vtk对体数据进行渲染处理,处理结果直接存为图片而不通过窗口显示. 直接使用vtkRenderWindow加上vtkWindowToImageFilter类写入,在调用渲染的过程中会出现 ...
- P1069 细胞分裂——数学题,质因数分解
P1069 细胞分裂 我们求的就是(x^k)|(m1^m2) k的最小值: 先给m1分解质因数,再给每个细胞分解: 如果m1有的质因数,细胞没有就跳过: 否则就记录答案: 注意整数除法下取整的原则: ...
- 30行左右代码实现一个类似httprunner的接口框架
框架的最终归宿往往是领域语言+模板解析. 首先先约定一种所要执行操作的表述格式.然后通过模板解析将描述语言转化为代码进行执行.例如,我们可以使用以下yaml文件描述多个步骤并且需要关联的接口: api ...