Here is a step-by-step guide detailing how you set up this project.
-
Project Structure:
- Created a root
index.htmlfile to serve as the application's entry point. - Created a
srcdirectory for the React source code.
- Created a root
-
Core React Files:
src/Counter.jsx: A simple React component with state (useState) to manage a counter.src/Main.jsx: The main file that renders theCountercomponent into thediv#mainelement inindex.htmlusingReactDOM.createRoot.
-
Dependencies (
package.json):- Added
reactandreact-domas core dependencies. - Added
viteand@vitejs/plugin-reactas development dependencies to build and serve the project.
- Added
-
Scripts (
package.json):- Added a
"dev": "vite"script to start the development server.
- Added a
-
Installation:
- Added
prettieras a development dependency to yourpackage.json.
- Added
-
Configuration (
.prettierrc):- Created a
.prettierrcfile in the project root. - Configured it with your preferences (e.g.,
"useTabs": true).
- Created a
-
Scripts (
package.json):- Added a
"format"script (prettier . --write) to automatically format all files. - Added a
"format:check"script (prettier . --check) to check for formatting issues without modifying files.
- Added a
-
Ignoring Files (
.prettierignore):- Created a
.prettierignorefile in the root. - Added entries like
node_modules,dist, andpackage-lock.jsonto prevent Prettier from formatting files that should not be touched.
- Created a
-
Installation:
- Added
eslint,@eslint/js,eslint-plugin-react, andglobalsas development dependencies.
- Added
-
Configuration (
eslint.config.mjs):- Created a modern
eslint.config.mjs(flat config) file. - Configured it to use recommended rules for JavaScript (
js/recommended) and React (pluginReact.configs.flat.recommended).
- Created a modern
-
ESLint + Prettier Integration:
- Purpose: To prevent conflicts between ESLint's code style rules and Prettier's formatting.
- Installation: Added
eslint-config-prettieras a development dependency. - Configuration: Updated
eslint.config.mjsby importingeslint-config-prettierand adding it as the very last item in the configuration array. This turns off all ESLint rules that would conflict with Prettier.
By following these steps, you created a robust, minimal React project with professional-grade tooling for formatting and linting.