【问题标题】:Stack navigator giving me undefined error堆栈导航器给我未定义的错误
【发布时间】:2017-12-31 15:37:42
【问题描述】:

顺便说一句,我正在使用https://facebook.github.io/react-native/docs/navigation.html

我正在尝试使用StackNavigatorLogin.js 转到AboutDendro.js。我的<Button/> 组件有什么问题在我的iOS 模拟器中抛出该错误?

这里是Login.js

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 StackNavigator from 'react-navigation';
import AboutDendro from './AboutDendro';

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'; }

    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();
    }

    static navigationOptions = {
        title: 'AboutDendro',
    };

    render() {
        const { navigate } = this.props.navigation;
        const App = StackNavigator({
            Home: { screen: Login },
            Profile: { screen: AboutDendro },
        });

        return (
            <ScrollView style={{padding: 20}}>
                <Button
                    title="Go to Jane's profile"
                    onPress={() =>
                        navigate('AboutDendro', { name: 'AboutDendro' })
                    }
                />
                <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>
                </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);

【问题讨论】:

    标签: javascript ios reactjs react-native


    【解决方案1】:

    这是因为navigation 不在您的道具中。它是您创建的App 组件的一部分。但是你对这个组件什么都不做。

    您应该有一个App.js 文件,在您的 stackNavigator 中,将您的 Login 组件设置为您的 stackNavigator 参数中的默认组件。

    看看this documentation

    【讨论】:

      【解决方案2】:

      我尝试重构您的代码。 在组件render 也许你可以写:

      render() {
          const { navigate } = this.props.navigation;
          return (
              <ScrollView style={{padding: 20}}>
                  <Button
                      title="Go to Jane's profile"
                      onPress={() =>
                          navigate('Profile', { name: 'AboutDendro' })
                      }
                  />
                  <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>
                  </View>
              </ScrollView>
          );
        }
      }
      

      然后你将组件 StackNavigation 分隔在组件 render() 下方,例如:

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

      然后在您的index.ios.js导入组件应用程序,例如:

      import './Login';

      就是这样。或许我的回答对你有帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-01-28
        • 2011-05-16
        • 1970-01-01
        • 2016-05-29
        • 1970-01-01
        相关资源
        最近更新 更多