2017-02-20 69 views
0

對於如此「onPress = {this.onPressButton.bind(這)}」導致我的應用程序失敗?陣營本地未定義不是(評價「this.onPressButton.bind」)的對象

我能做些什麼來解決它。我相信它與(這)變得沒有束縛或某事有關?

我已經被渲染了一個名爲登錄組件:

export default class Login extends Component { 

constructor(props) { 
    super(props); 
    this.state = { 
     id:'login' 
    } 
} 

render() { 
    return(
     <KeyboardAvoidingView behavior="padding" style={style.container}> 
      <View style={style.logoContainer}> 
       <StatusBar barStyle="light-content" /> 
       <Image style={style.logo} source={require('../../image/Octocat.png')} /> 
       <Text style={style.logoText}> A Github app using React Native by{"\n"} Thomas Charlesworth</Text> 
      </View> 
      <View style={style.formContainer}> 
       <LoginForm /> 
      </View> 
     </KeyboardAvoidingView> 
    ); 
} 
} 

和登錄表單組件:

export default class LoginForm extends Component { 

onPressButton(){ 
    this.props.navigator.push({ 
     id: 'splash', 
     passProps: { 
     // Your Props 
     } 
    }) 
} 

render() { 
    return(
     <View style={style.container}> 
      <TextInput 
       style={style.input} 
       placeholder="Username" 
       placeholderTextColor="rgba(255,255,255,0.5)" 
       returnKeyType="next" 
       autoCapitalize="none" 
       autoCorrect={false} 
       onSubmitEditing={()=> this.passwordInput.focus()} 
      /> 
      <TextInput 
       style={style.input} 
       secureTextEntry 
       placeholder="Password" 
       placeholderTextColor="rgba(255,255,255,0.5)" 
       returnKeyType="go" 
       ref={(input) => this.passwordInput = input} 
      /> 
      <TouchableOpacity 
       style={style.buttonContainer} 
       onPress={this.onPressButton.bind(this)} 
      > 
       <Text style={style.buttonText}> 
        LOGIN 
       </Text> 
      </TouchableOpacity> 
     </View> 
    ); 
} 
} 
+0

您是否定義了此方法'onPressButton' ?? –

+0

我做了一個編輯請參考。我定義了一個方法是。哪些停止錯誤,但當我點擊按鈕,我得到:undefined不是一個對象(評估'this.props.navigator.push') –

回答

1

寫這樣的:

傳遞從父組件的功能:

<LoginForm update={this.update.bind(this)}> 

navigate(data){ 
    this.props.navigator.push({data}); 
} 

在子組件中:

onPressButton(){ 
    let data = { 
     /*data*/ 
    } 
    this.props.navigate(data); 
} 
+0

那麼解決方案是什麼? –

+0

onPressButton(){ this.props.navigator.push({id:'splashing' }) }不起作用 –

+0

乾杯!非常感謝:D –

相關問題