Vscode jsx 자동완성 - vscode jsx jadong-wanseong

Visual Studio Code 를 사용해 React 코드를 간편하게 완성시키는 방법이 있습니다. className 이나 태그를 입력하면 자동완성 시키는 방법인데 플러그인 설치 없이 설정에 들어가서 간단히 수정만으로 가능합니다.

{
    "window.zoomLevel": 1,
    "editor.wordWrap": "on",
    "workbench.colorTheme": "One Dark Pro",
    "workbench.iconTheme": "material-icon-theme",

    "emmet.includeLanguages": {
        "javascript": "javascriptreact"
    }
}

▼ 1. 우선 VS Code의 하단 설정을 선택합니다.

Vscode jsx 자동완성 - vscode jsx jadong-wanseong

 2. 설정을 텍스모드로 변경합니다. 다음 코드를 삽입합니다.

"emmet.includeLanguages": {
	"javascript": "javascriptreact"
}

이제 부터는 h2.test.ok 이렇게 하고 탭키를 누르면 다음과 같은 코드를 볼 수 있습니다.

<h2 className="test ok">

Greetings, friends! Have you ever been working with JSX in your React code, wishing VS Code would autocomplete it for you like it does HTML? It's a super easy configuration in VS Code!

In case you haven't heard, VS Code supports Emmet snippets by default. Emmet is a plugin for many text editors and IDEs such as VS Code. It helps you write HTML and CSS faster by giving you autocompletion support.

If you create a new .html file and start typing an element, VS Code will autocomplete it for you because it uses Emmet internally. VS Code is also built to understand a lot of common languages such as HTML, CSS, JavaScript, and TypeScript.

By default, VS Code isn't configured to recognize JSX with Emmet.

Vscode jsx 자동완성 - vscode jsx jadong-wanseong

In the image above, there's no autocomplete showing up when I type div in the JSX. If I was in an .html file, then I would see an autocomplete suggestion show up.

How do we fix this? It's actually super simple!

If you type Cmd+Shift+P or Ctrl+Shift+P in VS Code, then it will open up the Command Palette. Perform a search for "Preferences: Open Settings (JSON)". This will open up a settings.json file that represents a configuration for VS Code. We will add the following lines anywhere in this file:

json

Copied! ⭐

"emmet.includeLanguages": {
  "javascript": "javascriptreact",
  "typescript": "typescriptreact"
}

By adding these two lines, Emmet will be enabled in JSX and let you autocomplete element tags in files that are recognized as "javascriptreact" or "typescriptreact" in VS Code. You may have to restart VS Code for your changes to take effect.

The "typescriptreact" option will help Emmet recognize JSX within your .tsx files if you're using React with TypeScript.

Now, when you try to type div in your .js or .jsx file, you should see autocomplete suggestions appear! 🎉

Vscode jsx 자동완성 - vscode jsx jadong-wanseong

You can use your normal Emmet shortcuts as if you were in an .html file. Here's a shortcut that helps you write a <div> element with a class name:

Vscode jsx 자동완성 - vscode jsx jadong-wanseong

After hitting Enter on your keyboard, you should see it autocomplete:

Vscode jsx 자동완성 - vscode jsx jadong-wanseong

Now, I like to go a step further and add another line to our settings.json file we opened up earlier.

json

Copied! ⭐

"editor.autoClosingBrackets": "always"

This line will automatically enclose square brackets and curly brackets. Why is this useful? We can do cool tricks like this:

Vscode jsx 자동완성 - vscode jsx jadong-wanseong

This is a type of Emmet shortcut that is already built into VS Code. After hitting Enter on your keyboard, you should see it autocomplete:

Vscode jsx 자동완성 - vscode jsx jadong-wanseong

I've noticed that curly brackets have a hard time autocompleting in JSX. By telling VS Code to always close brackets as we start typing, it provides a small convenience of not having to type as much every time we create an element.

Conclusion

This entire post was about improving the developer experience regarding typing in JSX. You'll find that you'll be writing JSX code much faster with these tricks! Happy coding!