Skip to content
This repository was archived by the owner on Jun 2, 2025. It is now read-only.

🌟 A well-structured, scalable React starter template powered by Vite. Features fast HMR, optimized builds, clean architecture, and built-in support for ESLint, Prettier, Git hooks, and optional Tailwind CSS. Ideal for fast prototyping or building modern production-ready applications.

License

Notifications You must be signed in to change notification settings

NhanPhamThanh-IT/Vite-React-Template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

13 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Vite React Template with React Router

A modern, feature-rich template for multi-page React applications built with Vite and React Router. This template provides a comprehensive starter kit with all the essential components and configurations for building professional React applications.

Features

  • ⚑️ Fast Development & Building - Powered by Vite for lightning-fast HMR and optimized builds
  • βš›οΈ React 19 - Utilizing the latest React features and improvements
  • 🧭 React Router v7 - Modern routing with the latest React Router DOM
  • πŸ”„ Hot Module Replacement - Edit code and see changes instantly without losing state
  • πŸ“¦ Code Splitting - Automatic code splitting with lazy-loaded components for better performance
  • 🧩 Component Architecture - Well-organized component structure with clear separation of concerns
  • πŸ“„ Page Structure - Clean organization of page components with proper routing
  • πŸ—οΈ Layout System - Flexible, reusable layouts with nested routes
  • 🎨 CSS Modules - Locally scoped CSS for components to avoid style conflicts
  • πŸŒ— Theme System - Built-in light/dark mode with system preference detection
  • 🧰 Path Aliases - Import using @components, @pages, etc. for cleaner imports
  • πŸ“ Modern ESLint - Latest linting configuration with ES module support
  • πŸ› οΈ Utility Functions - Ready-to-use utility functions for common operations
  • πŸ§ͺ Testing Infrastructure - Comprehensive testing setup with Vitest and Testing Library
  • πŸš€ Custom Hooks - Reusable hooks like useLocalStorage and useForm
  • πŸ“Š API Service Layer - Structured API service pattern for data fetching

Getting Started

Prerequisites

  • Node.js 18+
  • npm, yarn, or pnpm

Installation

  1. Clone this repository:

    # Using template repository feature
    # Click "Use this template" on the GitHub repository page
    
    # Or clone directly
    git clone https://github.com/yourusername/vite-react-template.git my-project
    cd my-project
  2. Install dependencies:

    npm install
    # or
    yarn
    # or
    pnpm install
  3. Start the development server:

    npm run dev
    # or
    yarn dev
    # or
    pnpm dev

Project Structure

vite-react-template/
β”œβ”€β”€ public/                # Static public assets
β”‚   └── favicon.svg
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ api/               # API clients and request helpers
β”‚   β”œβ”€β”€ assets/            # Images, fonts and other assets
β”‚   β”œβ”€β”€ components/        # Reusable UI components
β”‚   β”‚   β”œβ”€β”€ Button/
β”‚   β”‚   β”œβ”€β”€ ErrorBoundary/
β”‚   β”‚   β”œβ”€β”€ Navigation/
β”‚   β”‚   β”œβ”€β”€ Spinner/
β”‚   β”‚   β”œβ”€β”€ ThemeIndicator/
β”‚   β”‚   β”œβ”€β”€ ThemeToggle/
β”‚   β”‚   └── index.js
β”‚   β”œβ”€β”€ constants/         # Application constants
β”‚   β”‚   β”œβ”€β”€ api.js
β”‚   β”‚   β”œβ”€β”€ app.js
β”‚   β”‚   └── index.js
β”‚   β”œβ”€β”€ context/           # React Context providers
β”‚   β”‚   β”œβ”€β”€ ThemeContext/
β”‚   β”‚   β”œβ”€β”€ ThemeContext.jsx
β”‚   β”‚   β”œβ”€β”€ ThemeProvider.jsx
β”‚   β”‚   β”œβ”€β”€ useTheme.js
β”‚   β”‚   └── index.js
β”‚   β”œβ”€β”€ hooks/             # Custom React hooks
β”‚   β”‚   β”œβ”€β”€ useForm.js
β”‚   β”‚   β”œβ”€β”€ useLocalStorage.js
β”‚   β”‚   └── index.js
β”‚   β”œβ”€β”€ layouts/           # Layout components
β”‚   β”‚   β”œβ”€β”€ MainLayout/
β”‚   β”‚   └── index.js
β”‚   β”œβ”€β”€ pages/             # Page components
β”‚   β”‚   β”œβ”€β”€ AboutPage/
β”‚   β”‚   β”œβ”€β”€ ContactPage/
β”‚   β”‚   β”œβ”€β”€ HomePage/
β”‚   β”‚   β”œβ”€β”€ NotFoundPage/
β”‚   β”‚   β”œβ”€β”€ ThemeDemoPage/
β”‚   β”‚   └── index.js
β”‚   β”œβ”€β”€ services/          # API services
β”‚   β”‚   β”œβ”€β”€ userService.js
β”‚   β”‚   └── index.js
β”‚   β”œβ”€β”€ store/             # State management (if needed)
β”‚   β”œβ”€β”€ styles/            # Global styles
β”‚   β”‚   └── global.css
β”‚   β”œβ”€β”€ utils/             # Utility functions
β”‚   β”‚   β”œβ”€β”€ dateUtils.js
β”‚   β”‚   └── index.js
β”‚   β”œβ”€β”€ App.jsx            # Main App component
β”‚   β”œβ”€β”€ main.jsx           # Entry point
β”‚   └── routes.js          # Routes configuration
β”œβ”€β”€ tests/
β”‚   β”œβ”€β”€ setup/             # Test setup files
β”‚   β”‚   β”œβ”€β”€ setupTests.js
β”‚   β”‚   β”œβ”€β”€ testHelpers.js
β”‚   β”‚   └── __mocks__/
β”‚   β”œβ”€β”€ unit/              # Unit tests
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”œβ”€β”€ context/
β”‚   β”‚   β”œβ”€β”€ hooks/
β”‚   β”‚   β”œβ”€β”€ layouts/
β”‚   β”‚   β”œβ”€β”€ pages/
β”‚   β”‚   β”œβ”€β”€ routes/
β”‚   β”‚   └── utils/
β”‚   β”œβ”€β”€ README.md          # Testing documentation
β”‚   └── vitest.config.js   # Vitest configuration
β”œβ”€β”€ eslint.config.js       # ESLint configuration
β”œβ”€β”€ index.html             # HTML template
β”œβ”€β”€ jsconfig.json          # JavaScript configuration
β”œβ”€β”€ package.json           # Project dependencies and scripts
└── vite.config.js         # Vite configuration

