Visual studio code changes format (React-JSX)

后端 未结 12 1024
难免孤独
难免孤独 2020-12-12 13:45

I\'ve the following snippet in my index.js

class App extends Component {
    render() {
        return ( 
Welc
相关标签:
12条回答
  • 2020-12-12 13:49

    Make sure you dont have multiple javascript formatters enabled in your current workspace. (You have to disable the rest of them for your current workspace).

    react-beautify mostly does the magic but fails if you have some other JS formatter/beautifier already installed.

    In my case, I had react-beautify and JS-CSS-HTML Formatter extension installed. I had to disable the JS-CSS-HTML Formatter for my current workspace.

    0 讨论(0)
  • 2020-12-12 13:51

    I just added all the combinations mentioned above.

    1. added this
    "files.associations": {
        "*.js": "javascriptreact"
    }
    
    1. added this also
    "beautify.ignore": ["**/*.js","**/*.jsx"]
    
    1. Deleted additional js formatting
    2. installed prettier
    3. turn ON prettier and formatting
    0 讨论(0)
  • 2020-12-12 13:52

    change vscode preferences settings > user settings below:

    "files.associations": {
            "*.js":"javascriptreact"
        }
    
    0 讨论(0)
  • 2020-12-12 13:52

    Install Prettier (if not installed by default) and try to add this to your user or workplace settings:

    "prettier.jsxBracketSameLine": true

    Do not put linebreak between return and the returned JSX expression.

    Trigger autoformat (Alt+Shift+F) and check if works.

    0 讨论(0)
  • 2020-12-12 13:54

    Alternatively, saving the file with a .jsx extension resolves this in vscode.

    I had the same challenge and I am hoping to discover a better way of handling this issue in vscode.

    I noticed your suggested work-around has to be done each time you open the react file with an extension of .js

    0 讨论(0)
  • 2020-12-12 13:54

    Prettier is an opinionated code formatter. It enforces a consistent style by parsing your code and re-printing it with its own rules that take the maximum line length into account, wrapping code when necessary. include : JavaScript TypeScript Flow JSX JSON CSS SCSS Less HTML Vue Angular GraphQL Markdown YAML https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode

    0 讨论(0)
提交回复
热议问题