2017-11-15 12 views
0
export default class App extends React.Component { 
    constructor(props){ 
    super(props); 
    this.state = { 
     createId: 'create ID here!', 
     createPw: 'create Pw here', 
     inputId: 'input Id', 
     inputPw: 'input Pw', 
     sentence: '', 
     kev : false 
    } 
    } 


    _onPressButton(){ 
    if(this.state.kev) 
    Alert.alert('You tapped the button!'); 
    } 

저는 kev 값을 사용하고 싶습니다. 사실이라면 경고하고, 그렇지 않으면 false입니다.반응 상태 값을 사용하고 싶습니다. 어떻게해야합니까?

는하지만 난 오류가 발생했습니다 왜

사람이 그 오류 메시지가 발생하는 이유는 상태 값 케빈과 를 호출하는 방법을 설명 할 수있다 '(this.state.kev 평가') '객체가 아닌 정의되지 않은'궁금합니다.

감사합니다.

답변

1

귀하의 생성자에 _onPressButton() 함수를 바인딩해야합니다. 두 가지 방법으로 그것을 할 수 있습니다 : - 생성자에

export default class App extends React.Component { 
     constructor(props){ 
     super(props); 
     this.state = { 
      createId: 'create ID here!', 
      createPw: 'create Pw here', 
      inputId: 'input Id', 
      inputPw: 'input Pw', 
      sentence: '', 
      kev : false 
     } 

    this._onPressButton = this._onPressButton.bind(this) 
     } 


    _onPressButton(){ 
    if(this.state.kev) 
    Alert.alert('You tapped the button!'); 
    } 
+0

미안 바인딩을 사용하여 화살표 기능

_onPressButton =() => { if(this.state.kev) Alert.alert('You tapped the button!'); } 

를 사용 하지만 난 처음에 대한 개념이 없습니다 : 바인드를 ....? 둘째 : 내가 사용한 방법과 화살표 func ...의 차이점은 무엇입니까? 도와 주셔서 감사합니다 – KimDongHwan

+0

@ 김동환 바인드를 이해하는 데 도움이 될 답변을 읽어보십시오. 또한 화살표 기능은 ES6 개념이며 매우 유용합니다. https://stackoverflow.com/questions/2236747/use-of-the-javascript-bind-method – Jagrati

+0

댓글에 대단히 감사합니다 !!!! – KimDongHwan