A React application simulating a "Star Destroyer" game where stars appear randomly in space every few seconds and can be destroyed by clicking them. This project serves as an exercise to practice the powerful useEffect and useRef hooks in React.
- Dynamic Star Generation: Stars are generated at random positions within the viewport every 2.5 seconds.
- Unique Identifiers: Each star has a unique ID for management.
- Star Destruction: Stars can be clicked to destroy them, removing them from the display.
- Focus Management: Individual stars gain focus on their initial render, with a visual effect (e.g., a shadow) to indicate focus.
- Interval Management:
useEffectis used to set up and properly clean up the star generation interval, preventing memory leaks. - Component-Based Design: The application is structured with
SpaceandStarcomponents, each with distinct responsibilities.
- React: Frontend JavaScript library for building user interfaces.
- Vite: Fast build tool and development server for modern web projects.
- HTML/CSS/JavaScript: Core web technologies.
Follow these steps to get the project up and running on your local machine:
-
Clone the repository:
git clone [https://github.com/YOUR_USERNAME/star-destroyer.git](https://github.com/YOUR_USERNAME/star-destroyer.git)
(Replace
YOUR_USERNAMEwith your actual GitHub username) -
Navigate into the project directory:
cd star-destroyer -
Install dependencies:
npm install
-
Start the development server:
npm run dev
The application should open in your browser, typically at
http://localhost:5173/.
This project specifically focuses on demonstrating the practical application of:
useEffectHook:- Setting up and cleaning up
setIntervalfor continuous star generation. - Managing side effects related to the component's lifecycle.
- Setting up and cleaning up
useRefHook:- Directly accessing DOM nodes (individual
Starelements) to programmatically manage focus. - Applying imperative effects (like adding a shadow on focus).
- Directly accessing DOM nodes (individual
- State Management: Managing the collection of stars in the
Spacecomponent's state. - Component Communication: Passing props (star data, destruction callback) from
SpacetoStarcomponents.
Happy coding!