How do I export axios get request from separate .js file so I can use it in my main.js by importing i.e:
import { getNewQuote } from './api'
class App extends Component {
constructor () {
super();
this.state = {
quote: []
}
this.handleNewQuote = this.handleNewQuote.bind(this);
}
componentDidMount() {
getNewQuote();
}
handleNewQuote() {
getNewQuote();
}
...
My api.js looks like this:
export function getNewQuote(){
axios.defaults.baseURL = 'https://andruxnet-random-famous-quotes.p.mashape.com';
axios.defaults.headers.common['X-Mashape-Key'] = "MY-API-KEY";
axios.get('/?cat=famous&count=1')
.then(res => {
this.setState({ quote: res.data })
})
.catch(error => {
console.log(error)
})
}
With this setup I'm getting an error in console:
TypeError: Cannot read property 'setState' of undefined at api.js:8 at
I assume the issue is with:
axios getNewQuote export or getNewQuote call in componentDidMount
Any help?