Building a News App Using React and the New York Times API

In this tutorial, I will show you how I built a news app using React and the New York Times API. This project was a great way for me to learn more about React, how to work with an API, and how to deploy a web app with Vercel.
What We Will Cover:
- Getting news articles from the New York Times API
- Adding pagination and smooth animations
- Deploying the app on Vercel
1. Project Overview
The New York Times News App is a simple web application that lets users see the latest news from the New York Times. The app gets data directly from the New York Times API and shows it in a clean and easy-to-use interface.
Key Features:
- Pagination: Users can easily move through different pages of news articles.
- Smooth Animations: Articles load with a smooth transition, making the app feel more polished.
- Deployment: The app is deployed on Vercel, so it’s available to anyone online. You can check out the GitHub repository to see all the code and learn more.
2. Setting Up the Project
Before we start coding, make sure you have the following on your computer:
- Node.js and npm: You will need these to run the React app and install other tools.
- A New York Times API Key: You can get this by signing up on the NY Times Developer Portal. Installation Steps Let’s set up the project on your local computer.
1. Clone the Repository:
git clone https://github.com/tomasdevs/the-new-york-times-app.git cd the-new-york-times-app
2. Install Dependencies:
We need to install the tools for both the client and server parts of our app.
cd client npm install npm install react-transition-group cd ../server npm install
3. Set Up Environment Variables:
Create a .env file in the server folder and add your New York Times API key:
NYT_API_KEY=your_api_key_here
3. Building the Application
Fetching Data from the New York Times API
In the Articles.js component, I used the Fetch API to get the latest news articles. Here’s how it works:
const response = await fetch(
process.env.NODE_ENV === "production"
? `https://api.nytimes.com/svc/topstories/v2/home.json?api-key=${process.env.REACT_APP_NYT_API_KEY}`
: `${process.env.REACT_APP_API_URL}/api/articles`
);
This code makes sure that when the app is live, it gets data directly from the New York Times API. During development, it gets data from a local server, which makes testing easier.
Adding Pagination and Animations
To handle pagination, I added simple logic that divides the list of articles into pages. I also used react-transition-group to add smooth animations when moving between articles:
<TransitionGroup component="ul" className="articles-list">
{currentArticles.map((article, index) => (
<CSSTransition key={index} timeout={500} classNames="article">
<ArticleItem article={article} />
</CSSTransition>
))}
</TransitionGroup>
4. Challenges and Lessons Learned
Handling CORS Issues
At first, I had some problems with CORS when trying to get data from the New York Times API during local development. I solved this by setting up a simple backend server that acted as a proxy. For the live version, I fetched data directly from the API to keep things simple.
Managing Environment Variables
I also learned how important it is to manage environment variables properly, especially when deploying to platforms like Vercel. This is important to keep sensitive data, like API keys, safe.
5. Conclusion
This project was a great learning experience. It helped me improve my skills in React, API integration, and web deployment. I’m really happy with how the app turned out, and I’m excited to build more complex projects in the future.
If you have any feedback or suggestions, feel free to reach out or leave a comment. You can also check out the code on GitHub and try it yourself.
Try the live demo here!
Thanks for reading!
The above is the detailed content of Building a News App Using React and the New York Times API. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undresser.AI Undress
AI-powered app for creating realistic nude photos
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undress AI Tool
Undress images for free
Clothoff.io
AI clothes remover
AI Hentai Generator
Generate AI Hentai for free.
Hot Article
Hot Tools
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
1378
52
How do I create and publish my own JavaScript libraries?
Mar 18, 2025 pm 03:12 PM
Article discusses creating, publishing, and maintaining JavaScript libraries, focusing on planning, development, testing, documentation, and promotion strategies.
How do I optimize JavaScript code for performance in the browser?
Mar 18, 2025 pm 03:14 PM
The article discusses strategies for optimizing JavaScript performance in browsers, focusing on reducing execution time and minimizing impact on page load speed.
What should I do if I encounter garbled code printing for front-end thermal paper receipts?
Apr 04, 2025 pm 02:42 PM
Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...
How do I debug JavaScript code effectively using browser developer tools?
Mar 18, 2025 pm 03:16 PM
The article discusses effective JavaScript debugging using browser developer tools, focusing on setting breakpoints, using the console, and analyzing performance.
Who gets paid more Python or JavaScript?
Apr 04, 2025 am 12:09 AM
There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.
How do I use source maps to debug minified JavaScript code?
Mar 18, 2025 pm 03:17 PM
The article explains how to use source maps to debug minified JavaScript by mapping it back to the original code. It discusses enabling source maps, setting breakpoints, and using tools like Chrome DevTools and Webpack.
Getting Started With Chart.js: Pie, Doughnut, and Bubble Charts
Mar 15, 2025 am 09:19 AM
This tutorial will explain how to create pie, ring, and bubble charts using Chart.js. Previously, we have learned four chart types of Chart.js: line chart and bar chart (tutorial 2), as well as radar chart and polar region chart (tutorial 3). Create pie and ring charts Pie charts and ring charts are ideal for showing the proportions of a whole that is divided into different parts. For example, a pie chart can be used to show the percentage of male lions, female lions and young lions in a safari, or the percentage of votes that different candidates receive in the election. Pie charts are only suitable for comparing single parameters or datasets. It should be noted that the pie chart cannot draw entities with zero value because the angle of the fan in the pie chart depends on the numerical size of the data point. This means any entity with zero proportion
The difference in console.log output result: Why are the two calls different?
Apr 04, 2025 pm 05:12 PM
In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...


