登录后在导航中苦苦挣扎-对本机做出反应

问题描述 投票:0回答:2

在以响应方式从登录屏幕导航到仪表板时遇到问题,并遇到类似TypeError的问题:undefined不是一个对象(评估'_this.props.navigation.navigate'),以便进行更明确的说明,正在附加屏幕截图和图像。任何帮助都将是可贵的。

App.js

import React, {Component} from 'react';
import {
  SafeAreaView,
  StyleSheet,
  ScrollView,
  View,
  Text,
  StatusBar,
} from 'react-native';

import {
  Header,
  LearnMoreLinks,
  Colors,
  DebugInstructions,
  ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';

import {createSwitchNavigator, createAppContainer}  from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import {createDrawerNavigator}  from 'react-navigation-drawer';
import { createBottomTabNavigator } from 'react-navigation-tabs';
import Icon from 'react-native-vector-icons/FontAwesome';  

import WelcomeScreen  from './screens/Welcome';
import LoginScreen  from './screens/Login';
import DashboardScreen  from './screens/Dashboard';
import SearchScreen from './screens/Search';
import ProfileScreen from './screens/Profile';
import BookingScreen from './screens/Booking';
import SupportScreen from './screens/Support';

const App: () => React$Node = () => {
  return (
    <AppContainer />
  );
};


const DashboardTabNavigator = createBottomTabNavigator(
  {
        Search :{screen : SearchScreen,
          navigationOptions:{  
            headerTitle: 'Search',
            tabBarLabel:'Search',  
            tabBarIcon:({tintColor})=>(  
                <Icon name="search" color={tintColor} size={30}/>  
            )  
          }  
      },
      Booking :{screen : BookingScreen,
        navigationOptions:{  
          headerTitle: 'Booking',
          tabBarLabel:'Booking',  
          tabBarIcon:({tintColor})=>(  
              <Icon name="list" color={tintColor} size={30}/>  
          )  
        }  
      },
      Support :{screen : SupportScreen,
        navigationOptions:{  
          headerTitle: 'Support',
          tabBarLabel:'Support', 
          tabBarIcon:({tintColor})=>(  
              <Icon name="support" color={tintColor} size={30}/>  
          )  
        }  
      },
      Profile :{screen : ProfileScreen,
        navigationOptions:{  
          headerTitle: 'Profile',
          tabBarLabel:'Profile',  
          tabBarIcon:({tintColor})=>(  
              <Icon name="user" color={tintColor} size={30}/>  
          )  
        }  
      },
  },
  {
    tabBarOptions: {
      showIcon: 'true', // Shows an icon for both iOS and Android
    },
  }
);

const DashboardStackNavigator = createStackNavigator({
  Welcome :{screen : WelcomeScreen},
  DashboardTabNavigator : DashboardTabNavigator
});

const AppSwitchNavigator = createSwitchNavigator({
  Welcome :{screen : WelcomeScreen},
  Dashboard :{screen :  DashboardStackNavigator}
});

const AppContainer = createAppContainer(AppSwitchNavigator);

const styles = StyleSheet.create({
  scrollView: {
    backgroundColor: Colors.lighter,
  },
  engine: {
    position: 'absolute',
    right: 0,
  },
  body: {
    backgroundColor: Colors.white,
  },
  sectionContainer: {
    marginTop: 32,
    paddingHorizontal: 24,
  },
  sectionTitle: {
    fontSize: 24,
    fontWeight: '600',
    color: Colors.black,
  },
  sectionDescription: {
    marginTop: 8,
    fontSize: 18,
    fontWeight: '400',
    color: Colors.dark,
  },
  highlight: {
    fontWeight: '700',
  },
  footer: {
    color: Colors.dark,
    fontSize: 12,
    fontWeight: '600',
    padding: 4,
    paddingRight: 12,
    textAlign: 'right',
  },
});

export default App;

Welcome.js的脚本

import React, {Component} from 'react';
import {
    StyleSheet,
    Text,
    View,
    Animated,
    Button,
    Dimensions,
    TouchableOpacity,
    TouchableHighlight,
    Image,
    ImageBackground,
    ScrollView,
    StatusBar, 
    Alert
} from 'react-native';

let { height, width } = Dimensions.get('window');

let InputWidth = width - ((width * 10) /100);

import Icon from 'react-native-vector-icons/FontAwesome'; 

import LoginScreen from './Login';

class WelcomeScreen extends Component{
    constructor(props) {
        super(props);
        this.state = { isAnimationVisible: true, animationProgress: new Animated.Value(0) };
        //this.LoginScreen = this.LoginScreen.bind(this);
    }

    componentDidMount() {
        Animated.timing(this.state.animationProgress, { toValue: 1, duration: 5000 })
            .start(() => this.setState(s => ({ ...s, isAnimationVisible: false })));
    }

    render() {
        if (this.state.isAnimationVisible) {
            const interpolation = this.state.animationProgress.interpolate({ inputRange: [0, 1], outputRange: [0, 1] });
            return (
                <View style={styles.backgroundColor}>
                     <Text style={styles.textAlignmentSplashHeader}>Text App</Text>
                     <View style={styles.textAlignmentSplashCenter}>
                        <Text style={styles.textAlignmentSplash}> <Icon name="check-square-o" color="#00bfff" size={20}/> Verified Provider</Text>
                        <Text style={styles.textAlignmentSplash}> <Icon name="check-square-o" color="#00bfff" size={20}/> Trained Professional</Text>
                        <Text style={styles.textAlignmentSplash}> <Icon name="check-square-o" color="#00bfff" size={20}/> Compare Prices</Text>
                        <Text style={styles.textAlignmentSplash}> <Icon name="check-square-o" color="#00bfff" size={20}/> At door step</Text>
                     </View>
                </View>
            );
        }
        else {
            return (<LoginScreen />);
        }
    }
}


export default WelcomeScreen;

const styles = StyleSheet.create({
    backgroundColor: {
        flex: 1,
        resizeMode: 'cover', // or 'stretch',
        justifyContent: 'center',
        backgroundColor: '#ffffff'
    },
    textAlignmentSplashHeader :{
        textAlign:'center',
        fontSize:35,
        //color : '#70AB8F',
        color : '#DC5B21'
    },
    textAlignmentSplashCenter :{
        width: InputWidth,
        margin : '20%',
        justifyContent: 'center',
    },
    textAlignmentSplash :{
        textAlign:'left',
        fontSize:20,
        color : '#808080'
    },
  }); 

Login.js

import React, {Component} from 'react';
import {
    StyleSheet,
    Text,
    View,
    Button,
    TouchableOpacity,
    TouchableHighlight,
    Image,
    ImageBackground,
    ScrollView,
    StatusBar, 
    Alert
} from 'react-native';

import { Dashboard } from './Dashboard';

class LoginScreen extends Component{
    static navigationOptions = {
        header : 'Login'
    } 

    constructor(props){
        super(props);
        this.state={
            mobile : null
        }
        this.__login = this.__login.bind(this);
    }
    render(){
        return(
            <View style={{flex : 1, alignItems : 'center', justifyContent : 'center'}}>
                <Text>LoginScreen</Text>
                <Button 
                    title ="Login"
                    onPress={this.__login}
                />
            </View>
        );
    }
    __login = ()=>{
        this.props.navigation.navigate('Dashboard')
    }
}

export default LoginScreen; 

Dashboard.js

import React, {Component} from 'react';
import {
    StyleSheet,
    Text,
    View,
    Button,
    TouchableOpacity,
    TouchableHighlight,
    Image,
    ImageBackground,
    ScrollView,
    StatusBar, 
    Alert
} from 'react-native';

class DashboardScreen extends Component{
    render(){
        return(
            <View style={{flex : 1, alignItems : 'center', justifyContent : 'center'}}>
                <Text>DashboardScreen</Text>
            </View>
        );
    }
}

export default DashboardScreen; 

并且下面是在登录屏幕中单击登录按钮后的错误

Login.js

单击登录按钮后出现错误,导航代码在Login.js中

enter image description here

请帮助我。

react-native react-navigation
2个回答
1
投票
udpate your createSwitchNavigator

1
投票
您必须像这样在WithNavigation组件中使用LoginScreen
© www.soinside.com 2019 - 2024. All rights reserved.