JSX or HTML autocompletion in Visual Studio Code

ReactjsAutocompleteVisual Studio-CodeJsxEmmet

Reactjs Problem Overview


Is there any way to use components or HTML completion in Visual Studio Code? Because typing each letter manually is not good idea when we have classes like Bootstrap etc. For example completion as in Emmet: ul>li*2>a

var React = require('react');

var Header = React.createClass({
    render: function () {
        return (
            
            <nav className="navbar navbar-defaullt">
                <div className="container-fluid">
                    <a href="/" className="navbar-brand">
                        <img width="50" height="50" src="images/logo.png" alt="logo" />
			        </a>
                    <ul className="nav navbar-nav">
                        <li><a href="/">Home</a></li>
                        <li><a href="/#about">About</a></li>
                    </ul>
		        </div>
	        </nav>
            
                );
                }
                });
module.exports  = Header;

Reactjs Solutions


Solution 1 - Reactjs

2019: Straight-to-the-point answer for React

The most straight-forward way to get JSX/HTML autocomplete in your React projects is to add this to your user settings or workspace settings (<project-path>/.vscode/settings.json):

      "emmet.includeLanguages": {
        "javascript": "javascriptreact"
      },
      "emmet.triggerExpansionOnTab": true

You may have to restart VS Code for the change to take effect.

P.S. If you're not doing this mapping for a React.js project, then KehkashanFazal's answer should probably work for you.

Solution 2 - Reactjs

