-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelayedForm.js
More file actions
43 lines (39 loc) · 1.08 KB
/
DelayedForm.js
File metadata and controls
43 lines (39 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import React, { useState, useEffect } from 'react'
export const DelayedForm = () => {
const [delay, setDelay] = useState("");
useEffect(() => {
(async () => {
console.log("calling endpoint");
// const delayRes = await fetch("https://catfact.ninja/fact");
// console.log("request done");
// setDelay(await delayRes.text());
const req = new XMLHttpRequest();
req.open("GET", "https://catfact.ninja/fact", true);
req.onload = (e) => {
console.log("got it", req.responseText);
setDelay("request done")
}
req.onerror = () => {
console.log("error");
}
req.send();
// fetch("https://catfact.ninja/fact").then(res => {
// console.log("request done");
// res.text().then(text => {
// setDelay(text);
// })
// })
})()
}, [])
console.log("delay", delay, Date.now().toString());
if (delay === "") {
return (
<div data-testid="loading">
Still loading...
</div>
)
}
return (
<div data-testid="loaded">{delay}</div>
)
}