


How to debug React source code? Introduction to debugging methods using multiple tools
How to debug React source code? The following article will talk about how to debug React source code under various tools, and introduce how to debug the real source code of React in contributors, create-react-app, and vite projects. I hope it will be helpful to everyone!
clone React
Clone React locally and install dependencies.
git clone https://github.com/facebook/react.git
Compile project
If you just run a simple yarn build
, the sourcemap will not be generated. This is not what we want. We need to compile the sourcemap## required by the modern editor. #Map to actual source code for debugging.
scripts/rollup/build.js [Recommended learning:
vscode tutorial ,Programming Teaching】
②: The comment part cannot generate the sourcemap plug-in
ok , it looks like a lot, but in fact they are all roughly connected together (353-355, 387-415). Several plug-ins have been commented out. At this time we can build
yarn build
Note: If build If it fails, you will be prompted to install jdk. Just install it according to the error message.The successful results are as follows:
Perform debugger
We followOfficial DocumentWe learned that the basic development file directory is in /fixtures/packaging/babel-standalone/dev.html, so we first performed a simple debug configuration based on the html.
vscode
1. Create alaunch.json
launch.json Configuration
{ // 使用 IntelliSense 了解相关属性。 // 悬停以查看现有属性的描述。 // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ { "type": "chrome", "request": "launch", "name": "Open dev.html", // 这里路径可能不一样 做统一调整 修改为如下 "file": "${workspaceFolder}/fixtures/packaging/babel-standalone/dev.html" } ] }Then put a breakpoint in the source code, click to open debug and you will see that the source code is successfully run.
Note: We already have the sourcemap at this time, directly In/packages/react-dom
or
/packages/react, the code will run until the breakpoint is set
webstrom
webstrom is very simple. Just right-click on dev.html to debugdev.html:
Debug in create-react-app
We usually run projects based on create-react-app or vite, we can use npm link to debug Source code link. The above case is suitable for react developers and has met a small part of your needs. However, most of us usually run projects based on create-react-app or vite. We can link it through npm link. . ①: Create a create-react-app project ②: Linkreact,
react-dom to the global in the React project.
Note: You need to execute it according to your actual current location. In short, go toThe following is a successful one:build/node_modules/react
and
build/node_modules/react-domJust execute
npm linkseparately.
cd build/node_modules/react && npm link
cd .. && cd react-dom && npm link
③: 在create-react-app的项目中link react与react-dom
npm link react react-dom
大功小成,接下来开始正式的debugger.
vscode如何debugger
官方已经给出部分文档, 参考文档: 文档
- 先启动项目
yarn start
- 增加launch.json配置: 文档
注意:
1、如果你项目端口进行了修改,需要把上方的端口也做修改.
2、官方提供的是edge浏览器,如果你想改谷歌浏览器只需要把type修改为chrome
笔者的配置如下:
{ "version": "0.2.0", "configurations": [ { "type": "chrome", "request": "launch", "name": "调试creat-react-app源码", "url": "http://localhost:3000", "webRoot": "${workspaceFolder}" } ] }
- 找个位置打上断点进行调试
③、④为我打上断点的位置,点击 ⑤ 即可看到进入源码啦(可能要多点几下)
至此:我们已经大功告成,可以进行源码调试。
webstrom如何debugger
webstorm就显得十分简单,参考官方文档: 文档
- 打上断点
- 启动项目 yarn start
- 使用按键打开调试面板, 文档
调试React vite项目
和上方一致,也是通过link
总结:
调试源码的逻辑核心在于sourcemap,但是我们不难发现其中存在一些问题:
1、当前我们跨项目进行调试(源码在react项目中,我们的项目在另外一个文件中),导致类型管理出现问题, 这是基于开发项目的定义管理,不同编辑器表现不同.
- vscode
这是因为vscode默认解析ts的,但是不会默认识别flow的语法,所以这种的代码会解析成ts语法,就会报错,我们在项目增加
.vscode/settings.json
增加配置:
{ "javascript.validate.enable": false }
即表现正常,不再报错:
- webstorm
webstrom则是无法找到定义
更多关于VSCode的相关知识,请访问:vscode基础教程!
The above is the detailed content of How to debug React source code? Introduction to debugging methods using multiple tools. For more information, please follow other related articles on the PHP Chinese website!

Visual Studio is available in three versions: Community Free Edition is for individuals and small teams, Professional Paid Edition is for professional developers and small and medium teams, and Enterprise Ultimate Edition is for large enterprises and complex projects.

VisualStudio is highly valuable in .NET development because it is powerful and comprehensive. Despite the high cost and resource consumption, the efficiency improvement and development experience it brings is significant. Community is ideal for individual developers and small teams; large enterprises are suitable for Professional or Enterprise.

Free versions of VisualStudio include VisualStudioCommunity and VisualStudioCode. 1. VisualStudioCommunity is suitable for individual developers, open source projects and small teams. It is powerful and suitable for individual projects and learning programming. 2. VisualStudioCode is a lightweight code editor that supports multiple programming languages and extensions. It has a fast startup speed and low resource usage, making it suitable for developers who need flexibility and scalability.

The steps to install VisualStudio on Windows 8 are as follows: 1. Download the VisualStudioCommunity2019 installation package from the official Microsoft website. 2. Run the installer and select the required components. 3. It can be used after installation is completed. Be careful to select Windows 8-compatible components and make sure there is sufficient disk space and administrator rights.

VSCode can run on most modern computers as long as the basic system requirements are met: 1. Operating system: Windows 7 and above, macOS 10.9 and above, Linux; 2. Processor: 1.6GHz or faster; 3. Memory: at least 2GB RAM (4GB or higher recommended); 4. Storage space: at least 200MB of available space. By optimizing settings and reducing extended usage, you can get a smooth user experience on low-configuration computers.

To make the program run smoothly on Windows 8, the following steps are required: 1. Use compatibility mode, detect and enable this mode through code. 2. Adjust API calls and select the appropriate API according to the Windows version. 3. Perform performance optimization, try to avoid using compatibility mode, optimize API calls and use general controls.

Yes,VSCodeiscompatiblewithWindows8.1)DownloadtheinstallerfromtheVSCodewebsiteandensurethelatest.NETFrameworkisinstalled.2)Installextensionsusingthecommandline,notingsomemayloadslower.3)Manageperformancebyclosingunnecessaryextensions,usinglightweightt

VSCode is a lightweight code editor suitable for multiple languages and extensions; VisualStudio is a powerful IDE mainly used for .NET development. 1.VSCode is based on Electron, supports cross-platform, and uses the Monaco editor. 2. VisualStudio uses Microsoft's independent technology stack to integrate debugging and compiler. 3.VSCode is suitable for simple tasks, and VisualStudio is suitable for large projects.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Chinese version
Chinese version, very easy to use

Dreamweaver Mac version
Visual web development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Atom editor mac version download
The most popular open source editor