2017-04-18 2 views
0

GET 요청 (객체 배열)의 응답을 가져오고 배열의 템플릿 (templates)에 상태를 설정하려고합니다. 그러면 나중에이 코드에 액세스 할 수 있습니다. 그래서 같은 state.templates는 : 나는 setState를 줄이 주석하고 바로 응답을 기록 콘솔이있을 때React Native this2.state error

fetch('https://tiy-warmup.herokuapp.com/api/templates?token=' + 
    token) 
    .then(response => response.json()) 
    .then(response => this.setState({templates: response})) 
    // .then(response => console.log(response)) 

, 나는 다시 올바른 배열을 얻을, 그래서 난 내 좋은 가져 오는 것을 알고있다.

Possible Unhandled Promise Rejection (id: 0): 
_this2.setState is not a function 

은 무엇이 오류 주위를 얻고 렌더링에 this.state.templates를 사용 할 수있을 해결책이 될 것이다 : 나는 setState를 줄을 실행하려고 할 때 그러나, 나는이 오류가? 기능을 결합 또는 기능을 화살표로 변경하거나, 당신이 당신의 약속을 catch를 구현하지 않는 때문에

.catch(error => { 
    console.log('There has been a problem with your fetch operation: '+ error.message); 
}); 

답변

0

것은이 인출 사용하고 어디든지

+0

이 감사를 받아! 화살표 기능이 작동했습니다. –

0

Possible Unhandled Promise Rejection

이 오류가 발생합니다. this은 구성 요소에 바인딩되지 않으므로 this.setState에서 오류가 발생합니다. 오류를 처리하도록 catch 문을 설정하지 않았기 때문에 처리되지 않은 약속 거부 오류가 표시됩니다.

0

_this2.setState is not a function 일반적으로 이것은 사용자가 참조한 구성 요소가 아니므로 디버거를 시작하고 this을 신중히 확인하십시오. 이 문제를 해결하려면 bind 또는 const self = this을 사용하는 것이 좋습니다.

당신을 도울 수 있습니다이 게시물을 살펴, https://www.sitepoint.com/bind-javascripts-this-keyword-react/