undefined不是对象(评估this.props.navigator.push)

时间:2016-05-25 15:18:37

标签: react-native

我有两页,即登录页面和付款页面。 我试图导航到点击按钮上的付款页面,但我得到错误未定义不是一个对象(评估this.props.navigator.push)

代码如下:

import React, {
StyleSheet,
Text,
View,
TextInput,
Component,
Alert,
Navigator
} from 'react-native';

var Button = require('react-native-button');
import Payments from '../payments'


class Signin extends Component{


 onSubmitPressed(){
    this.props.navigator.push({
        title: 'secure Page',
        component: <Payments/>

    });


  };

render() {
return(

 <View style={styles.container}>
<View style={styles.Inputview}>
<TextInput id='user' style={styles.input}
placeholder={'Username'}
/>
<TextInput id='Password' secureTextEntry={true}
placeholder={'Password'}
onChangeText={password => this.setState({password})}
/>
</View>

<View >
 <Button style={styles.Register}  
 onPress={(this.onSubmitPressed)}>
  Sign In
  </Button>
 </View>
 </View>
 )
  }
 }


 export default Signin


If any one let me know how to solve this issue??

2 个答案:

答案 0 :(得分:7)

您需要将导航器和初始路线设置为入口点,而不是常规组件。尝试这样的事情:

(还设置了一个工作示例here

https://rnplay.org/apps/iKx2_g

class App extends Component {
  renderScene (route, navigator) {
    return <route.component navigator={navigator} />
  }
  render() {
    return (
      <Navigator
        style={styles.container}
        renderScene={this.renderScene.bind(this)}
        initialRoute={{component: SignIn}}  
      />
    );
  }
}

class SignIn extends Component {
  _navigate () {
    this.props.navigator.push({
        component: Payments
    })  
  }
  render () {
    return (
      <View>
        <Text>Hello from SignIn</Text>
        <Button onPress={this._navigate.bind(this)} />
      </View>
    )
  }
}

class Payments extends Component {
    render () {
    return (
      <Text>Hello from Payments</Text>
    )
  }      
}

答案 1 :(得分:5)

首先,您需要将this绑定到函数onSubmitPressed。并确保您已在导航器的renderScene功能上将导航器对象传递给此组件。

// binding this to onSubmitPressed
<Button style={styles.Register}  
   onPress={this.onSubmitPressed.bind(this)}
 >
    Sign In
</Button>

// binding this to on SubmitPressed using arrow function 
<Button style={styles.Register}  
   onPress={() => this.onSubmitPressed()}
 >
    Sign In
</Button>