-
Notifications
You must be signed in to change notification settings - Fork 9
/
kitties.jsx
44 lines (39 loc) · 1.11 KB
/
kitties.jsx
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
44
import React, {Suspense} from 'react';
import {unstable_createResource as createResource} from 'react-cache';
import './kitties.css';
const catApiResource = createResource(async() => {
const response = await fetch('https://api.thecatapi.com/v1/images/search');
const [result] = await response.json();
const img = new Image();
const src = await new Promise(resolve => {
img.onload = () => resolve(result.url);
img.src = result.url;
});
return src;
});
const KittyImage = ({ID}) => {
const url = catApiResource.read(ID);
return <img src={url} alt="randomKitty" className="kitty-image" />;
};
export default class Kitties extends React.Component {
state = {
catID: 0
}
_handleButtonClick = () => {
this.setState({
catID: Math.random()
});
}
render = () => {
const {catID} = this.state;
return (
<div className="kitties">
<button onClick={this._handleButtonClick}>Show Another Kitty</button>
<Suspense fallback={<div>Loading the kitty...</div>}
maxDuration={3000}>
<KittyImage ID={catID} />
</Suspense>
</div>
);
}
}