首页 > 解决方案 > 将参数传递给嵌套导航器

问题描述

我正在尝试将参数传递给另一个基于类的组件。当我在第一页(登录页面)上提醒值时,我得到了正确的结果,但在另一页(主页)上我得到了一个错误!但是,我要导航到的主页有 4 个选项卡,我正在尝试将参数传递给每个选项卡,从 profile 开始。

登录.js:

login = () => {
        if (this.state.inputFieldsStatus == true)
            {  
                fetch('http://192.168.0.6:3000/login', {
                    method: 'POST', // *GET, POST, PUT, DELETE, etc.
                    headers: {
                        'Accept': 'application/json',
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({
                        email: this.state.email,
                        password: this.state.password,
                    })
                })
                .then((response) => response.json())
                .then((res) => {

                    alert(JSON.stringify(res.message));
                
                    if (res.success === true ) {
                        this.props.navigation.navigate('Home', 
                            {
                                screen: 'Messages',
                                params: 
                                    {
                                        message: res.message,
                                        otherParam: 'anything you want here'
                                    } 
                            },
                            {
                                screen: 'Profile',
                                params: 
                                    {
                                        message: res.message,
                                        otherParam: 'anything you want here'
                                    } 
                            }
                        );
                    } else {
                        this.setState({passwordError: res.message })
                    }
                })
                .done();
            }
            else 
                {
                    // alert(' Please fill in all fields! ');
                    this.setState({passwordError:" Please fill in all fields! "})
                }
    }
}

export default seeker_login 

另一个页面(导出类 View_Profile 扩展组件):

return (
          <>
          <View style={styles.container}>

                <View style={styles.header}>
                    <Text>
                        {JSON.stringify(this.props.route.getparams.otherParam)}
                    </Text>
                </View>

错误: 在此处输入图像描述

标签: react-nativereact-routerreact-native-androidreact-native-iosreact-native-navigation

解决方案


推荐阅读