react native :undefined 不是一个对象(评估'_this2.props.navigation.navigate')

react native :undefined is not an object (evaluating '_this2.props.navigation.navigate')

我是 react-native 的新手,我正在尝试使用 StackNavigator,但它不起作用 我正在尝试调用一个组件并在您单击按钮时呈现它。 但我收到这个错误: undefined is not an object (evaluating '_this2.props.navigation.navigate')

这是我的主要组件:

export default class Home extends Component<{}> {

    constructor(props) {
        super(props);
        this.email = null;
        this.amount = null;
        this.device = null;
    }

    render() {
        return (
            <ScrollView style={styles.styleSV}>
                <Text
                    style={styles.styleLogin}>
                    Login
                </Text>
                <View style={styles.styleForm}/>
                <TextInput placeholder='email' onChangeText={(text) => this.email }/>
                <TextInput placeholder='amount' onChangeText={(text) => this.amount }/>
                <TextInput placeholder='device' onChangeText={(text) => this.device }/>
                <View style={styles.styleButtonSignup}/>
                <Button
                    onPress={() =>
                        this.props.navigation.navigate('PaypalPayment', { email: this.email })
                    }
                    title='Next'
                />
            </ScrollView>
        );
    }

}

const NavigationScreen = StackNavigator({
    PaypalPayment: { screen: PaypalPayment },
    Home: { screen: Home}
});

AppRegistry.registerComponent('paypal', () => NavigationScreen);

我发现了我的错误:

在我的 App.js 我没有打电话给我 StackNavigator

export const RootNavigation = StackNavigator({
    Home: { screen: Home },
    PaypalPayment: { screen: PaypalPayment }
});

AppRegistry.registerComponent('paypal', () => RootNavigation);

export default class App extends Component<{}> {

   render() {
        return (
            <RootNavigation/>
        )
    }
}

现在可以使用了。

我使用了这个文档:https://reactnavigation.org/docs/intro/#Introducing-Stack-Navigator

Tha's how I solved it.

转到您正在使用的组件的父组件navigation。 并调用组件如下。

<Component {...this.props} />

这使得 navigation 也可用于子组件。