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.
- β‘οΈ 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
anduseForm
- π API Service Layer - Structured API service pattern for data fetching
- Node.js 18+
- npm, yarn, or pnpm
-
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
-
Install dependencies:
npm install # or yarn # or pnpm install
-
Start the development server:
npm run dev # or yarn dev # or pnpm dev
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
npm run dev
- Start the development servernpm run build
- Build for productionnpm run preview
- Preview the production build locallynpm run lint
- Run ESLint to check for issuesnpm test
- Run all testsnpm run test:watch
- Run tests in watch modenpm run test:coverage
- Generate test coverage reportnpm run test:components
- Run component tests onlynpm run test:pages
- Run page tests onlynpm run test:hooks
- Run hook tests onlynpm run test:routes
- Run routing tests only
npm install package-name
# or
yarn add package-name
# or
pnpm add package-name
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
},
},
})
The template uses React Router v7 for routing with a clean, maintainable structure:
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
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>
)
}
- 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>
)
}
- Create an index.js file for easy imports:
// src/pages/YourNewPage/index.js
export { default } from './YourNewPage'
- 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 },
]
}
]
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>
)
}
The project uses Vitest and React Testing Library for testing. Test files are organized to mirror the source code structure.
# Run all tests
npm test
# Run tests in watch mode
npm run test:watch
# Generate coverage report
npm run test:coverage
// 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()
})
})
This project is open source and available under the MIT License.