fetch quote api and display

This commit is contained in:
Yusuf İpek
2021-01-24 16:48:15 +03:00
parent 3c9c75f668
commit 85d6f7575a
2 changed files with 41 additions and 11 deletions
+40 -10
View File
@@ -1,15 +1,45 @@
import React from "react";
import "./quote-box-styles.scss";
const QuoteBox = () => {
return (
<div className="wrapper">
<div id="quote-box">
<h3 id="text">Deneme</h3>
<p id="author">Deneme</p>
{/* new quote button */}
class QuoteBox extends React.Component {
constructor(props) {
super(props);
this.state = {
quotes: null,
displayQuote: "null",
displayAuthor: null,
};
}
random = () => {
return Math.floor(Math.random() * 1600);
};
async componentDidMount() {
const response = await fetch("https://type.fit/api/quotes");
const json = await response.json();
await this.setState({ quotes: json });
const randomNum = this.random();
await this.setState({
displayQuote: this.state.quotes[randomNum].text,
displayAuthor: this.state.quotes[randomNum].author,
});
}
render() {
if (!this.state.quotes) {
return <div>Loading...</div>;
}
return (
<div className="wrapper">
<div id="quote-box">
<h3 id="text">{this.state.displayQuote}</h3>
<p id="author"> {this.state.displayAuthor} </p>
{/* new quote button */}
</div>
</div>
</div>
);
};
);
}
}
export default QuoteBox;