Skip to content

Commit

Permalink
Challenge and Code: Stories Component
Browse files Browse the repository at this point in the history
  • Loading branch information
15Dkatz committed Oct 25, 2022
1 parent 8061365 commit 05700a3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
3 changes: 3 additions & 0 deletions react-hooks/home/src/App.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useState } from 'react';
import Joke from './Joke';
import Stories from './Stories';

function App() {
const [userQuery, setUserQuery] = useState('');
Expand Down Expand Up @@ -33,6 +34,8 @@ function App() {
</div>
<hr />
<Joke />
<hr />
<Stories />
</div>
);
}
Expand Down
31 changes: 31 additions & 0 deletions react-hooks/home/src/Stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React, { useState, useEffect } from 'react';

function Stories() {
const [stories, setStories] = useState([]);

useEffect(() => {
fetch('https://news-proxy-230704.appspot.com/topstories')
.then(response => response.json())
.then(json => setStories(json));
}, []);

return (
<div className='Stories'>
<h3>Stories</h3>
{
stories.map(story => {
const { id, by, time, title, url } = story;

return (
<div key={id}>
<a href={url}>{title}</a>
<div>{by} - {new Date(time * 1000).toLocaleString()}</div>
</div>
);
})
}
</div>
)
}

export default Stories;

0 comments on commit 05700a3

Please sign in to comment.