Organizing the Frontend project&#s components folder

WBOY
Release: 2024-08-08 15:30:49
Original
615 people have browsed it

Organizing the Frontend project

Structuring thecomponentsfolder of a Frontend project is important. Because it makes developing and maintaining projects easier. Especially when dealing with large or complex components, organizing folders helps keep your code organized and easier to find and understand.

Here is thecomponentsfolder structure in various formats. Commonly used in projects developed usingNext.jsandTypeScript:

1.Atomic Design Structure

Atomic Designis a design concept that divides components according to their complexity and functionality. It is divided into 5 levels: Atoms, Molecules, Organisms, Templates, and Pages.

src/ └── components/ ├── atoms/ # Small, reusable elements (e.g., buttons, inputs) │ ├── Button.tsx │ ├── Input.tsx │ ├── Icon.tsx │ └── ... # Additional atoms │ ├── molecules/ # Combinations of atoms (e.g., form groups) │ ├── FormInput.tsx │ ├── NavLink.tsx │ └── ... # Additional molecules │ ├── organisms/ # Complex UI components (e.g., headers, cards) │ ├── Header.tsx │ ├── Card.tsx │ ├── Footer.tsx │ └── ... # Additional organisms │ ├── templates/ # Page templates (layouts with placeholders) │ ├── MainLayout.tsx │ ├── DashboardLayout.tsx │ └── ... # Additional templates │ └── pages/ # Page-specific components (used directly in pages) ├── HomePage.tsx ├── AboutPage.tsx └── ... # Additional page components
Copy after login

example:

Atoms: Button.tsx

import React from 'react'; interface ButtonProps { label: string; onClick: () => void; type?: 'button' | 'submit' | 'reset'; disabled?: boolean; } const Button: React.FC = ({ label, onClick, type = 'button', disabled = false }) => (  ); export default Button;
Copy after login

Molecules: FormInput.tsx

import React from 'react'; import Input from '../atoms/Input'; import Label from '../atoms/Label'; interface FormInputProps { label: string; value: string; onChange: (value: string) => void; } const FormInput: React.FC = ({ label, value, onChange }) => ( 
); export default FormInput;
Copy after login

Organisms: Header.tsx

import React from 'react'; import NavLink from '../molecules/NavLink'; import Logo from '../atoms/Logo'; const Header: React.FC = () => ( 
); export default Header;
Copy after login

2.Feature-Based Structure

Structures that separate components by feature or module are popular in feature-rich projects. Helps manage and expand features efficiently

src/ └── components/ ├── authentication/ # Components related to authentication │ ├── Login.tsx │ ├── Signup.tsx │ └── PasswordReset.tsx │ ├── dashboard/ # Components specific to the dashboard │ ├── DashboardHeader.tsx │ ├── DashboardSidebar.tsx │ └── StatsCard.tsx │ ├── userProfile/ # Components for user profile │ ├── ProfileHeader.tsx │ ├── EditProfileForm.tsx │ └── Avatar.tsx │ ├── shared/ # Shared or common components across features │ ├── Button.tsx │ ├── Modal.tsx │ └── ... # Additional shared components │ └── layout/ # Layout components ├── Header.tsx ├── Footer.tsx └── Sidebar.tsx
Copy after login

example:

Authentication: Login.tsx

import React, { useState } from 'react'; import Button from '../shared/Button'; import FormInput from '../shared/FormInput'; const Login: React.FC = () => { const [email, setEmail] = useState(''); const [password, setPassword] = useState(''); const handleLogin = () => { // Logic for login }; return ( 

Login

); }; export default Login;
Copy after login

Dashboard: StatsCard.tsx

import React from 'react'; interface StatsCardProps { title: string; value: number; icon: React.ReactNode; } const StatsCard: React.FC = ({ title, value, icon }) => ( 
{icon}

{title}

{value}

); export default StatsCard;
Copy after login

3.Domain-Driven Structure

This structure focuses on organizing components according to the domain or bounded context of your project. This makes this structure suitable for complex systems that require clear domain separation

src/ └── components/ ├── domain/ │ ├── product/ # Components related to product domain │ │ ├── ProductCard.tsx │ │ ├── ProductList.tsx │ │ └── ProductDetail.tsx │ │ │ ├── cart/ # Components for cart domain │ │ ├── CartItem.tsx │ │ ├── CartSummary.tsx │ │ └── CartIcon.tsx │ │ │ ├── user/ # Components for user domain │ │ ├── UserAvatar.tsx │ │ ├── UserProfile.tsx │ │ └── UserSettings.tsx │ │ │ └── ... # Additional domain-specific components │ ├── ui/ # UI elements (atoms, molecules, etc.) │ ├── atoms/ │ ├── molecules/ │ └── organisms/ │ └── layout/ # Layout components ├── Header.tsx ├── Footer.tsx └── Sidebar.tsx
Copy after login

example:

Product: ProductCard.tsx

import React from 'react'; interface ProductCardProps { name: string; price: number; imageUrl: string; onAddToCart: () => void; } const ProductCard: React.FC = ({ name, price, imageUrl, onAddToCart }) => ( 
{name}

{name}

${price.toFixed(2)}

); export default ProductCard;
Copy after login

