Last active
March 11, 2024 03:27
-
-
Save rollendxavier/409eb0022d474296557f74aa1b5d342e to your computer and use it in GitHub Desktop.
Price Tracker - News.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { useEffect, useState } from 'react'; | |
import axios from 'axios'; | |
import Marquee from 'react-marquee-slider'; | |
function NewsTicker() { | |
const [news, setNews] = useState([]); | |
const [isLoading, setIsLoading] = useState(true); | |
const [error, setError] = useState(null); | |
useEffect(() => { | |
axios.get(`https://newsapi.org/v2/everything?q=cryptocurrency&apiKey=YOUR_API_KEY`) | |
.then(response => { | |
setNews(response.data.articles); | |
setIsLoading(false); | |
}) | |
.catch(error => { | |
console.error(error); | |
setError('Failed to fetch news'); | |
setIsLoading(false); | |
}); | |
}, []); | |
if (isLoading) { | |
return <div>Loading...</div>; | |
} | |
if (error) { | |
return <div>{error}</div>; | |
} | |
return ( | |
<Marquee velocity={1}> | |
{news.map((article, index) => ( | |
<a key={`marquee-example-news-${index}`} href={article.url} target="_blank" rel="noopener noreferrer" className="news-ticker"> | |
<h1>{article.description}</h1> | |
<p>{new Date(article.publishedAt).toLocaleString()}</p> | |
</a> | |
))} | |
</Marquee> | |
); | |
} | |
export default NewsTicker; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment