Hello,
I’m trying to build this projects with React
Before I build it with jQuery
And now I have met with some issue
bellow is a part of my code
in the componentDidMount() I am getting API
all-saw here I’m generating random number with randomeQuote() for selecting random quote from API
Now I’m trying to remove randomeQuote() from componentDidMount to generateNewQuote().
generateNewQuote() is event handling, when I press button it must to generate random integer number, I want to send it to the componentDidMount and such way generate new quote
Question
How I can pass random number from generateNewQuote() to componentDidMount?
import React from "react";
import api from "../api/api";
const NewQuote = props => {};
class Quote extends React.Component {
constructor(props){
super(props);
this.state = {
quotes: "",
author: ""
};
}
componentDidMount() {
const quote = api.get("/quotes.json");
const randomQuote = () => {
return Math.floor(Math.random() * 102);
};
quote.then(res => {
this.setState({
quotes: res.data.quotes[randomQuote()].quote,
author: res.data.quotes[randomQuote()].author
});
});
}
generateNewQuote = event => {
event.preventDefault();
// **HOW TO SEND randomQuote int value from here to componentDidMount?**
const randomQuote = () => {
return Math.floor(Math.random() * 102);
};
console.log(randomQuote());
//return randomQuote;
};
render() {
return (
<div>
<h3>"{this.state.quotes}"</h3>
<h5>"{this.state.author}"</h5>
<div>
<button onClick={this.twitClick} className="btn btn-primary twit">
Twit
</button>
<button
onClick={this.generateNewQuote}
className="btn btn-primary new-quote"
>
New Quote
</button>
</div>
</div>
);
}
}
export default Quote;