2017-04-13 3 views
0

내 영화 상태에 대한 응답을 설정하려고합니다. 그러나, 나는 함수상태를 약속으로 설정하면 this.setState가 함수가 아닙니다.

getMovie(e){ 
    //use local this 
    let _this = this; 
    axios.get(`http://www.omdbapi.com/?i=${e.value}`) 
    .then((response) => { 
    _this.setState({movie: response.data }); 
}) 
.catch((error) => { 
console.log(error); 
}); 
} 

모든 아이디어는, _this.setState 오류가되지가 계속?

+1

당신이'getMovie (E)'메소드를 호출하는 곳에서 일부를 표시 할 수 있습니다? –

+0

'console.log (_this)'를하면 어떻게됩니까? – saadq

+0

아마도 saadq는 메서드 내에서 '_this'에 액세스 할 수 없다는 점을 지적하고자하므로 메서드 매개 변수 –

답변

0

덕분에 응답, 나는 this에 결합하는 것을 잊었다 :

this.getMovie.bind(this)

+1

FWIW를 통해 사용할 수 있도록 설정해야합니다. [_this]는 사용할 필요가 없으므로 [ "fat arrows"] (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions#Arrow_functions). – robertklep

관련 문제