Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Developing React with VSCode

Developing React with VSCode

Visual Studio Code (abbreviated as VS Code) is a free, open-source code editor developed by Microsoft that supports multiple programming languages and provides features such as syntax highlighting, intelligent code completion, code refactoring, and debugging.

VS Code has a built-in terminal that lets you run command-line tools such as npm and yarn directly in the editor, making project building and management convenient.

If you want a richer Vue.js development environment, you can install the Volar and Volar for TypeScript extensions, which provide Vue.js intelligent hints.

If you are not yet familiar with VS Code or have not installed it, you can refer to:VSCode Tutorial。

Why choose VS Code for React development?


Downloading and Installing VS Code

- Windows: Download the .exe installer - macOS: Download the .dmg (drag to Applications) - Linux: Choose .deb or .rpm depending on your distribution - Windows/macOS: Double-click the installer and click Next/Continue throughout. - It is recommended to check "Add to PATH" and "Register as code editor" (for easy command-line opening). - Open VS Code, and you will see the welcome screen. - Windows users can type `code --version` in the command line to test.

Open Your React Project

  1. Open VS Code.

  2. Method 1: Menu File → Open Folder, select your project folder (for example my-first-react-app)。

  3. Method 2 (recommended): Enter the project directory in the terminal, then open it in one step:

    cd my-first-react-app
    

code .

(code . The command will open the current folder directly in VS Code)

Now you see the file explorer on the left, showing the project structure (such as src, public, etc.).

Installing Essential Extensions

The power of VS Code lies in its extensions. Click the Extensions icon (four squares) in the left activity bar, or press the shortcut Ctrl+Shift+X(macOS: Cmd+Shift+X) to open the Extensions marketplace.

Search for and install the following extensions (click Install):

- Real-time code standard checking, officially recommended by React. - After installation, errors are highlighted in red in the code. - Automatically formats code to keep a consistent style. - A handy code snippet tool! Type a shortcut to quickly generate components. - Common snippets: - `rafce` → Generate arrow function component + export default - `rfc` → Generate function component - `useState` → Quickly generate useState - Auto-completion for import paths (super useful when importing). - Brackets are distinguished by color, making nested JSX easier to read. - Git enhancement, view code commit history.

React-specific recommendations(Optional, advanced):

After installation, restart VS Code (or press Ctrl+Shift+P → Reload Window)。

Configure VS Code to improve the React development experience

Press Ctrl+Shift+P(macOS: Cmd+Shift+P) to open the Command Palette, type "Preferences: Open Settings (JSON)" to open settings.json。

Create in the project root directory .vscode/settings.json(Project-level configuration is recommended), add the following:

{
  // Auto-format on save
  "editor.formatOnSave": true,
  // Default to Prettier formatting
  "editor.defaultFormatter": "esbenp.prettier-vscode",

  // ESLint auto-fix
  "editor.codeActionsOnSave": {
    "source.fixAll.eslint": "explicit"
  },

  // JSX/TSX files also apply Prettier
  "[javascript]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },
  "[javascriptreact]": {
    "editor.defaultFormatter": "esbenp.prettier-vscode"
  },

  // Show line numbers, indentation guide
  "editor.rulers": [80],
  "editor.guides.indentation": true
}

Takes effect after saving. Now every time you save code, Prettier will automatically format it and ESLint will automatically fix common problems.

Validation: Deliberately write a piece of non-standard code (such as a missing semicolon), then save and see if it is automatically fixed.

Actually writing React code: creating a component from scratch

In the Explorer on the left, right-click src the folder → New Folder → Create components folder.

Right-click components → New File → Create Greeting.jsx。

Use code snippets to quickly generate a component:

import React from 'react'

const Greeting = () => {
  return (
    
Greeting
) } export default Greeting

Modify it into a complete component (demonstrating state and Props):

Example

import

{

useState

}

from

'react'

;

const

Greeting

=

(

{

name

=

'Stranger'

}

)

=>

{

 

const

[

count

,

setCount

]

=

useState

(

0

)

;

 

return

(

   

<

div style

=

{

{

padding

:

'20px'

,

border

:

'1px solid #ccc'

}

}

     

<
h2
Hello,
{
name
}

!

h2

     

<

p

You have clicked

{

count

}

times

p

     

<

button onClick

=

{

(

)

=>

setCount

(

count

1

)

}

        Click me

1

     

button

   

div

 

)

;

}

;

