我使用React-native和节点js.In从Node js获得响应后无法从一个屏幕导航到另一个屏幕

时间:2018-06-19 08:02:34

标签: node.js react-native

这是我的登录功能,点击登录按钮

后执行
login=()=>{
    fetch('http://192.168.0.101:9090/Shakehands',{
        method:'post',
        headers:{
            'Accept':'application/json',
            'Content-type':'application/json',
        },
        body:JSON.stringify({
            email:this.email,
            password:this.password,
            text:"Login"
        })
    })
    .then((responseData)=>{
        return responseData.json();
    })
    .then((responseData)=>{
        //console.log(responseData);
        //alert(this.state.insdata)
        if(responseData.success==true){
            this.props.navigation.navigate('Home');
        }
        else{
            alert(responseData.message);
        }
    }).done();
}

这是我的node.js后端

con.query('SELECT *  from Shakehands where email=? AND password=?',[email,password],function(error,rows,fields){
  if(!!error) console.log(error);

  if(rows.length>0){
    res.send({'success':true,'message':'Welcome'})
  }
  else{
    res.send(JSON.stringify({'success':false,'message':'User not found'}))
  }
})

///导航功能

const Navigation = StackNavigator({
Splash:{
    screen:Splash,
    navigationOptions:{
        header:null,
    }
},
Login:{
    screen:Login,
    navigationOptions:{
        header:null,
    }
},
Signup:{
    screen:Signup,
    navigationOptions:{
      header:null,
    }
},
ForgetPass:{
    screen:ForgetPass,
    navigationOptions:{
        header:null,
    }
},
Home:{
    screen:Home,
    navigationOptions:{
        header:null,
    },
}

}) 导出默认导航;

登录功能包含在文件logtxtbox中,其中包含文本输入和按钮,而按钮又包含在登录文件中。

// Login.js 导入React,{Component}来自'反应&#39 ;; 从' react-native'导入{View,AppRegistry,StyleSheet,ScrollView,TouchableOpacity,Text,StatusBar}; 从' ../ Components / Animate'中导入Animate; 从' ../ Components / Logtxtbox';

导入Logtxtbox

导出默认类登录扩展组件{

render(){
    return(
        <ScrollView>
            <View style={styles.container}>
            <StatusBar backgroundColor='#1c313a'/>
            <Animate />
            <Logtxtbox />


            <TouchableOpacity onPress={()=>this.props.navigation.navigate('ForgetPass')}>
                <Text title='ForgotPassword' style={{fontSize:16 ,marginTop:10 ,
                    color:'white',fontWeight:'bold'}}>ForgotPassword?</Text>
            </TouchableOpacity>

            <Text style={styles.sign}>Don't have an acoount yet?</Text>

            <TouchableOpacity onPress={()=>this.props.navigation.navigate('Signup')}>
                <Text title='SignUp' style={styles.signup}>SignUp</Text> 
            </TouchableOpacity>

            </View>   
        </ScrollView>   
    )
}

} AppRegistry.registerComponent(&#39;登录&#39;()=&GT;登录)

const styles = StyleSheet.create({     容器:{         的backgroundColor:&#39;#455a64&#39 ;,         alignItems:&#39;中心&#39 ;,         身高:600     },     注册:{         颜色:&#39;白色&#39 ;,         字体:16,         fontWeight设置:&#39;粗体&#39 ;,         textAlign设置:&#39;中心&#39 ;,     },     标志:{         textAlign设置:&#39;中心&#39 ;,         颜色:&#39;白色&#39 ;,         字体:16,         marginTop:20,     } })

1 个答案:

答案 0 :(得分:0)

您必须将this.props.navigation传递给内部组件。您可以使用这部分代码:

render(){
    return(
        ...
        <Logtxtbox navigation = { this.props.navigation }/>
        ...
    )
}