0

I'm having problem with setState function. This is my code:

class App extends Component {
  constructor(props){
    super(props);
    this.state={
      movies: [],
      selectedMovie: null
    }
    this.movieSearch("kingsman")

  }
  movieSearch = (term) =>{
    const request = axios.get(`${ROOT_URL}${API_KEY}&query=${term}`)
      .then(function (response) {
        let parsed = JSON.parse(response.request.response);
        console.log(parsed);
        this.setState({movies:parsed.results})
      })
      .catch(function (error) {
        console.log(error);
      });
  }
.............

For some reason I get "TypeError: Cannot read property 'setState' of undefined" in my console. Could someone explain me what mistake I'm doing here?

Kreha
  • 195
  • 2
  • 10
  • As suggested in the link above, use [arrow functions](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Functions/Arrow_functions) and it will work. Change `.then(function (response) {` into `.then((response) => {` and it will work. Same for the `.catch` – Chris May 07 '17 at 11:06
  • Read this answer http://stackoverflow.com/a/4886696/1915893 – Aluan Haddad May 07 '17 at 11:06
  • @AluanHaddad, just mark it as duplicate instead :) – Chris May 07 '17 at 11:07
  • But not of that one because that one is itself a duplicate, and the one I linked is asking the question conversely – Aluan Haddad May 07 '17 at 11:08

0 Answers0