Visual studio code detect .jsx extensions and add emmet support by default ( i'm using VS code 1.8.1)

But if you prefer to use .js extention for all your react files - you can associate JavaScript React mode with .js files in bottom right corner of VS Code window.

How to do this step by step (gif)

enter image description here

Update 2021

For those who just want to copy-paste the code:

"emmet.syntaxProfiles": {
  "javascript": "jsx"
}

If the solution above doesn't work, try this:

"emmet.triggerExpansionOnTab": true,
"emmet.includeLanguages": {
  "javascript": "javascriptreact"
}

Tested with VS Code v1.56.2.

Solution 3 - Reactjs

If someone is still struggling with this issue:

I have discovered that simply setting

"emmet.syntaxProfiles": {
     "javascript": "jsx" 
 },

does not enable HTML completion. But, using:

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

does.

According to emmet docs:

> "emmet.includeLanguages": {}

> Enable emmet abbreviations in languages that are not supported by default. Add a mapping here between the language and emmet supported language.
> Eg: {"vue-html": "html", "javascript": "javascriptreact"}

Solution 4 - Reactjs

Just select the appropriate Language mode at the bottom-right on the screen: set it to JavaScript React.

Solution 5 - Reactjs

2018

I'm using VSCode (ver 1.27.2)

Base on my experienced, even though I'm working with React . The detected language on my VSCode is still vanilla JavaScript. And emmet did not work.

  • One of the ways to make it work again is change the VSCode detected language to JavaScript React. This is for single JS file only.

vscode options

  • To change it once entirely, you need to associate it.

two

Click Configure File Association for .js...

three

And select JSX , which in my case, I already did.

four

  • For Workplace Setting, and last if none of them work for you. Go to Preference of just to ctrl + , (comma) to open it.

Type and search for emmet or Emmet. Then copy the Setting you want to override. In my case:

{
    "emmet.triggerExpansionOnTab": true,
    "emmet.includeLanguages": {
        "javascript": "javascriptreact"
    },
}

Note: I didn't try to use jsx only javascriptreact.

settings

I implemented the second and third step. And I can now do Emmet.

Solution 6 - Reactjs

Simple answer in 2020

First, select the File Association in the bottom-center-right of the window. enter image description here

Second, select Configure File Association for .js from the menu that drops down at the top-center of the window. Change it to JavaScript React. enter image description here

Solution 7 - Reactjs

None of those solutions worked... but the Auto Close Tag extension does! https://marketplace.visualstudio.com/items?itemName=formulahendry.auto-close-tag

Solution 8 - Reactjs

2019 Update


Auto closing tags in .html, .js, and .jsx

Works out of the box. That is, after typing in the closing bracket to the opening tag, the closing tag will be inserted automatically.

Emmet with basic HTML in .jsx files

Works out of the box.

Emmet with basic HTML in .js files:

Add the following setting, required for Emmet abbreviation suggestions, and required for tab expansion to work consistently.

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

Emmet with custom tags (e.g. React Components) in both .js and .jsx files

Add the following setting:

  "emmet.triggerExpansionOnTab": true,

Note that with this setting, Emmet will expand all words as custom tags (not just React component names)

Also note that when using Emmet to expand React components as custom tags, you have to actually choose the component name from the suggestion list and complete that first (or type out the whole name manually and close the suggestion menu with the escape key). After the word expands, you then have to tab again to get Emmet to expand the custom tag.

There's an active feature request to potentially remove this extra step (automatically expand when selecting the suggestion so that it works the same way as expanding standard html tags would).


Troubleshooting

Ensure you have the latest version of VSCode.

Ensure you did not change the following default settings:

"html.autoClosingTags": true,
"javascript.autoClosingTags": true,
"typescript.autoClosingTags": true,

// read the GitHub issue listed above if you're curious why this is required).
"editor.wordBasedSuggestions": true,

// you obviously don't want javascript, javascriptreact included in here if you want Emmet to be available in those files
"emmet.excludeLanguages": [
    "markdown"
],

Solution 9 - Reactjs

I solved the problem simply by following the steps below:

  1. On the left bottom of VSCode click to Javascript

  2. Then on the top, you will see a list, click on "Configure 'Javascript' language based settings"

  3. Add these lines to the file:

    "emmet.triggerExpansionOnTab": true,
     "emmet.includeLanguages": {
       "javascript": "javascriptreact"
    }
    

If you want to more details, you can check this link.

Solution 10 - Reactjs

I went throw all answers and this config worked for me. had to include language as well as add syntaxProfile. without the trigger expansion nothing worked but now I only press Tab button to get the result.

"emmet.includeLanguages": {
    "javascript": "javascriptreact"
},
"emmet.syntaxProfiles": {
    "javascript": "jsx"
},
"emmet.triggerExpansionOnTab": true

Solution 11 - Reactjs

  1. Just go to settings in vscode.
  2. You can see a search bar in setting type emmet.
  3. Scroll down, there is an option include language.
  4. Click on the add item button in include language.
  5. Add item as javascript and value as javascriptreact.

if you get confused, please refer to the picture below: enter image description here

Solution 12 - Reactjs

Easy & Modern way to enable the auto suggestions for JSX for ReactJs is Shown in GIF below.

for Windows => File > Preferences > Settings

for MacOs => Code > Preferences > Settings

then follow the steps shown in GIF.

enter image description here

Solution 13 - Reactjs

you can use The Auto Close Extention In Visual Studio Code . ps. when you install the extension, the autocomplete won't work until you reload VS Code , just Reopen VS Code , or go to auto close tag extension and click Reload.

link of the auto close tag Extension

Solution 14 - Reactjs

Adding an item called javascript with value javascriptreact in the emmet section in settings worked for me.

a screen shot of my settings

Solution 15 - Reactjs

Changing the extension from ".js" to ".jsx" will do the job. But in a case that you want to keep ".js" extension follow below steps.

Go to

Files > Preferences > Settings

In the settings tab you will see two tabs named 'user' and 'workspace' (look close to the bottom of search bar). Both the tabs will show you same settings but user settings will effect for all the projects in VS Code while workspace will effect only for current project. Choose whatever option you want, workspace or user.

And then on the left side menu bar you have to go

Extensions > JSON

Then at the right side you can see several settings. Scroll down a little bit and you will see something like this.

> JSON:Schemas
Associate schemas to JSON files in the current project.
Edit in settings.json

Click on "Edit in settigns.json". It will open a json file. Add below code to the json file.

"emmet.triggerExpansionOnTab": true,
"emmet.includeLanguages": {
    "javascript": "javascriptreact"
}

And save the file. Now go and check if your js file supports for jsx auto-completion. This has been tested with

VS Code versino 1.61.2.

P.S. -: Following part is only for the learners who don't know about json.

Copy and paste the above code to the bottom of settings.json file as shown in the following image. Don't forget to add a comma (pointed by red arrow) and the code should be pasted before the last closing curly bracket (pointed by yellow arrow).

If you choose workspace settings tab then there might be nothing in you settings.json file. If so just paste the code inside the curly brackets. enter image description here

Solution 16 - Reactjs

It took me two steps to get auto-closing tags in JSX.

  1. Follow Kehkashan Fazal's instructions above about setting "emmet.includeLanguages"
  2. Download the Auto Close Tag extension from VSCode (formulahendry.auto-close-tag)

And now you have nice auto-closing JSX tags!

Solution 17 - Reactjs

Follow these two steps only:

  1. On bottom of VSCode where detect language click on that

1st step

  1. Click on "Configure 'Javascript(Babel)' language based settings..." or what ever

2st step

  1. paste this code on it separate first by comma ',' and save it.

"emmet.includeLanguages": { "javascript": "javascriptreact" }, "emmet.triggerExpansionOnTab": true

3rd step

Solution 18 - Reactjs

Autocomplete for React Babel was working fine until yesterday for me.

None of these answers helped so I just restarted my computer. Worked like a charm ;)

