简体   繁体   English

如何防止Visual Studio代码破坏我的jsx代码?

[英]How can I keep visual studio code from trashing my jsx code?

I've been working on a simple crud app using reactjs. 我一直在使用reactjs开发一个简单的Crud应用程序。

Here's a snippet of code 这是一段代码

enter code here render() { return ( Products Manager < AddProduct onAdd = { this.onAdd } /> { this.state.products.map(product => { return ( < ProductItem key = { product.name } {...product } onDelete = { this.onDelete } onEditSubmit = { this.onEditSubmit } /> ); }) } ); enter code here render(){return(Products Manager <AddProduct onAdd = {this.onAdd} /> {this.state.products.map(product => {return(<ProductItem key = {product.name} {...产品} onDelete = {this.onDelete} onEditSubmit = {this.onEditSubmit}));})}));

Looks fine, nothing wrong there. 看起来不错,那里没什么问题。 However if I save this, VS Code does the following: tags separated from their angle brackets, etc. My questions is simple-how do I keep VS Code from trashing my code? 但是,如果我保存此代码,VS Code会执行以下操作:标签与尖括号分开,等等。我的问题很简单-如何防止VS Code破坏代码? enter code here render() { return ( < div className = "App" > < h1 > Products Manager < /h1> < AddProduct onAdd = { this.onAdd } is.state.products.map(product => { return ( < ProductItem key = { product.name } {...product } onDelete = { this.onDelete } onEditSubmit = { this.onEditSubmit } /> ); }) } < /div> ); enter code here render(){return(<div className =“ App”> <h1>产品管理器</ h1> <AddProduct onAdd = {this.onAdd} is.state.products.map(product => {return(< ProductItem键= {product.name} {... product} onDelete = {this.onDelete} onEditSubmit = {this.onEditSubmit} />);}}} </ div>);

You probably have an extension installed that is auto formatting your text. 您可能安装了自动格式化文本的扩展程序。 Prettier is one of the most popular ones. Prettier是最受欢迎的之一。 VSCode to my knowledge does not autoformat your code out of the box. 据我所知,VSCode不会自动将您的代码格式化。

It seems like two issues may be contributing towards your issue. 似乎有两个问题可能导致了您的问题。 Your code is invalid, which might be preventing your formatter from working correctly. 您的代码无效,这可能会导致格式化程序无法正常工作。

First we can clean up your code so that it is valid Javascript. 首先,我们可以清理您的代码,使其成为有效的Javascript。 Two issues exist: 存在两个问题:

  1. You're missing a closing brace for your render function. 您缺少渲染功能的右括号。
  2. Adjacent JSX elements must be assigned keys or wrapped in a React Fragment . 必须为相邻的JSX元素分配键或包装在React Fragment中

Here's what the valid Javascript looks like after fixing these issue: 解决这些问题后,有效的Javascript如下所示:

render() {
  return (
    <React.Fragment>
      Products Manager
      <AddProduct onAdd={this.onAdd} />
      {this.state.products.map(product => {
        return (
          <ProductItem
            key={product.name}
            {...product}
            onDelete={this.onDelete}
            onEditSubmit={this.onEditSubmit}
          />
        );
      })}
    </React.Fragment>
  );
}

VSCode has a built in Javascript Language Service which allows the editor to support Javascript features out-of-the-box. VSCode具有内置的Javascript语言服务 ,该编辑器允许开箱即用地支持Javascript功能。 Learn more here . 在这里了解更多

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM