import React, { useState, useEffect, useRef } from 'react'; import dateFormat from 'dateformat'; const NewsFeed = props => { const [articles, setArticles] = useState([]); const aborter = useRef(new AbortController()); //BUGFIX: double-renders = double fetches + react update after unmount useEffect(() => { //this... um... fetch(`${process.env.NEWS_URI}/news`, { signal: aborter.current.signal //oh dear }) .then(blob => blob.json()) .then(json => setArticles(json)) .catch(e => null) //swallow errors ; return () => aborter.current.abort(); //This is an ugly, ugly solution, but it's the only one that works }, []); return (
Written by {article.author}, { article.edits > 0 ? Last Updated {dateFormat(article.updatedAt, 'fullDate')} ({`${article.edits} edit${article.edits > 1 ? 's': ''}`}) : Published {dateFormat(article.createdAt, 'fullDate')} }