A modern news web application built with React and Vite, providing live, real-time world news powered by the GNews API. Designed for rapid development and learning, this project demonstrates best practices in React, state management, API integration, and frontend tooling. The application features categorized news, a responsive layout, and showcases integration with third-party APIs using Axios.
- Live-Demo: https://news-arnob.vercel.app/
- Project Details
- Features
- Project Structure
- Technology Stack
- Installation & Setup
- API Integration
- Usage Instructions
- Key Concepts & Learning Points
- Example Code Snippets
- Conclusion
- Live Demo: News-ReactVite Webpage
- Purpose: Deliver the latest world news in a fast, organized, and visually engaging way.
- API Source: GNews API
- Frontend: Built with React & Vite for blazing fast development and HMR (Hot Module Replacement).
- HTTP Requests: Managed through Axios.
- Live news fetched from GNews API.
- Categorized news navigation via Navbar (e.g., World, Sports, Technology, etc.).
- Detailed news view for each article.
- Responsive design for desktop and mobile.
- Secure API key management using
.env
. - Clean code structure and reusable components.
- Easily extensible for more features or categories.
News--ReactVite/
├── .eslintrc.cjs
├── .gitignore
├── README.md
├── index.html
├── package.json
├── package-lock.json
├── vite.config.js
└── src/
├── components/
│ ├── Navbar.jsx
│ ├── NewsList.jsx
│ ├── NewsItem.jsx
│ └── ...otherComponents
├── App.jsx
├── main.jsx
└── assets/
└── ...images, styles
Note: The
src
folder contains all source code, whilecomponents
hold reusable UI elements.
- React: Core UI library.
- Vite: Fast build tool and dev server.
- Axios: For HTTP requests.
- JavaScript (ES6+)
- CSS / SCSS: For styling.
- GNews API: News data provider.
- Node.js & npm: Dependency and build management.
- ESLint: Linting and code quality.
- Vercel: (optional) for deployment.
-
Install Node.js
Download and install Node.js from nodejs.org. -
Clone the Repository
git clone https://github.com/arnobt78/News--ReactVite.git cd News--ReactVite
-
Install Dependencies
npm install
-
Setup .env File
Create a.env
file in the root, and add your GNews API key:VITE_NEWS_API_KEY=your_gnews_api_key_here
-
Install Axios
If not already installed:npm install axios
-
Run the Application Locally
npm run dev
Open http://localhost:5173/ in your browser.
- GNews API:
- Sign up at gnews.io to get your API key.
- Store the API key in your
.env
file as shown above. - Axios is used to fetch news data from the API endpoints.
- Start the app (
npm run dev
) - Browse categories using the top navigation bar
- Click on articles to view detailed news content
- Stay up to date with the latest world news, refreshed live from the GNews API
- React Functional Components: All UI built from modular, reusable JS functions.
- State Management: Use of React Hooks (useState, useEffect) for local state and effects.
- API Requests with Axios: Fetch and handle data asynchronously.
- Environment Variables: Secure API keys with
.env
and Vite’s environment system. - Component-Based Architecture: Separation of concerns, reusability, and scalability.
- Routing (if implemented): Page navigation and dynamic rendering.
- Responsive Design: Mobile-first, adaptive layout.
Fetching News with Axios:
import axios from 'axios';
const API_KEY = import.meta.env.VITE_NEWS_API_KEY;
const url = `https://gnews.io/api/v4/top-headlines?token=${API_KEY}&lang=en`;
const fetchNews = async () => {
try {
const response = await axios.get(url);
// set state with response.data.articles
} catch (error) {
console.error('Failed to fetch news:', error);
}
};
Sample Navbar Component:
export default function Navbar() {
return (
<nav>
<ul>
<li>World</li>
<li>Technology</li>
<li>Sports</li>
{/* Add more categories */}
</ul>
</nav>
);
}
This project is a practical example for learning full-stack JavaScript with React and Vite, API integration, and modern web development best practices. It is easily extendable for more features, categories, or different APIs.
Contributions, suggestions, and feedback are welcome!
React, Vite, GNews API, Axios, News App, JavaScript, .env, Environment Variables, API Integration, Responsive Design, Components, Frontend, Web Development, Learning Project
Thank you for exploring and using News-ReactVite.
Feel free to fork, star, and contribute!