1

I 반응 네이티브에 사용자가 로그인 할 수있는 간단한 예를 다음입니다 "정의되지 않은은 (평가 '_this2.onLoginSuccess.bind') 객체가 아닌". 나는 코드가반작용 - 네이티브 오류

를 처리하기 위해 다음과 같은 추가 된
onButtonPress() { 
    const { email, password } = this.state; 
    this.setState({ error: '', loading: true }); 

    firebase.auth().signInWithEmailAndPassword(email, password) 
    .then(this.onLoginSuccess.bind(this)) 
    .catch(() => { 
     firebase.auth().createUserWithEmailAndPassword(email, password) 
     .then(this.onLoginSucess.bind(this)) 
     .catch(this.onLoginFail.bind(this)); 
    }); 
} 

onLoginSuccess() { 
    this.setState({ 
     email: '', 
     password: '', 
     loading: false, 
     error: '' 
    }); 
} 

onLoginFail() { 
    this.setState({ 
     error: 'Authentication Failed', 
     loading: false 
    }); 
} 

하지만 난 반응 네이티브 매우 새로운 오전 "undefined is not an object (evaluating '_this2.onLoginSuccess.bind') "

오류 때문에 설명해주십시오.

enter image description here

+1

내 생각 엔'onButtonPress'가 이벤트 핸들러로 사용하고 있기 때문에 아마 제대로 바인딩되지 않았을 것이다. – Li357

답변

0

연간 문제는 내가 메소드 이름을 맞춤법이 틀린 것입니다. 대신 onLoginSuccess, 나는 그것을 onLoginSucess라고 불렀다.

1

bind() 번을 그렇게 여러 번 입력 할 수 없습니다. 자리에 bind()하려면 종종 익명의 기능에서만 작동합니다.

대신이 작업을 수행 : 내가 가진

constructor(props) { 
    super(props); 

    this.onLoginSuccess = this.onLoginSuccess.bind(this); 
    this.onLoginFailed = this.onLoginFailed.bind(this); 
} 

onButtonPress() { 
    const { email, password } = this.state; 
    this.setState({ error: '', loading: true }); 

    firebase.auth().signInWithEmailAndPassword(email, password) 
    .then(this.onLoginSuccess) 
    .catch(() => { 
     firebase.auth().createUserWithEmailAndPassword(email, password) 
     .then(this.onLoginSucess) 
     .catch(this.onLoginFail); 
    }); 
} 

onLoginSuccess() { 
    this.setState({ 
     email: '', 
     password: '', 
     loading: false, 
     error: '' 
    }); 
} 

onLoginFail() { 
    this.setState({ 
     error: 'Authentication Failed', 
     loading: false 
    }); 
} 
+0

감사합니다. @Val. 그 문제가 해결되었습니다 – pixel

+1

또는이 유형을 사용하여 함수를 선언하십시오 : onLoginSuccess =() => {귀하의 함수에있는 것들 ...}. 그런 식으로 처리하면 "이"바인딩이 필요하지 않습니다. – Dedpul

0
onButtonPress() { 
    const { email, password } = this.state; 
    this.setState({ error: '', loading: true }); 

    firebase.auth().signInWithEmailAndPassword(email, password) 
    .then(this.onLoginSuccess.bind(this)) 
    .catch(() => { 
     firebase.auth().createUserWithEmailAndPassword(email, password) 
     .then(this.onLoginSucess.bind(this)) 
     .catch(this.onLoginFail.bind(this)); 
    }); 
} 

onLoginSuccess =() => { 
    this.setState({ 
     email: '', 
     password: '', 
     loading: false, 
     error: '' 
    }); 
} 

onLoginFail =() => { 
    this.setState({ 
     error: 'Authentication Failed', 
     loading: false 
    }); 
}