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

标签 react-native react-navigation

我是react-native新手,我正在尝试使用StackNavigator,但它不起作用 我正在尝试调用一个组件并在您单击按钮时呈现它。 但我收到此错误: undefined 不是对象(正在计算“_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

关于react-native - native react :undefined is not an object (evaluating '_this2.props.navigation.navigate' ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46743628/

相关文章:

swift - React Native - 调用 native Swift 函数调用另一个触发 RCTEventEmiiter 发送事件的函数导致 RCTCallableJSModules 错误

javascript - 错误 "LexAudio.conversation is not a constructor"。 React 组件调用 vanilla javascript

javascript - Material UI 图标的类型

react-native - 使用 navigation.goBack() 时如何将数据发送回前一屏幕?

react-native - 在 React Navigation 5 中的另一个导航器中导航到屏幕

reactjs - 如何在 React Native 屏幕之间共享状态?

react-native - 任务 :react-native-screens:javaPreCompileDebug error in React Navigation 5. x

facebook - 如何在 React Native Facebook SDK 中使用标准事件

node.js - 我正在使用 React-native 和 Node js。在收到 Node js 的响应后,我无法从一个屏幕导航到另一个屏幕

react-native - 如何在 React Navigation 5 中将父函数传递给子屏幕?