React学习笔记--程序调试
React学习笔记
二 程序调试
前面我们搭建好了React的基本开发环境,可以编写基本的React js程序了。完整的开发环境肯定包含调试器,怎么调试用React编写的JS程序呢?有浏览器,比如Firefox,Chrome,按F12,找到javaScript脚本,打断点,然后调试。这是调试JavaScript的基本环境,但是React由于使用了ES6的语法,在浏览器中经过Babel解析,或者在发布前就将React的代码编译成了ES5规范的JavaScript代码,我们调试时怎么能调试到自己的写的React代码呢?
前面我曾经提到使用Webpack打包发布React程序,那么我们能否借助Webpack来运行调试React程序呢?答案时肯定的。
- 首先我们在项目目录下安装相关的Babel,Webpack模块
npm init
npm install --save-dev webpack webpack-dev-server
npm install --save-dev react react-dom
npm install --save-dev babel-core babel-preset-react babel-preset-es2015 babel-loader babel-cli babel-plugin-transform-runtime
2.修改package.json
{
"name": "demo1",
"version": "1.0.0",
"main": "webpack.config.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"devDependencies": {
"babel-cli": "^6.26.0",
"babel-core": "^6.26.0",
"babel-loader": "^6.4.1",
"babel-plugin-transform-runtime": "^6.23.0",
"babel-preset-es2015": "^6.24.1",
"babel-preset-react": "^6.24.1",
"react": "^15.6.2",
"react-dom": "^15.6.2",
"webpack": "^2.2.1",
"webpack-dev-server": "^2.9.7"
},
"dependencies": {
"react": "^15.6.2",
"react-dom": "^15.6.2",
"react-router-dom": "^4.2.2",
"webpack-dev-server": "^2.4.1"
},
"presets": [
"react"
],
"author": "",
"license": "ISC",
"description": ""
}
3.项目下填加webpack.config.js
module.exports = {
devtool: 'source-map',//这个配置很重要,告诉Webpack生成map.js,只有加入此配置才能在浏览器中真正调试React Js
entry: './src/test.jsx',
output: {
filename: './lib/bundle.js'
},
module: {
loaders: [
{
test: /\.js[x]?$/,
exclude: /node_modules/,
loader: 'babel-loader',
query: {
presets: ['es2015', 'react']
}
}
]
}
}
4..babelrc填加如下配置
{
"presets": ["es2015","react"],
"plugins": ["transform-runtime"],
"comments": true
}
5.项目子目录src中创建test.jsx
import React from "react"
import ReactDOM from "react-dom"
var HelloMessage = React.createClass({
render: function () {
return <h1>Hello World!</h1>;
}
});
ReactDOM.render(
<HelloMessage />,
document.getElementById('test')
);
6.创建index.htm,其中script直接引用webpack输出的js
<!DOCTYPE html>
<html>
<body>
<div id="test"></div>
<script src="../lib/bundle.js"></script>
</body>
</html>
7.在console中的项目目录下运行webpack,可以看到在lib子目录下生成了bundle.js和bundle.map.js
PS D:\DEV\react\demo1> webpack
(node:9404) DeprecationWarning: loaderUtils.parseQuery() received a non-string value which can be problematic, see https://github.com/webpack/loader-utils/issues/56
parseQuery() will be replaced with getOptions() in the next major version of loader-utils.
Hash: 47d9ffef6066b3ac6378
Version: webpack 2.7.0
Time: 2131ms
Asset Size Chunks Chunk Names
./lib/bundle.js 779 kB 0 [emitted] [big] main
./lib/bundle.js.map 942 kB 0 [emitted] main
[28] ./~/react/lib/React.js 4.96 kB {0} [built]
[77] ./~/babel-runtime/helpers/typeof.js 1.07 kB {0} [built]
[122] ./~/babel-runtime/core-js/object/get-prototype-of.js 104 bytes {0} [built]
[123] ./~/babel-runtime/helpers/classCallCheck.js 208 bytes {0} [built]
[124] ./~/babel-runtime/helpers/createClass.js 904 bytes {0} [built]
[125] ./~/babel-runtime/helpers/inherits.js 1.11 kB {0} [built]
[126] ./~/babel-runtime/helpers/possibleConstructorReturn.js 542 bytes {0} [built]
[127] ./~/react-dom/index.js 59 bytes {0} [built]
[128] ./~/react/react.js 56 bytes {0} [built]
[129] ./src/test.jsx 5.21 kB {0} [built]
[130] ./~/babel-runtime/core-js/object/create.js 94 bytes {0} [built]
[131] ./~/babel-runtime/core-js/object/define-property.js 103 bytes {0} [built]
[132] ./~/babel-runtime/core-js/object/set-prototype-of.js 104 bytes {0} [built]
[137] ./~/babel-runtime/~/core-js/library/fn/object/get-prototype-of.js 125 bytes {0} [built]
[199] ./~/react-dom/lib/ReactDOM.js 5.05 kB {0} [built]
+ 254 hidden modules
8.启动webpack-dev-server,访问http://localhost:8080就可以看到helloword了。
PS D:\DEV\react\demo1> webpack-dev-server --inline
Project is running at http://localhost:8080/
webpack output is served from /
(node:4340) DeprecationWarning: loaderUtils.parseQuery() received a non-string value which can be problematic, see https://github.com/webpack/loader-utils/issues/56
parseQuery() will be replaced with getOptions() in the next major version of loader-utils.
Hash: 64a0de3cefb7972eab47
Version: webpack 2.7.0
Time: 2449ms
Asset Size Chunks Chunk Names
./lib/bundle.js 1.1 MB 0 [emitted] [big] main
./lib/bundle.js.map 1.32 MB 0 [emitted] main
chunk {0} ./lib/bundle.js, ./lib/bundle.js.map (main) 1.07 MB [entry] [rendered]
[124] ./src/test.jsx 5.21 kB {0} [built]
[125] (webpack)-dev-server/client?http://localhost:8080 7.95 kB {0} [built]
[130] ./~/babel-runtime/core-js/object/get-prototype-of.js 104 bytes {0} [built]
[134] ./~/babel-runtime/helpers/classCallCheck.js 208 bytes {0} [built]
[135] ./~/babel-runtime/helpers/createClass.js 904 bytes {0} [built]
[136] ./~/babel-runtime/helpers/inherits.js 1.11 kB {0} [built]
[137] ./~/babel-runtime/helpers/possibleConstructorReturn.js 542 bytes {0} [built]
[198] ./~/react-dom/index.js 59 bytes {0} [built]
[282] ./~/react/react.js 56 bytes {0} [built]
[284] ./~/strip-ansi/index.js 161 bytes {0} [built]
[285] ./~/url/url.js 23.3 kB {0} [built]
[287] (webpack)-dev-server/client/overlay.js 3.73 kB {0} [built]
[290] (webpack)/hot nonrecursive ^\.\/log$ 160 bytes {0} [built]
[291] (webpack)/hot/emitter.js 77 bytes {0} [built]
[292] multi (webpack)-dev
9.打开firefox(本人使用firefox浏览器进行调试),在调试器左侧资源栏中可以找到webpack相关的资源,找到自己编写的test.jsx,打上断点,就可以像调试ES5一样调试自己写的React代码了。
React学习笔记--程序调试的更多相关文章
- react学习笔记1--基础知识
什么是react A JAVASCRIPT LIBRARY FOR BUILDING USER INTERFACES[React是一个用于构建用户界面的JavaScript库.] React之所以快, ...
- React学习笔记(一)- 入门笔记
React入门指南 作者:狐狸家的鱼 本文链接:React学习笔记 GitHub:sueRimn 1.组件内部状态state的修改 修改组件的每个状态,组件的render()方法都会再次运行.这样就可 ...
- React学习笔记(七)条件渲染
React学习笔记(七) 六.条件渲染 使用if或条件运算符来创建表示当前状态的元素. 可以使用变量来存储元素.比如: let button = null; if (isLoggedIn) { but ...
- React学习笔记(六)事件处理
React学习笔记(六) 五.事件处理 React事件绑定属性的命名采用驼峰写法,不同于传统DOM全部小写. 如果采用JSX的语法,事件函数需要用大括号{}包裹函数名,不同于传统DOM字符串小括号的方 ...
- React学习笔记(五)State&声明周期
React学习笔记(五) 四.State&声明周期 可以为组件添加"状态(state)".状态与属性相似,但是状态是私有的,完全受控于当前组件. 局部状态就是只能用于类(定 ...
- React学习笔记 - 组件&Props
React Learn Note 4 React学习笔记(四) 标签(空格分隔): React JavaScript 三.组件&Props 组件可以将UI切分成一些独立的.可复用的部件,这样你 ...
- React学习笔记 - 元素渲染
React Learn Note 3 React学习笔记(三) 标签(空格分隔): React JavaScript 二.元素渲染 元素是构成react应用的最小单位. 元素是普通的对象. 元素是构成 ...
- React学习笔记 - JSX简介
React Learn Note 2 React学习笔记(二) 标签(空格分隔): React JavaScript 一.JSX简介 像const element = <h1>Hello ...
- React学习笔记 - Hello World
React Learn Note 1 React学习笔记(一) 标签(空格分隔): React JavaScript 前.Hello World 1. 创建单页面应用 使用Create React A ...
随机推荐
- MFC属性表单修改“应用”键名并对其响应
1.重载CPropertySheet的虚函数OnInitDialog(),添加如下代码 BOOL DialogInputData::OnInitDialog() { BOOL bResult = CP ...
- 暑假练习赛 007 B - Weird Cryptography
Weird Cryptography Description standard input/outputStatements Khaled was sitting in the garden unde ...
- Flex 基础语法(三)
2.flex-wrap 默认情况下,项目都排在一条线(又称"轴线")上.flex-wrap属性定义,如果一条轴线排不下,如何换行 属性 含义 nowrap(默认值) 不换行 wra ...
- Fiddler手机抓包图文教程
上篇Fiddler教程,我们教了大家Fiddler安装配置及如何使用Fiddler进行基本的Http抓包及模拟请求,今天给大家介绍下如何使用Fiddler进行手机抓包. 运行环境为Windows 10 ...
- java 中 针对数组进行的工具类
1.遍历数组的方法: public static void printfArray(int[] arr) 2. 获取数组中最大值: public static int getMax(int[] ar ...
- 使用 gulp-file-include 构建前端静态页面
前言 虽然现在单页面很流行,但是在 PC 端多页面还是常态,所以构建静态页面的工具还有用武之地.最近也看到了一些询问如何 include HTML 文件的问题. 很多时候我们在写静态页面的时候也希望能 ...
- IntelliJ配置jenkins服务的Crumb Data
近期在做jenkins测试,IntelliJ并没有自动安装jenkins服务器,因此需要自己添加,但是如果不配置Crumb Data,jenkins的服务就不能使用. 首先在服务器中开启CSRF服务, ...
- socket阻塞IO流程图
单线程 多线程
- Mac上查看隐藏文件夹/文件
一.查看隐藏文件夹: 可以直接在终端执行 open ~/文件夹名称 如: open ~/.ssh 二.查看隐藏文件: 在Finder下进入你想要操作的文件夹,按快捷键Command + F 调出搜索窗 ...
- cursor() — 数据库连接操作 python
python 操作数据库,要安装一个Python和数据库交互的包MySQL-python-1.2.2.win32-py2.5.exe,然后我们就可以使用MySQLdb这个包进行数据库操作了. ...