
Use the NewsData.io React client library to integrate NewsData.io API into your React application without having to make HTTP requests directly. Focus on your data, not the boilerplate.
Package
NewsData.io React PackageSOURCE CODE
NewsData.io React Client RepositoryInstallation
Install the official NewsData.io React client using pip. We recommend using a virtual environment for your projects.
Terminal
npm install newsdataapi
Usage
Import Client
Bring in the core NewsDataApiClient class.
Initialize
Authenticate with your secret API key.
Request
Fetch news with flexible parameters.
Terminal
import useNewsDataApiClient from "newsdataapi";
# API key authorization, Initialize the client with your API key
const { latest } = useNewsDataApiClient(YOUR_API_KEY)
# You can pass empty or with request parameters (ex. country: "us")
const data = await latest({ q: "ronaldo", country: "us" });You can pass any supported request parameters directly into await latest("parameter 1", "parameter 2");
Pagination
Handling large datasets is seamless. The React client allows you to loop through results automatically by tracking the nextPage token.
Terminal
import useNewsDataApiClient from "newsdataapi";
# API key authorization, Initialize the client with your API key
const { latest } = useNewsDataApiClient(YOUR_API_KEY);
let allNews = [];
let nextPage;
while (true) {
# Include page only if we already have a nextPage value
const params = { q: "ronaldo", country: "us" };
if (nextPage) params.page = nextPage;
const data = await latest(params);
if (data?.results?.length) {
allNews.push(...data.results);
}
# Exit loop if no nextPage
if (!data.nextPage) break;
# Set nextPage for next iteration
nextPage = data.nextPage;
}
# Use the collected news
console.log(allNews);Was this page helpful?