A React application demonstrating the Model-View-Controller (MVC) architectural pattern. This project showcases how to structure a React application using MVC principles, custom hooks, and component-based architecture.
- List of clickable cars
- Detailed view of selected car
- Click counter for each car
- Responsive design
- Unit tests for components and hooks
- JSON Server as mock backend
src/
├── assets/ # Static assets like images
├── components/
│ ├── CarList/ # Car list component
│ ├── CarView/ # Car view component
│ └── Header/ # Header component
├── hooks/ # Custom hooks
│ └── useCarModel.ts # Car data management hook
├── models/ # Data models
│ └── car.ts # Car type definitions
├── services/ # API services
│ └── carService.ts # Car data operations
└── App.tsx # Main application component
- Node.js (v14 or higher)
- npm or yarn
- Git
- Clone the repository:
git clone https://github.com/yourusername/mvc-architecture-pattern.git
cd mvc-architecture-pattern
- Install dependencies:
npm install
# or
yarn install
- Create a
.env
file in the root directory:
VITE_API_URL=http://localhost:3001
- Start the JSON Server (mock backend):
npx json-server --watch db.json --port 3001
- In a new terminal, start the development server:
npm run dev
# or
yarn dev
The application will be available at http://localhost:5173
To run the test suite:
npm test
# or
yarn test
This project follows the MVC pattern adapted for React:
- Model: Implemented through the
useCarModel
hook and car service - View: React components (
CarList
,CarView
, etc.) - Controller: Logic within hooks and components handling user interactions
- CarList: Displays available cars and handles car selection
- CarView: Shows detailed information about the selected car
- useCarModel: Custom hook managing car data and interactions
The mock API (JSON Server) provides the following endpoints:
GET /cars
- Fetch all carsGET /cars/:id
- Fetch a specific carPATCH /cars/:id
- Update car click count
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- React Team for the amazing framework
- JSON Server for providing an easy way to create a mock backend
- All contributors who help improve this project