2017-10-23 9 views
1

내 매개 변수가 정의되지 않았는지 확인하는 방법을 찾고 있습니다. 나는 그것을 시도했지만 작동하지 않습니다내 기본 매개 변수가 정의되지 않은 경우 실제 기본 검사

"정의가 ('this.props.navigation.state.params.Username을'평가) 객체가 아닌"

componentWillMount() { 
    if (typeof this.props.navigation.state.params.Username != 'undefined'){ 
     this.getInfos() 
    } else { 
     this.navigate('ComponentOne'); 
    } 
} 

아직도이 오류가 어떤 생각?

감사

답변

1

componentDidMount을 (사용해보십시오) 대신 componentWillMount의()

componentDidMount() { 
    if (this.props.navigation.state.params.Username){ 
     this.getInfos() 
    } else { 
     this.navigate('ComponentOne'); 
    } 
} 

여전히 정의되지 않은 오류가 발생하는 경우 'params'자체가 정의되지 않아야합니다. 당신은 정의되지 않은

console.log(this.props.navigation.state.params) 

검사에 도트 연산자를 사용할 수없는 경우는 당신의 결정을 실수 답변에 대한

+0

고마워, 대답에 내 오류를 발견! –

+0

@DuvalTheophane 내 답변과 동일하므로 console.log()의 결과는 무엇입니까? – nhoxbypass

0

이 시도 :

if (this.props.navigation.state.params.Username) { 
    this.getInfos() 
} 

또는 (Reactjs에서 테스트) 큰 따옴표로 따옴표를 대체 :

if (typeof this.props.navigation.state.params.Username != "undefined") { 
    this.getInfos() 
} 
+0

감사합니다,하지만 doesn'twork 어디 정의 및 디버깅 :/ –

+0

@DuvalTheophane 당신이 이중으로 작은 따옴표를 교체하려고합니까 "undefined"에 대한 인용문? – nhoxbypass

+0

아직도 오류가 있습니다 :/ –