React JavaScript Tutorial in Visual Studio Code
React JavaScript Tutorial in Visual Studio Code
Version 1.77 (/updates) is now available! Read about the new features and fixes from March. ×
✏ (https://vscode.dev/github/microsoft/vscode-docs/blob/main/docs/nodejs/reactjs-tutorial.md)
Using React in Visual Studio Code
React (https://reactjs.org) is a popular JavaScript library developed by Facebook for building user interfaces. The Visual Studio Code editor supports React.js IntelliSense
and code navigation out of the box.
Welcome to React
We'll be using the create-react-app generator (https://reactjs.org/docs/create-a-new-react-app.html#create-react-app) for this tutorial. To use the generator as well
as run the React application server, you'll need Node.js (https://nodejs.org/) JavaScript runtime and npm (https://www.npmjs.com/) (Node.js package manager) installed.
npm is included with Node.js which you can download and install from Node.js downloads (https://nodejs.org/en/download/).
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.
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.
Note: If you've previously installed create-react-app globally via npm install -g create-react-app , we recommend you uninstall the package using npm
uninstall -g create-react-app to ensure that npx always uses the latest version.
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 (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 .
https://code.visualstudio.com/docs/nodejs/reactjs-tutorial 1/11
02/04/2023, 00:07 React JavaScript Tutorial in Visual Studio 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 (/docs/languages/markdown#_markdown-preview). You can open the preview in either the current
editor group (Markdown: Open Preview ⇧⌘V ) or in a new editor group to the side (Markdown: Open Preview to the Side ⌘K V ). You'll get nice formatting, hyperlink
navigation to headers, and syntax highlighting in code blocks.
https://code.visualstudio.com/docs/nodejs/reactjs-tutorial 2/11
02/04/2023, 00:07 React JavaScript Tutorial in Visual Studio Code
IntelliSense
As you start typing in index.js , you'll see smart suggestions or completions.
After you select a suggestion and type . , you see the types and methods on the object through IntelliSense (/docs/editor/intellisense).
VS Code uses the TypeScript language service for its JavaScript code intelligence and it has a feature called Automatic Type Acquisition (/docs/nodejs/working-with-
javascript#_typings-and-automatic-type-acquisition) (ATA). ATA pulls down the npm Type Declaration files ( *.d.ts ) for the npm modules referenced in the
package.json .
https://code.visualstudio.com/docs/nodejs/reactjs-tutorial 3/11
02/04/2023, 00:07 React JavaScript Tutorial in Visual Studio Code
Hello World
Let's update the sample application to "Hello World!". Create a new H1 header with "Hello, world!" and replace the <App /> tag in ReactDOM.render with element .
https://code.visualstudio.com/docs/nodejs/reactjs-tutorial 4/11
02/04/2023, 00:07 React JavaScript Tutorial in Visual Studio Code
reportWebVitals();
Once you save the index.js file, the running instance of the server will update the web page and you'll see "Hello World!" when you refresh your browser.
Tip: VS Code supports Auto Save, which by default saves your files after a delay. Check the Auto Save option in the File menu to turn on Auto Save or directly configure
the files.autoSave user setting (/docs/getstarted/settings).
Debugging React
To debug the client side React code, we'll use the built-in JavaScript debugger.
Note: This tutorial assumes you have the Edge browser installed. If you want to debug using Chrome, replace the launch type with chrome . There is also a debugger
for the Firefox (https://marketplace.visualstudio.com/items?itemName=hbenl.vscode-firefox-debug) browser.
Set a breakpoint
To set a breakpoint in index.js , click on the gutter to the left of the line numbers. This will set a breakpoint which will be visible as a red circle.
We need to make one change for our example: change the port of the url from 8080 to 3000 . Your launch.json should look like this:
https://code.visualstudio.com/docs/nodejs/reactjs-tutorial 5/11
02/04/2023, 00:07 React JavaScript Tutorial in Visual Studio Code
{
"version": "0.2.0",
"configurations": [
{
"type": "msedge",
"request": "launch",
"name": "Launch Edge against localhost",
"url": "http://localhost:3000",
"webRoot": "${workspaceFolder}"
}
]
}
Ensure that your development server is running ( npm start ). Then press F5 or the green arrow to launch the debugger and open a new browser instance. The source
code where the breakpoint is set runs on startup before the debugger was attached, so we won't hit the breakpoint until we refresh the web page. Refresh the page and
you should hit your breakpoint.
You can step through your source code ( F10 ), inspect variables such as element , and see the call stack of the client side React application.
https://code.visualstudio.com/docs/nodejs/reactjs-tutorial 6/11
02/04/2023, 00:07 React JavaScript Tutorial in Visual Studio Code
For more information about the debugger and its available options, check out our documentation on browser debugging (/docs/nodejs/browser-debugging).
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 Problems panel (View > Problems ⇧⌘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.
https://code.visualstudio.com/docs/nodejs/reactjs-tutorial 7/11
02/04/2023, 00:07 React JavaScript Tutorial in Visual Studio Code
Linters can provide more sophisticated analysis, enforcing coding conventions and detecting anti-patterns. A popular JavaScript linter is ESLint (https://eslint.org/). ESLint,
when combined with the ESLint VS Code extension (https://marketplace.visualstudio.com/items/dbaeumer.vscode-eslint), provides a great in-product linting experience.
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.js . You can create one using the extension's
ESLint: Create ESLint configuration command from the Command Palette ( ⇧⌘P ).
https://code.visualstudio.com/docs/nodejs/reactjs-tutorial 8/11
02/04/2023, 00:07 React JavaScript Tutorial in Visual Studio Code
The command will prompt you to answer a series of questions in the Terminal panel. Take the defaults, and it will create a .eslintrc.js file in your project root that
looks something like this:
module.exports = {
env: {
browser: true,
es2020: true
},
extends: ['eslint:recommended', 'plugin:react/recommended'],
parserOptions: {
ecmaFeatures: {
jsx: true
},
ecmaVersion: 11,
sourceType: 'module'
},
plugins: ['react'],
rules: {}
};
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 (https://eslint.org/docs/rules/) in the .eslintrc.js file.
"rules": {
"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.
https://code.visualstudio.com/docs/nodejs/reactjs-tutorial 9/11
02/04/2023, 00:07 React JavaScript Tutorial in Visual Studio Code
TypeScript React
If you're curious about TypeScript and React, you can also create a TypeScript version of the create-react-app application by specifying that you want to use the
TypeScript template:
See the details at Adding TypeScript (https://create-react-app.dev/docs/adding-typescript) on the Create React App site (https://create-react-app.dev).
Angular
Angular (https://angular.io/) is another popular web framework. If you'd like to see an example of Angular working with VS Code, check out the Debugging with Angular
CLI (https://github.com/microsoft/vscode-recipes/tree/main/Angular-CLI) recipe. It will walk you through creating an Angular application and configuring the
launch.json file for the JavaScript debugger.
Common questions
https://code.visualstudio.com/docs/nodejs/reactjs-tutorial 10/11
02/04/2023, 00:07 React JavaScript Tutorial in Visual Studio Code
Yes No
3/30/2023
IN THIS ARTICLE
Welcome to React
Hello World
Debugging React
Linting
Common questions
Tweet(https://twitter.com/intent/tweet?original_referer=https://code.visualstudio.com/docs/nodejs/reactjs-
this tutorial&ref_src=twsrc%5Etfw&text=React%20JavaScript%20Tutorial%20in%20Visual%20Studio%20Code&tw_p=tweetbutton&url=https://code.visualstudio.com/d
link tutorial&via=code)
Subscribe(/feed.xml)
Ask questions(https://stackoverflow.com/questions/tagged/vscode)
Follow @code(https://go.microsoft.com/fwlink/?LinkID=533687)
Request features(https://go.microsoft.com/fwlink/?LinkID=533482)
Report issues(https://www.github.com/Microsoft/vscode/issues)
Watch videos(https://www.youtube.com/channel/UCs5Y5_7XK8HLDX0SLNwkd3w)
(https://www.microsoft.com)
© 2023 Microsoft
https://code.visualstudio.com/docs/nodejs/reactjs-tutorial 11/11