<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: KhoaDD</title>
    <description>The latest articles on DEV Community by KhoaDD (@dangkhoado43).</description>
    <link>https://dev.to/dangkhoado43</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1373873%2F013720f7-44bc-43f0-a58d-051276950096.png</url>
      <title>DEV Community: KhoaDD</title>
      <link>https://dev.to/dangkhoado43</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dangkhoado43"/>
    <language>en</language>
    <item>
      <title>Clean Code Principles &amp; Code Conventions for React + TypeScript</title>
      <dc:creator>KhoaDD</dc:creator>
      <pubDate>Sun, 11 May 2025 15:53:39 +0000</pubDate>
      <link>https://dev.to/dangkhoado43/clean-code-principles-code-conventions-for-react-typescript-3n7d</link>
      <guid>https://dev.to/dangkhoado43/clean-code-principles-code-conventions-for-react-typescript-3n7d</guid>
      <description>&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;When working in a team of developers, it's essential to follow a consistent coding style. Clean Code principles help in maintaining a codebase that's readable, maintainable, and avoids the dreaded 💩 smells. Adopting Clean Code Principles not only improves the quality of your code but also reflects the professionalism of the development team.&lt;/p&gt;

&lt;p&gt;In projects using React and TypeScript, establishing code conventions is crucial as both technologies allow for various ways to write code. This post will guide you through setting up clean code principles and code conventions for TypeScript + React.&lt;/p&gt;

&lt;h2&gt;
  
  
  Clean Code Principles for TypeScript + React
&lt;/h2&gt;

&lt;p&gt;Here are some key principles to follow:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. Meaningful Naming&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Use clear and descriptive names for variables, functions, components, etc.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ❌ Bad
const d = (x) =&amp;gt; x.map((a) =&amp;gt; a.value);

// ✅ Good
const getValues = (items: Item[]) =&amp;gt; items.map((item) =&amp;gt; item.value);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;2. Single Responsibility Principle (SRP)&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each component should have only one responsibility.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ❌ Bad: Mixing UI rendering with API logic
// ✅ Good: Separate UI and logic into custom hooks or service layers
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;3. Avoid Magic Numbers &amp;amp; Strings&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// ❌
if (status === 2) {}

// ✅
const STATUS_APPROVED = 2;
if (status === STATUS_APPROVED) {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;4. Keep Components Small and Focused&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Avoid creating components larger than 200 lines. Break them into smaller subcomponents when needed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;5. Use TypeScript Effectively&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Always define explicit types for props, state, and function return types.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Code Convention: ESLint + Prettier Setup
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Install Dependencies
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -D eslint prettier eslint-plugin-react eslint-plugin-react-hooks @typescript-eslint/eslint-plugin @typescript-eslint/parser eslint-config-prettier eslint-plugin-prettier
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;.eslintrc.js&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  parser: '@typescript-eslint/parser',
  plugins: ['react', '@typescript-eslint', 'prettier'],
  extends: [
    'plugin:react/recommended',
    'plugin:@typescript-eslint/recommended',
    'plugin:prettier/recommended'
  ],
  parserOptions: {
    ecmaVersion: 2020,
    sourceType: 'module',
    ecmaFeatures: { jsx: true }
  },
  rules: {
    'prettier/prettier': 'error',
    '@typescript-eslint/explicit-function-return-type': 'warn',
    'react/react-in-jsx-scope': 'off', // for Next.js / React 17+
  },
  settings: {
    react: { version: 'detect' }
  }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;prettier.config.js&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;module.exports = {
  semi: true,
  singleQuote: true,
  printWidth: 100,
  trailingComma: 'es5',
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add Scripts to &lt;code&gt;package.json&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"scripts": {
  "lint": "eslint . --ext .ts,.tsx",
  "lint:fix": "eslint . --ext .ts,.tsx --fix",
  "format": "prettier --write ."
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Add Pre-commit Hooks with Husky + Lint-Staged
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Install Husky and Lint-Staged
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm install -D husky lint-staged
npx husky install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Configure lint-staged in &lt;code&gt;package.json&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"lint-staged": {
  "src/**/*.{ts,tsx}": [
    "npm run lint:fix",
    "npm run format"
  ]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Add Pre-commit Hook
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx husky add .husky/pre-commit "npx lint-staged"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Extra Resouces
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Clean Code (Book) by Robert C. Martin&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Refactoring UI by Adam Wathan &amp;amp; Steve Schoger&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/airbnb/javascript" rel="noopener noreferrer"&gt;Airbnb JavaScript Style Guide&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://typescript-eslint.io/rules/" rel="noopener noreferrer"&gt;TypeScript ESLint Rules&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://prettier.io/docs/en/index.html" rel="noopener noreferrer"&gt;Prettier Documentation&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://eslint.org/docs/latest/" rel="noopener noreferrer"&gt;ESLint Documentation&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://typicode.github.io/husky/#/" rel="noopener noreferrer"&gt;Husky Git Hooks&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/okonet/lint-staged" rel="noopener noreferrer"&gt;lint-staged&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>cleancode</category>
      <category>react</category>
      <category>typescript</category>
    </item>
  </channel>
</rss>
