Javascript 为什么赢了';我的堆栈导航器没有出现吗?

Javascript 为什么赢了';我的堆栈导航器没有出现吗?,javascript,ios,reactjs,react-native,Javascript,Ios,Reactjs,React Native,我试图在程序中使用StackNavigator。在Login.js文件中,我试图使用来实现这一点,但它不起作用。我得到下面的错误。为什么会这样?顺便说一句,我跟在后面 下面是index.ios.js: import React, { Component } from 'react'; import { AppRegistry, View} from 'react-native'; import { Provider } from 'react-redux'; import Application

我试图在程序中使用
StackNavigator
。在
Login.js
文件中,我试图使用
来实现这一点,但它不起作用。我得到下面的错误。为什么会这样?顺便说一句,我跟在后面

下面是
index.ios.js

import React, { Component } from 'react';
import { AppRegistry, View} from 'react-native';
import { Provider } from 'react-redux';
import Application from './pages/Application';
import store from './redux';
import api from './utilities/api';
import StackNavigator from 'react-navigation';
import Login from './pages/Login';
import About from './pages/About';

const App = StackNavigator({
    Home: { screen: Login },
    Profile: { screen: About },
});

export default class DApp extends Component {
    constructor(props) {
        super(props);
        this.state = {
            data: [],
            isLoading: true
        }
        console.log("something");
    }

    componentWillMount() {
        api.getData().then((res) => {
            this.setState({
                data: res.data
            })
        });
    }

    render() {
        if(this.state.isLoading) {
            console.log("The data is: " + this.state.data);
        } else {
            console.log("Else got executed");
        }

        return (
            <View>
                <Provider store={store}>
                    <Application/>
                </Provider>
                <App/>
            </View>
        );
    }
}

AppRegistry.registerComponent('DApp', () => DApp);
import React, { Component } from 'react';
import { connect } from 'react-redux';
import { ScrollView, Text, TextInput, View, Button, StyleSheet } from 'react-native';
import { login } from '../redux/actions/auth';
import {AuthenticationDetails, CognitoUser, CognitoUserAttribute, CognitoUserPool} from '../lib/aws-cognito-identity';
import About from './About';

const awsCognitoSettings = {
    UserPoolId: 'something',
    ClientId: 'something'
};

class Login extends Component {
    constructor(props) {
        super(props);
        this.state = {
            page: 'Login',
            username: '',
            password: ''
        };
    }

    // get alt () {
    //     return (this.state.page === 'Login') ? 'SignUp' : 'Login';
    // }

    get alt() {
        if(this.state.page == 'Login') {
            return 'SignUp';
        } else {
            return 'Login';
        }
    }

    handleClick (e) {
        e.preventDefault();
        const userPool = new CognitoUserPool(awsCognitoSettings);

        // Sign up
        if (this.state.page === 'SignUp') {
            const attributeList = [
                new CognitoUserAttribute({ Name: 'email', Value: this.state.username })
            ];
            userPool.signUp(
                this.state.username,
                this.state.password,
                attributeList,
                null,
                (err, result) => {
                    if (err) {
                        alert(err);
                        this.setState({ username: '', password: '' });
                        return;
                    }
                    console.log(`result = ${JSON.stringify(result)}`);
                    this.props.onLogin(this.state.username, this.state.password);
                }
            );
        } else {
            const authDetails = new AuthenticationDetails({
                Username: this.state.username,
                Password: this.state.password
            });
            const cognitoUser = new CognitoUser({
                Username: this.state.username,
                Pool: userPool
            });
            cognitoUser.authenticateUser(authDetails, {
                onSuccess: (result) => {
                    console.log(`access token = ${result.getAccessToken().getJwtToken()}`);
                    this.props.onLogin(this.state.username, this.state.password);
                },
                onFailure: (err) => {
                    alert(err);
                    this.setState({ username: '', password: '' });
                    return;
                }
            });
        }
    }

    togglePage (e) {
        this.setState({ page: this.alt });
        e.preventDefault();
    }

    render() {
        const { navigate } = this.props.navigation;
        return(
            <ScrollView style={{padding: 20}}>
                <Text style={{fontSize: 27}}>{this.state.page}</Text>
                <TextInput
                    placeholder='Email Address'
                    autoCapitalize='none'
                    autoCorrect={false}
                    autoFocus={true}
                    keyboardType='email-address'
                    value={this.state.username}
                    onChangeText={(text) => this.setState({ username: text })} />
                <TextInput
                    placeholder='Password'
                    autoCapitalize='none'
                    autoCorrect={false}
                    secureTextEntry={true}
                    value={this.state.password}
                    onChangeText={(text) => this.setState({ password: text })} />
                <View style={{margin: 7}}/>
                <Button onPress={(e) => this.handleClick(e)} title={this.state.page}/>
                <View style={styles.firstView}>
                    <Text onPress={(e) => this.togglePage(e)} style={styles.buttons}>
                        {this.alt}
                    </Text>
                    <Button
                        title="Go to about"
                        onPress={() =>
                            navigate('About', { name: 'About' })
                        }
                    />
                </View>
            </ScrollView>
        );
    }
}

const styles = StyleSheet.create({
    buttons: {
        fontSize: 12,
        color: 'blue',
        flex: 1
    },

    firstView: {
        margin: 7,
        flexDirection: 'row',
        justifyContent: 'center'
    }
});

const mapStateToProps = (state, ownProps) => {
    return {
        isLoggedIn: state.auth.isLoggedIn
    };
}

const mapDispatchToProps = (dispatch) => {
    return {
        onLogin: (username, password) => { dispatch(login(username, password)); }
    }
}

export default connect(mapStateToProps, mapDispatchToProps)(Login);

您没有正确导入StackNavigator<代码>从“react navigation”导入{StackNavigator}

谢谢,我做到了。但是现在我在我的模拟器中得到一个不同的错误,它说
undefined不是一个对象(评估'this.props.navigation.navigate)
。请发布有关错误的详细信息,例如它发生在哪里。在
Login.js
中,它说
const{navigate}=this.props.navigation未定义。看起来您没有正确设置带有react navigation的登录组件。您能用您拥有的最新代码更新代码段吗?您是否也在应用程序组件中呈现登录组件?导航器将为您呈现组件,并用导航道具包装它们以控制导航。什么是
道具。导航
以及您如何传递它?您忘记设置
initialRouteName
请不要破坏您的帖子。