So I’m trying to complete the random quote machine challenge, and I’m using react to do so. I keep getting a result of ‘undefined’ when I try to save the data to the state. I am working from an api at this link https://quotes.rest/qod.json *I did have to change it from http to https, otherwise the request wouldn’t go through. This is the code that makes the call. I used axios because it makes the process simpler and I don’t want to deal with polyfill’s right now.
import React, { Component } from 'react';
import axios from 'axios'
const url = "https://quotes.rest/qod.json";
class NewQuote extends Component {
constructor(props) {
super(props);
this.state = {
quote: ''
}
this.handleClick = this.handleClick.bind(this)
}
handleClick(){
axios.get(url)
.then(results => console.log(results.data.contents.quotes.quote))
}
render(){
return(
<div>
<button onClick={this.handleClick}>New Quote</button>
<p></p>
</div>
)
}
}
export default NewQuote;
and this is the JSON that it returns
{
success: {
total: 1
},
contents: {
quotes: [
{
quote: "Keep a positive mind. Remember, a failed attempt doesn't make you a failure—giving up does.",
length: "98",
author: "Lorii Myers",
tags: [
"giving-up",
"inspire",
"positive-attitude",
"self-improvement"
],
category: "inspire",
date: "2018-12-31",
permalink: "https://theysaidso.com/quote/bUu2mjT_nWdPCQyDq7IsugeF/lorii-myers-keep-a-positive-mind-remember-a-failed-attempt-doesnt-make-you-a-fai",
title: "Inspiring Quote of the day",
background: "https://theysaidso.com/img/bgs/man_on_the_mountain.jpg",
id: "bUu2mjT_nWdPCQyDq7IsugeF"
}
],
copyright: "2017-19 theysaidso.com"
}
}
I must be doing something wrong to access the quote string, but I can’t figure out what. Any help would be greatly appreciated! I’m new to react and having a hard time figuring it out so constructive criticism is welcome.
P.S, If you’re wondering about the empty
in the render component, That is where I plan to output the quote initially, then I will be sending the data to a separate react class to handle the output.