This is a simple React app demonstrating the Model-View-ViewModel (MVVM) pattern using React hooks.
This project is built using Create React App and TypeScript. The main purpose of the project is to showcase how to implement the MVVM pattern in a React application using hooks, without relying on third-party libraries such as MobX, Redux, or the Context API.
In this example, the MVVM pattern is achieved by separating the model, view, and ViewModel logic. This helps achieve a clean separation of concerns and makes the code more maintainable and testable.
The project includes a simple counter component that demonstrates the MVVM pattern.
In this example, the MVVM pattern is implemented by separating the model, view, and ViewModel logic as follows:
- The Model is defined in
CounterModel.ts. This file represents the application's data and state. - The
useCounterViewModel.tsfile contains the custom hookuseCounterViewModel, which connects the model and view by implementing the ViewModel layer. This hook returns an instance of the ViewModel. - The
Counter.tsxfile represents the view component that consumes the ViewModel instance. TheuseCounterViewModelhook is used to access the ViewModel instance, and the view component interacts with the ViewModel to update and display the state. - The
App.tsxfile serves as the main React component for the application.
By organizing the code this way, the MVVM pattern is achieved, and components can easily consume the ViewModel instance without having to manage the state and logic themselves. This approach helps achieve a clean separation of concerns and improves the code's maintainability and testability.
- MVVM pattern with hooks
- Clean separation of concerns
- Improved maintainability and testability
Ensure you have Node.js and yarn installed on your local machine.
- Clone the repository:
git clone https://github.com/or2ooo/react-todo-app-mvvm-hooks.git- Install the dependencies:
yarn installTo run the app in development mode, execute the following command:
yarn startOpen http://localhost:3000 to view the app in the browser. The page will reload if you make edits, and you will see any lint errors in the console.
To build the app for production, run:
yarn buildThe build will be minified and optimized for best performance, and it will be output to the build folder.
Contributions are welcome. Please follow these steps to contribute:
- Fork the repository
- Create a new branch (
git checkout -b feature/MyNewFeature) - Commit your changes (
git commit -m 'Add some feature') - Push to the branch (
git push origin feature/MyNewFeature) - Create a new Pull Request
This project is licensed under the MIT License.