Switched to using useEffect
This commit is contained in:
@@ -1,23 +1,27 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
import dateFormat from 'dateformat';
|
import dateFormat from 'dateformat';
|
||||||
|
|
||||||
//DOCS: props.uri is the address of a live news-server
|
//DOCS: props.uri is the address of a live news-server
|
||||||
const NewsFeed = props => {
|
const NewsFeed = props => {
|
||||||
const [articles, setArticles] = useState(null);
|
const [articles, setArticles] = useState(null);
|
||||||
|
|
||||||
if (!articles) {
|
useEffect(async () => {
|
||||||
fetch(props.uri, {
|
const result = await fetch(props.uri, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'Access-Control-Allow-Origin': '*'
|
'Access-Control-Allow-Origin': '*'
|
||||||
},
|
},
|
||||||
})
|
});
|
||||||
.then(blob => blob.json())
|
|
||||||
.then(a => setArticles(a))
|
if (!result.ok) {
|
||||||
.catch(e => console.error(e))
|
const err = `${result.status}: ${await result.text()}`;
|
||||||
;
|
console.log(err);
|
||||||
}
|
alert(err);
|
||||||
|
} else {
|
||||||
|
setArticles(await result.json());
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
|
|||||||
Reference in New Issue
Block a user