Cart: CartSummary.tsx

import React from 'react'; interface CartSummaryProps { totalItems: number; totalPrice: number; } const CartSummary: React.FC = ({ totalItems, totalPrice }) => ( 

Cart Summary

Total Items: {totalItems}

Total Price: ${totalPrice.toFixed(2)}

); export default CartSummary;
Copy after login

4.Component-Driven Development (CDD) with Storybook

This structure is designed to support development.Component-Driven Development (CDD)usingStorybookwhich allows you to develop and test components in a format separate from the main application

src/ └── components/ ├── Button/ │ ├── Button.tsx # Component implementation │ ├── Button.stories.tsx # Storybook stories │ ├── Button.test.tsx # Unit tests │ └── Button.module.css # Component-specific styles │ ├── Input/ │ ├── Input.tsx │ ├── Input.stories.tsx │ ├── Input.test.tsx │ └── Input.module.css │ ├── Modal/ │ ├── Modal.tsx │ ├── Modal.stories.tsx │ ├── Modal.test.tsx │ └── Modal.module.css │ └── ... # Additional component folders
Copy after login

example:

Button: Button.tsx

import React from 'react'; import styles from './Button.module.css'; interface ButtonProps { label: string; onClick: () => void; variant?: 'primary' | 'secondary'; } const Button: React.FC = ({ label, onClick, variant = 'primary' }) => (  ); export default Button;
Copy after login

Button: Button.stories.tsx (Storybook)

import React from 'react'; import { Meta, Story } from '@storybook/react'; import Button, { ButtonProps } from './Button'; export default { title: 'Components/Button', component: Button, } as Meta; const Template: Story = (args) => 
Copy after login

5.Shared Component Library

In projects where multiple teams work together Creating a structure that uses common components is important. This structure emphasizes separating components that can be reused throughout the project

src/ └── components/ ├── shared/ # Shared components across the application │ ├── Button/ │ │ ├── Button.tsx │ │ └── Button.module.css │ │ │ ├── Modal/ │ │ ├── Modal.tsx │ │ └── Modal.module.css │ │ │ └── ... # Additional shared components │ ├── featureSpecific/ # Feature-specific components │ ├── UserProfile/ │ │ ├── ProfileHeader.tsx │ │ ├── ProfileDetails.tsx │ │ └── Avatar.tsx │ │ │ ├── ProductList/ │ │ ├── ProductCard.tsx │ │ └── ProductFilter.tsx │ │ │ └── ... # Additional feature-specific components │ └── layout/ # Layout components ├── Header.tsx ├── Footer.tsx └── Sidebar.tsx
Copy after login

ตัวอย่าง:

Shared: Modal.tsx

import React from 'react'; import styles from './Modal.module.css'; interface ModalProps { title: string; isOpen: boolean; onClose: () => void; } const Modal: React.FC = ({ title, isOpen, onClose, children }) => { if (!isOpen) return null; return ( 

{title}

{children}
); }; export default Modal;
Copy after login

Feature-Specific: ProfileHeader.tsx

import React from 'react'; interface ProfileHeaderProps { name: string; bio: string; avatarUrl: string; } const ProfileHeader: React.FC = ({ name, bio, avatarUrl }) => ( 
{name}

{name}

{bio}

); export default ProfileHeader;
Copy after login

Factors to Consider When Structuring Components

  1. Reusability:ควรแยก component ที่สามารถใช้ซ้ำได้ออกจาก component ที่เฉพาะเจาะจงกับฟีเจอร์
  2. Maintainability:การจัดโครงสร้างที่ดีช่วยให้การดูแลรักษาและการอัพเดตโปรเจคเป็นไปอย่างราบรื่น
  3. Scalability:โครงสร้างที่ดีจะช่วยให้การขยายฟีเจอร์และการเพิ่ม component ใหม่ ๆ เป็นเรื่องง่าย
  4. Performance:ใช้เทคนิคที่เหมาะสมในการโหลดและใช้ component เพื่อให้แน่ใจว่าแอปพลิเคชันของคุณมีประสิทธิภาพ

Best Practices for Component Structure

  • Single Responsibility Principle:แต่ละ component ควรทำหน้าที่เดียวและทำได้ดี
  • Component Naming:ตั้งชื่อ component ให้สื่อความหมายและชัดเจน
  • Component Composition:ใช้ composition แทน inheritance เมื่อสร้าง component ใหม่
  • Use Prop Types or TypeScript:กำหนด prop types หรือใช้ TypeScript interfaces เพื่อเพิ่มความปลอดภัยในการใช้งาน
  • Write Tests:เขียน unit tests สำหรับ component ทุกตัวเพื่อตรวจสอบการทำงาน

ด้วยข้อมูลและแนวทางเหล่านี้ หวังว่าคุณจะสามารถจัดโครงสร้างในโฟลเดอร์componentsของโปรเจคได้อย่างมีประสิทธิภาพและเหมาะสมกับความต้องการของโปรเจคของคุณ!

The above is the detailed content of Organizing the Frontend project&#s components folder. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!