|
| 1 | +--- |
| 2 | +sidebar_position: 1 |
| 3 | +--- |
| 4 | + |
| 5 | +# Project Structure |
| 6 | + |
| 7 | +Understanding the project structure is crucial for effective development with ABP React. This guide explains the organization of files and directories in your ABP React application. |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +ABP React follows a clean, scalable architecture that separates concerns and promotes maintainability. The project structure is designed to be intuitive for developers familiar with modern React applications while integrating seamlessly with ABP Framework patterns. |
| 12 | + |
| 13 | +## Root Directory Structure |
| 14 | + |
| 15 | +``` |
| 16 | +my-awesome-app/ |
| 17 | +├── .env.sample # Environment variables template |
| 18 | +├── .eslintrc.json # ESLint configuration |
| 19 | +├── .gitignore # Git ignore rules |
| 20 | +├── components.json # shadcn/ui configuration |
| 21 | +├── Dockerfile # Docker configuration |
| 22 | +├── next.config.mjs # Next.js configuration |
| 23 | +├── package.json # Node.js dependencies and scripts |
| 24 | +├── pnpm-lock.yaml # Package lock file |
| 25 | +├── postcss.config.mjs # PostCSS configuration |
| 26 | +├── prettier.config.js # Prettier configuration |
| 27 | +├── README.md # Project documentation |
| 28 | +├── tsconfig.json # TypeScript configuration |
| 29 | +├── public/ # Static assets |
| 30 | +├── src/ # Source code |
| 31 | +└── .template.config/ # Template configuration |
| 32 | +``` |
| 33 | + |
| 34 | +## Source Code Structure (`src/`) |
| 35 | + |
| 36 | +The `src` directory contains all the application source code, organized by feature and responsibility: |
| 37 | + |
| 38 | +``` |
| 39 | +src/ |
| 40 | +├── app/ # Next.js App Router |
| 41 | +│ ├── api/ # API routes |
| 42 | +│ ├── auth/ # Authentication pages |
| 43 | +│ ├── admin/ # Admin panel pages |
| 44 | +│ ├── session/ # Session management |
| 45 | +│ ├── globals.css # Global styles |
| 46 | +│ ├── layout.tsx # Root layout component |
| 47 | +│ └── page.tsx # Home page |
| 48 | +├── components/ # Reusable UI components |
| 49 | +│ ├── ui/ # shadcn/ui components |
| 50 | +│ ├── sections/ # Page sections |
| 51 | +│ ├── navbar/ # Navigation components |
| 52 | +│ ├── menu/ # Menu components |
| 53 | +│ ├── user/ # User-related components |
| 54 | +│ ├── role/ # Role management components |
| 55 | +│ ├── tenant/ # Tenant components |
| 56 | +│ ├── permission/ # Permission components |
| 57 | +│ ├── profile/ # Profile components |
| 58 | +│ ├── settings/ # Settings components |
| 59 | +│ └── analytics/ # Analytics components |
| 60 | +├── hooks/ # Custom React hooks |
| 61 | +├── lib/ # Utility functions and configurations |
| 62 | +├── client/ # API client and services |
| 63 | +├── layout/ # Layout components |
| 64 | +├── config.ts # Application configuration |
| 65 | +├── middleware.ts # Next.js middleware |
| 66 | +├── sessionOptions.ts # Session configuration |
| 67 | +└── useSession.ts # Session hook |
| 68 | +``` |
| 69 | + |
| 70 | +## Key Directories Explained |
| 71 | + |
| 72 | +### `app/` - Next.js App Router |
| 73 | + |
| 74 | +This directory contains the Next.js 13+ App Router structure: |
| 75 | + |
| 76 | +- **`api/`**: Server-side API routes for handling backend communication |
| 77 | +- **`auth/`**: Authentication-related pages (login, register, forgot password) |
| 78 | +- **`admin/`**: Administrative interface pages |
| 79 | +- **`session/`**: Session management pages |
| 80 | +- **`layout.tsx`**: Root layout component that wraps all pages |
| 81 | +- **`page.tsx`**: The main homepage component |
| 82 | + |
| 83 | +### `components/` - UI Components |
| 84 | + |
| 85 | +Organized by feature and responsibility: |
| 86 | + |
| 87 | +- **`ui/`**: Base UI components from shadcn/ui (Button, Input, Dialog, etc.) |
| 88 | +- **`sections/`**: Larger page sections and layouts |
| 89 | +- **`navbar/`**: Navigation bar components |
| 90 | +- **`menu/`**: Menu and navigation components |
| 91 | +- **`user/`**: User management components (UserList, UserForm, etc.) |
| 92 | +- **`role/`**: Role management components |
| 93 | +- **`tenant/`**: Multi-tenancy components |
| 94 | +- **`permission/`**: Permission management components |
| 95 | +- **`profile/`**: User profile components |
| 96 | +- **`settings/`**: Application settings components |
| 97 | +- **`analytics/`**: Analytics and reporting components |
| 98 | + |
| 99 | +### `hooks/` - Custom React Hooks |
| 100 | + |
| 101 | +Contains custom hooks for: |
| 102 | +- Data fetching and caching |
| 103 | +- Form handling |
| 104 | +- Authentication state |
| 105 | +- Permission checking |
| 106 | +- Multi-tenancy operations |
| 107 | + |
| 108 | +### `lib/` - Utilities and Configurations |
| 109 | + |
| 110 | +Contains utility functions and configurations: |
| 111 | +- API client configuration |
| 112 | +- Validation schemas |
| 113 | +- Helper functions |
| 114 | +- Constants and enums |
| 115 | +- Type definitions |
| 116 | + |
| 117 | +### `client/` - API Client |
| 118 | + |
| 119 | +Contains the generated API client and services: |
| 120 | +- OpenAPI generated clients |
| 121 | +- Custom service wrappers |
| 122 | +- Request/response interceptors |
| 123 | +- Error handling utilities |
| 124 | + |
| 125 | +## Configuration Files |
| 126 | + |
| 127 | +### `next.config.mjs` |
| 128 | + |
| 129 | +Next.js configuration including: |
| 130 | +- Build optimization settings |
| 131 | +- Environment variables |
| 132 | +- Webpack customizations |
| 133 | +- Routing configuration |
| 134 | + |
| 135 | +### `tsconfig.json` |
| 136 | + |
| 137 | +TypeScript configuration with: |
| 138 | +- Path mappings for clean imports |
| 139 | +- Strict type checking rules |
| 140 | +- Module resolution settings |
| 141 | +- Compilation targets |
| 142 | + |
| 143 | +### `components.json` |
| 144 | + |
| 145 | +shadcn/ui configuration specifying: |
| 146 | +- Component library settings |
| 147 | +- Theme configuration |
| 148 | +- CSS variables |
| 149 | +- Utility classes |
| 150 | + |
| 151 | +### `package.json` |
| 152 | + |
| 153 | +Node.js project configuration with: |
| 154 | +- Dependencies and dev dependencies |
| 155 | +- Build and development scripts |
| 156 | +- Project metadata |
| 157 | + |
| 158 | +## File Naming Conventions |
| 159 | + |
| 160 | +### Components |
| 161 | +- **PascalCase**: `UserProfile.tsx`, `NavigationMenu.tsx` |
| 162 | +- **Directory-based**: Components with multiple files go in directories |
| 163 | +- **Index files**: Use `index.tsx` for main component exports |
| 164 | + |
| 165 | +### Hooks |
| 166 | +- **camelCase with 'use' prefix**: `useAuth.ts`, `usePermissions.ts` |
| 167 | + |
| 168 | +### Utilities |
| 169 | +- **camelCase**: `apiClient.ts`, `validation.ts` |
| 170 | + |
| 171 | +### Pages (App Router) |
| 172 | +- **kebab-case**: `user-profile/page.tsx`, `admin/users/page.tsx` |
| 173 | +- **Special files**: `layout.tsx`, `loading.tsx`, `error.tsx` |
| 174 | + |
| 175 | +## Import Organization |
| 176 | + |
| 177 | +Imports should be organized in the following order: |
| 178 | + |
| 179 | +1. **React and Next.js imports** |
| 180 | +2. **Third-party libraries** |
| 181 | +3. **Internal utilities and configurations** |
| 182 | +4. **Components (UI, then feature-specific)** |
| 183 | +5. **Types and interfaces** |
| 184 | +6. **Relative imports** |
| 185 | + |
| 186 | +Example: |
| 187 | +```typescript |
| 188 | +// React and Next.js |
| 189 | +import React from 'react'; |
| 190 | +import { useRouter } from 'next/navigation'; |
| 191 | + |
| 192 | +// Third-party libraries |
| 193 | +import { useForm } from 'react-hook-form'; |
| 194 | +import { zodResolver } from '@hookform/resolvers/zod'; |
| 195 | + |
| 196 | +// Internal utilities |
| 197 | +import { apiClient } from '@/lib/api-client'; |
| 198 | +import { userSchema } from '@/lib/schemas'; |
| 199 | + |
| 200 | +// Components |
| 201 | +import { Button } from '@/components/ui/button'; |
| 202 | +import { UserForm } from '@/components/user/user-form'; |
| 203 | + |
| 204 | +// Types |
| 205 | +import type { User } from '@/types/user'; |
| 206 | + |
| 207 | +// Relative imports |
| 208 | +import './user-profile.css'; |
| 209 | +``` |
| 210 | + |
| 211 | +## Environment Configuration |
| 212 | + |
| 213 | +### `.env.sample` |
| 214 | + |
| 215 | +Template for environment variables: |
| 216 | +```env |
| 217 | +# Application |
| 218 | +NEXTAUTH_URL=http://localhost:3000 |
| 219 | +NEXTAUTH_SECRET=your-secret-key |
| 220 | +
|
| 221 | +# API Configuration |
| 222 | +NEXT_PUBLIC_API_URL=https://your-api-url.com |
| 223 | +
|
| 224 | +# Authentication |
| 225 | +NEXT_PUBLIC_AUTH_PROVIDERS=credentials,google,github |
| 226 | +
|
| 227 | +# Feature Flags |
| 228 | +NEXT_PUBLIC_ENABLE_ANALYTICS=true |
| 229 | +NEXT_PUBLIC_ENABLE_MULTI_TENANCY=true |
| 230 | +``` |
| 231 | + |
| 232 | +### `.env.local` |
| 233 | + |
| 234 | +Local development environment variables (not committed to git): |
| 235 | +```env |
| 236 | +NEXTAUTH_SECRET=your-development-secret |
| 237 | +NEXT_PUBLIC_API_URL=http://localhost:44300 |
| 238 | +``` |
| 239 | + |
| 240 | +## Build and Deployment Structure |
| 241 | + |
| 242 | +### Production Build |
| 243 | + |
| 244 | +``` |
| 245 | +.next/ # Next.js build output |
| 246 | +├── static/ # Static assets |
| 247 | +├── server/ # Server-side code |
| 248 | +└── cache/ # Build cache |
| 249 | +``` |
| 250 | + |
| 251 | +### Docker |
| 252 | + |
| 253 | +The project includes Docker configuration for containerized deployment: |
| 254 | + |
| 255 | +```dockerfile |
| 256 | +# Multi-stage build for production |
| 257 | +FROM node:18-alpine AS builder |
| 258 | +# ... build steps |
| 259 | + |
| 260 | +FROM node:18-alpine AS runner |
| 261 | +# ... runtime configuration |
| 262 | +``` |
| 263 | + |
| 264 | +## Best Practices |
| 265 | + |
| 266 | +### Directory Organization |
| 267 | +- **Feature-based**: Group related components, hooks, and utilities together |
| 268 | +- **Consistent naming**: Use consistent naming conventions across the project |
| 269 | +- **Clear separation**: Separate concerns between UI, business logic, and data access |
| 270 | + |
| 271 | +### Component Structure |
| 272 | +- **Single responsibility**: Each component should have a single, well-defined purpose |
| 273 | +- **Reusability**: Design components to be reusable across different parts of the application |
| 274 | +- **Composition**: Use composition over inheritance for complex components |
| 275 | + |
| 276 | +### Import Management |
| 277 | +- **Absolute imports**: Use path mappings for cleaner imports |
| 278 | +- **Barrel exports**: Use index files to simplify imports |
| 279 | +- **Lazy loading**: Implement code splitting for large components |
| 280 | + |
| 281 | +## Next Steps |
| 282 | + |
| 283 | +Now that you understand the project structure, explore these related topics: |
| 284 | + |
| 285 | +- **[Configuration Guide](/docs/fundamentals/configuration)** - Learn about configuration options |
| 286 | +- **[Authentication Setup](/docs/fundamentals/authentication)** - Set up authentication |
| 287 | +- **[Component Development](/docs/components/ui-components)** - Build custom components |
| 288 | +- **[API Integration](/docs/fundamentals/api-integration)** - Connect to your ABP backend |
| 289 | + |
| 290 | +--- |
| 291 | + |
| 292 | +Understanding the project structure is the foundation for successful development with ABP React. This organization promotes maintainability, scalability, and developer productivity. |
0 commit comments