Yeah this has to do with how you are updating your state. You’re causing more renders than are necessary.
Still messing around but if you put a console.log(quote) in your render method after you declare quote you will see it’s being rendered three times
There’s some issues like calling this.randomIndex function inside your setState which calls setState itself, but your not calling this.randomIdex() your just returning the function definition.
componentDidMount() {
fetch(API)
.then((res) => res.json())
.then((res) => {
this.setState({
quotes: res.quotes
});
});
}
// getRandomIndex=()=>{
// const {quotes} = this.state;
// if(quotes.length> 0){
// const index = Math.floor(Math.random() * quotes.length);
// this.setState({
// index
// })
// }
// }
render() {
const { quotes, index } = this.state;
const quote = quotes[index];
if (quote) {
var tweetURl = `https://twitter.com/intent/tweet?text=${quote.quote}-${quote.author}`;
}
This gets the quote to go through.
If you uncomment the getRandomIndex it should work I just did that for troubleshooting purposes. You had that in the setState of the componentDidMount which seems unnecessary and was causing and extra render too.
Let me know if this makes sense or if you have any other questions.
Seems the URL encoding is happening somewhere without us manually having to do it to.