It is due to scoping in your API call. The value of this
is different within your response
callback in your promise chain. There are two ways to solve this.
You could set a variable referencing this
at the beginning of your apiCall
method.
apiCall(term){
const that = this;
const params = {
part: 'snippet',
key:APP_KEY,
q:term,
type: 'video'
};
axios.get(APP_URL, { params: params}).then(function(response) {
that.setState({
videos:response
});
}).catch(function(error) {
console.error(error);
});
}
or else you can use ES6 arrow functions, which maintain the original scope, like this:
axios.get(APP_URL, { params: params}).then(response => {
this.setState({
videos:response
});
})