Available Scripts

  • npm run dev - Start the development server
  • npm run build - Build for production
  • npm run preview - Preview the production build locally
  • npm run lint - Run ESLint to check for issues
  • npm test - Run all tests
  • npm run test:watch - Run tests in watch mode
  • npm run test:coverage - Generate test coverage report
  • npm run test:components - Run component tests only
  • npm run test:pages - Run page tests only
  • npm run test:hooks - Run hook tests only
  • npm run test:routes - Run routing tests only

Customization

Adding Dependencies

npm install package-name
# or
yarn add package-name
# or 
pnpm add package-name

Modifying Vite Config

Edit the vite.config.js file to customize your build process. The template comes with pre-configured path aliases for cleaner imports.

// vite.config.js
export default defineConfig({
  // ...
  resolve: {
    alias: {
      '@': path.resolve(__dirname, 'src'),
      '@components': path.resolve(__dirname, 'src/components'),
      '@pages': path.resolve(__dirname, 'src/pages'),
      // ...more aliases
    },
  },
})

Routing System

The template uses React Router v7 for routing with a clean, maintainable structure:

Routes Configuration

Routes are defined in src/routes.js for better organization:

// src/routes.js
import { lazy } from 'react'
import { MainLayout } from '@layouts'

// Lazy load pages for better performance
const HomePage = lazy(() => import('@pages/HomePage'))
const AboutPage = lazy(() => import('@pages/AboutPage'))
const ContactPage = lazy(() => import('@pages/ContactPage'))
const NotFoundPage = lazy(() => import('@pages/NotFoundPage'))
const ThemeDemoPage = lazy(() => import('@pages/ThemeDemoPage'))

const routes = [
  {
    path: '/',
    element: MainLayout,
    children: [
      { index: true, element: HomePage },
      { path: 'about', element: AboutPage },
      { path: 'contact', element: ContactPage },
      { path: 'theme-demo', element: ThemeDemoPage },
      { path: '*', element: NotFoundPage }
    ]
  }
]

export default routes

Layout System

The template uses a layout system with React Router's Outlet component:

// src/layouts/MainLayout/MainLayout.jsx
import { Outlet } from 'react-router-dom'
import { Navigation } from '@components'

export default function MainLayout() {
  return (
    <div className={styles.layout}>
      <header>
        <Navigation />
      </header>
      
      <main>
        <Outlet /> {/* Page content renders here */}
      </main>
      
      <footer>{/* Footer content */}</footer>
    </div>
  )
}

Adding New Pages

  1. Create a new page component in src/pages/YourNewPage/:
// src/pages/YourNewPage/YourNewPage.jsx
import styles from './YourNewPage.module.css'

export default function YourNewPage() {
  return (
    <div className={styles.page}>
      <h1>Your New Page</h1>
      <p>This is a new page in the application.</p>
    </div>
  )
}
  1. Create an index.js file for easy imports:
// src/pages/YourNewPage/index.js
export { default } from './YourNewPage'
  1. Add the route to src/routes.js:
// In routes.js
const YourNewPage = lazy(() => import('@pages/YourNewPage'))

const routes = [
  {
    path: '/',
    element: MainLayout,
    children: [
      // Existing routes...
      { path: 'your-new-path', element: YourNewPage },
    ]
  }
]

Theme System

The template includes a complete theme system with light, dark, and system preference modes:

// Using the theme
import { useTheme } from '@context'

function YourComponent() {
  const { currentTheme, toggleTheme } = useTheme()
  
  return (
    <div>
      <p>Current theme: {currentTheme}</p>
      <button onClick={toggleTheme}>Toggle Theme</button>
    </div>
  )
}

Testing

The project uses Vitest and React Testing Library for testing. Test files are organized to mirror the source code structure.

Running Tests

# Run all tests
npm test

# Run tests in watch mode
npm run test:watch 

# Generate coverage report
npm run test:coverage

Writing Tests

// Example component test
import { render, screen } from '@testing-library/react'
import { Button } from '@components'

describe('Button', () => {
  test('renders correctly', () => {
    render(<Button>Click me</Button>)
    expect(screen.getByText('Click me')).toBeInTheDocument()
  })
})

License

This project is open source and available under the MIT License.

About

🌟 A well-structured, scalable React starter template powered by Vite. Features fast HMR, optimized builds, clean architecture, and built-in support for ESLint, Prettier, Git hooks, and optional Tailwind CSS. Ideal for fast prototyping or building modern production-ready applications.

Topics

Resources

License

Stars

Watchers

Forks