export

default

Greeting

;

in src/App.jsx Import and use it in:

Example

import

Greeting from

'./components/Greeting.jsx'

;

// Add in return:

<
div
<

Greeting name

=

"Runoob"

/>

 

<
Greeting
/>
{

/* Use default name */

}

div

Save all files (Ctrl+S), switch to the browser, and hot reload will automatically display the new component!

Experience highlights:

Integrated terminal and debugging tips

Built-in terminal: Menu Terminal → New Terminal, or Ctrl+` (backtick).

Debugging React code:

  1. Install the browser extension React Developer Tools(if not already installed).
  2. In VS Code, press F5 to start debugging (requires configuring launch.json first, but at the beginner stage, use browser DevTools first).
  3. In the browser, press F12 → the Components tab to view the Props and state of your Greeting component.

Common shortcuts(must memorize):

Small exercise: build a card component

  1. creation components/Card.jsx, use rafce to generate.
  2. to implement a card component that receives three Props: title, content, and imageUrl.
  3. Use it twice in App.jsx, showing different content.
  4. Add some inline styles for beautification.

When done, your page will have multiple beautiful cards!

Step 1: Create the Card.jsx component

  1. in src Under the folder, right-click → New Folder → Create components folder (if it doesn't exist yet).

  2. Right-click components folder → New File → Create Card.jsx。

  3. in Card.jsx , enter rafce(press Tab to auto-generate the template), then modify it into the following code:

Card.jsx file code:

import

React from

'react'

;

const

Card

=

(

{

title

,

content

,

imageUrl

}

)

=>

{

 

return

(

   

<
div
style
=
{
{
width

:

'300px'

,

        border

:

'1px solid #ccc'

,

        borderRadius

:

'12px'

,

        overflow

:

'hidden'

,

        boxShadow

:

'0 4px 12px rgba(0, 0, 0, 0.1)'

,

        margin

:

'20px auto'

,

        backgroundColor

:

'#fff'

,

        fontFamily

:

'Arial, sans-serif'

,

     

}

}

   

     

{

imageUrl

&&

(

       

<
img
src
=
{
imageUrl
}
alt
=
{
title
}
style
=
{
{
width

:

'100%'

,

            height

:

'200px'

,

            objectFit

:

'cover'

,

         

}

}

       

/>

     

)

}

     

<

div style

=

{

{

padding

:

'20px'

}

}

       

<

h3 style

=

{

{

margin

:

'0 0 10px 0'

,

color

:

'#333'

}

}

{

title

}

h3

       

<

p style

=

{

{

margin

:

0

,

color

:

'#666'

,

lineHeight

:

'1.5'

}

}

         

{

content

}

       

p

     

div

   

div

 

)

;

}

;

export

default

Card

;

Description:

Step 2: Modify App.jsx to use the Card component twice

Open src/App.jsx, replace the content with the following code (keeps part of the original structure, removes the default counter, and focuses on card display):

src/App.jsx file code:

import

'./App.css'

;

import

Card from

'./components/Card.jsx'

;

function

App

(

)

{

 

return

(

   

<
div
style
=
{
{
display

:

'flex'

,

        flexWrap

:

'wrap'

,

        justifyContent

:

'center'

,

        padding

:

'40px'

,

        backgroundColor

:

'#f0f0f0'

,

        minHeight

:

'100vh'

,

     

}

}

   

     

{

/* First card: About React */

}

     

<
Card
title
=

"What is React?"

        content

=

"React is a JavaScript library for building user interfaces, developed by Facebook. It uses a component-based approach, making code easier to reuse and maintain."

        imageUrl

=

"https://react.dev/images/uwu.png"

     

/>

     

{

/* Second card: About Vite */

}

     

<
Card
title
=

"Advantages of Vite"

        content

=

"Vite is a new generation front-end build tool, with an extremely fast dev server startup, support for Hot Module Replacement (HMR), and is the top choice for modern React projects."

        imageUrl

=

"https://vite.dev/logo-without-border.svg"

     

/>

     

{

/* You can add a third one to try! */

}

   

div

 

)

;

}

export

default

App

;

Description:

Step 3: Run and view the result

  1. Save all files (Ctrl+S).
  2. If the dev server isn't running, execute in the VS Code terminal:
    npm run dev
  3. Open http://localhost:5173 in the browser, and you'll see two (or more) beautiful cards, centered, with images, titles, and content!

Result preview (text description):