Solution 19 - Reactjs

I was working on various projects and I have a big settings file.

I checked settings and found out that this settings was ruining that all.

"emmet.showExpandedAbbreviation": "inMarkupAndStylesheetFilesOnly"

So I commented it. And everyhting works perfectly in React Apps. Thanks

Solution 20 - Reactjs

Only work in JSX files. Let it not work with JS.

"files.associations": {
"*.js": "javascript",
"*.jsx": "javascriptreact",
},
"emmet.triggerExpansionOnTab": true,
"emmet.includeLanguages": {
    "javascriptreact": "javascriptreact"
},

Solution 21 - Reactjs

just name the file you are working on from .js to .jsx and you are fine .

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionbawa gView Question on Stackoverflow
Solution 1 - ReactjsJulian SoroView Answer on Stackoverflow
Solution 2 - ReactjsAndrii.VandakurovView Answer on Stackoverflow
Solution 3 - ReactjsKehkashan FazalView Answer on Stackoverflow
Solution 4 - ReactjsArif DewiView Answer on Stackoverflow
Solution 5 - ReactjsRoCk RoCkView Answer on Stackoverflow
Solution 6 - ReactjsDavis JonesView Answer on Stackoverflow
Solution 7 - ReactjsColeView Answer on Stackoverflow
Solution 8 - ReactjsjabacchettaView Answer on Stackoverflow
Solution 9 - ReactjsEsin ÖNERView Answer on Stackoverflow
Solution 10 - Reactjsuser9083221View Answer on Stackoverflow
Solution 11 - Reactjssurendra edigaView Answer on Stackoverflow
Solution 12 - ReactjsKamal PanaraView Answer on Stackoverflow
Solution 13 - Reactjszakaria kasmiView Answer on Stackoverflow
Solution 14 - ReactjsSaeid RezaeiView Answer on Stackoverflow
Solution 15 - ReactjsJagathView Answer on Stackoverflow
Solution 16 - ReactjsbildungsromanView Answer on Stackoverflow
Solution 17 - ReactjsAmeer HamzaView Answer on Stackoverflow
Solution 18 - ReactjsPixelomoView Answer on Stackoverflow
Solution 19 - ReactjsOleg ShkliaievView Answer on Stackoverflow
Solution 20 - ReactjsFatih ErtuğralView Answer on Stackoverflow
Solution 21 - Reactjsgouder hichamView Answer on Stackoverflow