最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Custom react code snippets not working in vscode in javascriptreact.json file - Stack Overflow

programmeradmin3浏览0评论

I want to add a few custom React.js code snippets in VSCode. I realize there are two files in VSCode for this. javascript.json and javascriptreact.json. javascript.json works just fine but when I move the snippets to javascriptreact.json, VSCode doesn't recognize them.

It'll be great if I could isolate these snippets in javacriptreact.json file because I also write server side JavaScript.

I'm guessing that VSCode doesn't recognize the app as a React app. Is there anyway to fix this?

I want to add a few custom React.js code snippets in VSCode. I realize there are two files in VSCode for this. javascript.json and javascriptreact.json. javascript.json works just fine but when I move the snippets to javascriptreact.json, VSCode doesn't recognize them.

It'll be great if I could isolate these snippets in javacriptreact.json file because I also write server side JavaScript.

I'm guessing that VSCode doesn't recognize the app as a React app. Is there anyway to fix this?

Share Improve this question asked Mar 28, 2019 at 23:50 Amiratak88Amiratak88 1,5491 gold badge16 silver badges20 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

save file with.jsx extension instead of .js. i.e. code snippets you are seeing in javascript.json is because you are editing a .js file

I very recently was getting this issue in Ubuntu 18.04, after including the below code for my "react-classnames" snippet in vscode file (which was not giving me the expected result when I type inside a React file in vscode)

~/.config/Code/User/snippets/javascriptreact.json

  "react-classnames": {
    "prefix": "clmi",
    "body": [
      "className={classes.}"
    ],
    "description": "react-classname"
  },

So, in a React ponent .js file when I type the snippet ('clmi' in this case) - before the React Component File's return() statement, I get the expected behaviour. But if I type the snippet ('clmi') inside the return() statement, I dont get anything. i.e. the snippet does not give me the code pletion option inside the return () statment.

The source was that VSCode was not recognizing this file to be a "javascriptreact" language type.

Solution was as below

In a React Component's .js file, try the tokens inspector via "F1 > Inspect TM Scopes" and position the cursor at those insert position. That will tell you the detected, embedded language. Depending on your grammar it might be a different language

And bleow is what I am getting.

So as you can see, this not JavaScript nor JavaScript React but the jsx-attr language. So now I had to target that type with vscode snippet

So now, I put the whole snippet in the jsx-attr type target file ~/.config/Code/User/snippets/jsx-attr.json

And now the custom snippets worked as expected.

When I added the react snippet to jsx.json file the configuration worked. It did not work when added to js.json.

This worked: ~/.config/Code/User/snippets/jsx.json

This did not work: ~/.config/Code/User/snippets/js.json

发布评论

评论列表(0)

  1. 暂无评论