onTypeSelect (str) {
this.setState({taskType: str})
}
<TouchableOpacity onPress={this.onTypeSelect('type1').bind(this)}>
</TouchableOpacity>
undefined는 객체가 아닙니다 ( '_this3.onTypeSelect ('type1) .bind '평가).
누구든지 나를 도울 수 있습니까? 저는 react-native의 초보자입니다.
이 시도
onTypeSelect (str) {
this.setState({taskType: str})
}
<TouchableOpacity onPress={() => { this.onTypeSelect('type1'); }} />
.bind (this) 때 함수의 반환 값이 아닌 함수를 전달해야합니다 (이 경우undefined
. 이 방법은 함수가 구성 요소 범위 내에서 호출되어 this
유효한 참조 를 만들기 때문에 작동 합니다.
문제를 해결하는 또 다른 방법은 다음과 같이 bind를 사용하는 것입니다.
this.onTypeSelect.bind(this, 'type1')
편집 : 의견 및 기타 답변에 감사 드리며 완성 및 정보를 위해 정보를 추가했습니다. 알렉산더 T. , Bartek F.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다