{"id":68017,"date":"2024-09-30T17:42:10","date_gmt":"2024-09-30T12:12:10","guid":{"rendered":"https:\/\/www.tothenew.com\/blog\/?p=68017"},"modified":"2024-10-03T14:12:00","modified_gmt":"2024-10-03T08:42:00","slug":"eslint-prettier-configuration-react-nativeairbnb-style","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/eslint-prettier-configuration-react-nativeairbnb-style\/","title":{"rendered":"EsLint &#038; Prettier Configuration React Native(Airbnb Style)"},"content":{"rendered":"<p>This is a step-by-step guide for integrating <strong>ESLint<\/strong> and <strong>Prettier<\/strong> into a <a href=\"https:\/\/www.tothenew.com\/react-native-development-services\"><strong>React Native<\/strong><\/a> project to ensure code quality and consistent formatting. You are essentially setting up <strong>Airbnb&#8217;s style guide<\/strong> with React-specific configurations and integrating it with <strong>Prettier<\/strong> to automatically format the code.<\/p>\n<div class=\"section\">\n<h3>Steps Breakdown<\/h3>\n<h4>1. Install Dependencies<\/h4>\n<p>Remove the previous ESLint configuration, and install the necessary packages.<\/p>\n<h4>Commands:<\/h4>\n<pre><code>yarn remove @react-native-community\/eslint-config eslint<\/code><\/pre>\n<pre><code>yarn add -D eslint eslint-plugin-react-native prettier eslint-config-prettier<\/code><\/pre>\n<\/div>\n<div class=\"section\">\n<h4>2. Initialize ESLint<\/h4>\n<p>Run the following command to initialize ESLint and generate a configuration file:<\/p>\n<pre><code>npx eslint --init<\/code><\/pre>\n<p>Follow the prompts as outlined below:<\/p>\n<ul>\n<li><strong>Q1:<\/strong> Choose &#8220;To check syntax, find problems, and enforce code style.&#8221;<\/li>\n<li><strong>Q2:<\/strong> Choose &#8220;JavaScript modules (import\/export).&#8221;<\/li>\n<li><strong>Q3:<\/strong> Choose &#8220;React.&#8221;<\/li>\n<li><strong>Q4:<\/strong> Select &#8220;No&#8221; for TypeScript support.<\/li>\n<li><strong>Q5:<\/strong> Choose &#8220;Node&#8221; (since React Native runs in a Node environment).<\/li>\n<li><strong>Q6:<\/strong> Choose &#8220;Use a popular style guide.&#8221;<\/li>\n<li><strong>Q7:<\/strong> Choose &#8220;Airbnb.&#8221;<\/li>\n<li><strong>Q8:<\/strong> Choose &#8220;JSON&#8221; for the config file format.<\/li>\n<\/ul>\n<p>Select &#8220;Yes&#8221; when prompted to install the dependencies.<\/p>\n<\/div>\n<div class=\"section\">\n<h4>3. Update <code>.eslintrc.json<\/code><\/h4>\n<p>Replace the generated <code>.eslintrc.json<\/code> file with the following configuration to integrate <strong>Prettier<\/strong> and React Native-specific rules:<\/p>\n<pre><code>{\r\n  \"env\": {\r\n    \"browser\": true,\r\n    \"es2021\": true\r\n  },\r\n  \"extends\": [\r\n    \"plugin:react\/recommended\",\r\n    \"airbnb\",\r\n    \"airbnb\/hooks\",\r\n    \"prettier\"\r\n  ],\r\n  \"plugins\": [\"react\", \"react-native\"],\r\n  \"parserOptions\": {\r\n    \"ecmaVersion\": \"latest\",\r\n    \"sourceType\": \"module\"\r\n  },\r\n  \"rules\": {\r\n    \"react\/function-component-definition\": \"off\",\r\n    \"no-param-reassign\": \"off\",\r\n    \"react\/jsx-filename-extension\": [1, { \"extensions\": [\".js\", \".jsx\"] }],\r\n    \"no-use-before-define\": [\"error\", { \"variables\": false }],\r\n    \"react\/prop-types\": [\"error\", { \"ignore\": [\"navigation\", \"navigation.navigate\"] }],\r\n    \"react-native\/no-inline-styles\": \"error\",\r\n    \"max-lines\": [\"error\", { \"max\": 500 }]\r\n  }\r\n}<\/code><\/pre>\n<\/div>\n<div class=\"section\">\n<h4>4. Create <code>.eslintignore<\/code><\/h4>\n<p>Add a <code>.eslintignore<\/code> file to exclude certain files or directories from ESLint checks.<\/p>\n<h4><code>.eslintignore<\/code>:<\/h4>\n<pre><code>node_modules\/\r\nbuild\/*.js\r\nconfig\/*.js\r\ncoverage\/*.js\r\ncoverage\/*\r\njest\/*.js\r\n__tests__\/*\r\n__tests__\/*.js<\/code><\/pre>\n<\/div>\n<div class=\"section\">\n<h4>5. Create <code>.prettierrc.json<\/code><\/h4>\n<p>Add the following configuration in a <code>.prettierrc.json<\/code> file to set up <strong>Prettier<\/strong> formatting rules:<\/p>\n<pre><code>{\r\n  \"arrowParens\": \"always\",\r\n  \"bracketSpacing\": true,\r\n  \"jsxBracketSameLine\": false,\r\n  \"jsxSingleQuote\": false,\r\n  \"quoteProps\": \"as-needed\",\r\n  \"singleQuote\": true,\r\n  \"semi\": true,\r\n  \"printWidth\": 100,\r\n  \"useTabs\": false,\r\n  \"tabWidth\": 2,\r\n  \"trailingComma\": \"es5\"\r\n}<\/code><\/pre>\n<\/div>\n<div class=\"section\">\n<h4>6. Add Scripts to <code>package.json<\/code><\/h4>\n<p>Add the following scripts to your <code>package.json<\/code> file to run <strong>ESLint<\/strong> and <strong>Prettier<\/strong>:<\/p>\n<h4><code>package.json<\/code>:<\/h4>\n<pre><code>\"scripts\": {\r\n  \"lint\": \"eslint .\",\r\n  \"lintFixAll\": \"eslint 'src\/**\/*.{js,jsx}' --fix\",\r\n  \"prettierFixAll\": \"prettier --write 'src\/**\/*.{js,jsx}'\",\r\n  \"fix:lintPrettier\": \"yarn prettierFixAll &amp;&amp; yarn lintFixAll\"\r\n}<\/code><\/pre>\n<p>This will allow you to lint and fix code formatting issues with a single command.<\/p>\n<\/div>\n<div class=\"section\">\n<h4>7. Usage<\/h4>\n<p>You can now run the following commands to check and fix issues in your project:<\/p>\n<ul>\n<li><strong>Lint all files:<\/strong> <code>yarn lint<\/code><\/li>\n<li><strong>Fix all ESLint issues:<\/strong> <code>yarn lintFixAll<\/code><\/li>\n<li><strong>Fix all Prettier formatting issues:<\/strong> <code>yarn prettierFixAll<\/code><\/li>\n<li><strong>Run both Prettier and ESLint fixes:<\/strong> <code>yarn fix:lintPrettier<\/code><\/li>\n<\/ul>\n<\/div>\n<div class=\"section\">\n<h3>Conclusion<\/h3>\n<p>By following these steps, your React Native project will be set up with <strong>ESLint<\/strong>, <strong>Prettier<\/strong>, and <strong>Airbnb\u2019s style guide<\/strong>, ensuring code quality and formatting consistency.<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>This is a step-by-step guide for integrating ESLint and Prettier into a React Native project to ensure code quality and consistent formatting. You are essentially setting up Airbnb&#8217;s style guide with React-specific configurations and integrating it with Prettier to automatically format the code. Steps Breakdown 1. Install Dependencies Remove the previous ESLint configuration, and install [&hellip;]<\/p>\n","protected":false},"author":1786,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":26},"categories":[5881],"tags":[6803,6808,6804,6805,6456,6801,6807,55,1156,6802,6806,4064,5853],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/68017"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/1786"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=68017"}],"version-history":[{"count":4,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/68017\/revisions"}],"predecessor-version":[{"id":68113,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/68017\/revisions\/68113"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=68017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=68017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=68017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}