0
votes

I did fetch data from NYTimes API and console log them. My initial state is {searchResponse: null} Then set state the response this.setState=({searchResponse:response.data}); and pass it to another component named listview_component. In that component, I handle the null value of prop.

but the response from the API did not push in searchResponse. and show the error : TypeError: Cannot set property 'setState' of undefined.

How to solve this?

please check out this code https://github.com/shojibMahabub/news-scroller/tree/develop

2

2 Answers

0
votes

this.setState = {user_input} is wrong. setState is a function it should be:

this.setState({user_input});

On top you forgot to bind your function in your constructor:

 this.handle_user_input = this.handle_user_input.bind(this);

I could not find any mention of setState=({searchResponse:response.data})in your code, but it probably fails because of the same issue.

0
votes

If needed to access .this convert the function to fat arrow function.

do_search = (keyword) => {
        axios
            .get(
                url, // takes the variable url
                {
                    params: {
                        api_key: api_key,
                        q: keyword
                    }
                }
            )
            .then(response => {
                console.log(response);
                this.setState=({searchResponse:response.data});
            })
            .catch(function(error) {
                console.log(error);
            });
    }