如何将数据从一个屏幕发送到TabNavigator

时间:2018-08-06 04:39:23

标签: reactjs react-native react-navigation

我有一个带有2个输入项(电子邮件和密码)的登录屏幕,如果登录正确,我想向TabNavigator发送一封这样的电子邮件

this.props.navigation.navigate("Profile", {
   email: email
})

我想收到这样的参数

const email = navigation.getParam('email');

但不幸的是,它不起作用。

1 个答案:

答案 0 :(得分:0)

/* --------- Define 'name' in state ---------*/

state = {
    name: ''
}

/*------- if you will get success response then write this  -------*/

LoginAPICall = () => {
    fetch(‘URL’, {
        method: 'POST',
            headers: {
            'Accept': 'application/json',
                'Content-Type': 'application/json',
        },
        body: JSON.stringify({
            Email: this.state.email,
            Password: this.state.password
        })
    }).then((response) => response.json())
        .then((responseJson) => {
            if(responseJson.statuscode == 1) {
                this.props.navigation.navigate(‘Login’,{
                    NAME : responseJson['data']['user'].name
                });
                alert("Check your email for a password reset link!");
            } else {
                Alert.alert(responseJson.message);
            }
        }).catch((error) => {
            console.error(error);
        });
}


/*--------- in next activity controller write below lines ---------*/

constructor(){
    super();
    this.state = {
        User_Name : '',
    }
}

componentDidMount(){
    this.setState({
        User_Name: this.props.navigation.state.params.NAME,
    })
}

render () {
    alert (this.state.User_Name);
    return (
        <View>
            <Text>Hello</Text>
        </View>
    );
}