简体   繁体   English

如何为 React Native 更改更漂亮的格式

[英]how to change prettier format for react native

my code formatting prettier didn't works well for react native, i don't understand where to config it but it works well with flutter我的代码格式 prettier 不适用于 React Native,我不知道在哪里配置它,但它适用于 flutter

from this code从这个代码

      import { View, Text } from 'react-native'
    
    import React from 'react'
    
    export default function App() {
      return (
        <View>
    
    <Text>Apps</Text>   
</View>
      )
    }

it's formatted to this它被格式化为这个

import {
  View,
  Text
} from 'react-native'

import React from 'react'

export default function App() {
  return ( <
    View >

    <
    Text > Apps < /Text>


    <
    /View>
  )
}

There is a shortcut for every idea to format your code, for instance for Vscode Ctrl + Shift + I每个想法都有一个快捷方式来格式化代码,例如 Vscode Ctrl + Shift + I

AND use Prettier to format your code in all files并使用 Prettier 格式化所有文件中的代码

1- First you can install the prettier code extension in your idea(Webstorm or vs code) https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode "for the vscode" 1- 首先你可以在你的想法中安装更漂亮的代码扩展(Webstorm 或 vs code) https://marketplace.visualstudio.com/items?itemName=esbenp.prettier-vscode “for the vscode”

2- Then install its package to your dependencies npm install --save-dev --save-exact prettier https://prettier.io/docs/en/install.html 2- 然后将其 package 安装到您的依赖npm install --save-dev --save-exact prettier prettier https://prettier.io/docs/en/install.html

3- create .prettierrc file and paste this code into it 3- 创建.prettierrc文件并将此代码粘贴到其中

{
    "arrowParens": "always",
    "bracketSameLine": false,
    "bracketSpacing": true,
    "embeddedLanguageFormatting": "auto",
    "htmlWhitespaceSensitivity": "css",
    "insertPragma": false,
    "jsxSingleQuote": false,
    "printWidth": 80,
    "proseWrap": "preserve",
    "quoteProps": "as-needed",
    "requirePragma": false,
    "semi": true,
    "singleQuote": false,
    "tabWidth": 2,
    "trailingComma": "es5",
    "useTabs": false,
    "vueIndentScriptAndStyle": false
}

4-Add 4-添加

"scripts": {
    ...
    "prettify": "prettier --write"
}

script to your package.json file脚本到您的package.json文件

Run npm run prettify whenever you think your code is ugly运行npm run prettify每当你认为你的代码很丑时运行 prettify

well i found the answer right away lol好吧,我马上就找到了答案,哈哈

  1. press f1 choose Format Document With按 f1 选择格式化文档

在此处输入图像描述

  1. and select prettier for default和 select 默认更漂亮

在此处输入图像描述

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

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