import React, { useState } from 'react'; import dateFormat from 'dateformat'; //DOCS: props.uri is the address of a live news-server const NewsFeed = props => { const [articles, setArticles] = useState(null); if (!articles) { fetch(props.uri, { method: 'GET' }) .then(a => { if (!a.ok) { throw `Network error ${a.status}: ${a.statusText} ${a.url}`; } return a.json(); }) .then(a => setArticles(a)) .catch(e => console.error(e)) ; } return (

News Feed

{(articles || []).map((article, index) => { return (

{article.title}

Written by {article.author}, { article.edits > 0 ? Last Updated {dateFormat(articles.updatedAt, 'fullDate')} ({`${article.edits} edit${article.edits > 1 ? 's': ''}`}) : Published {dateFormat(articles.createdAt, 'fullDate')} }

{article.body}

); })}
); }; export default NewsFeed;