【问题标题】:importing a screen from the same JS file从同一个 JS 文件导入屏幕
【发布时间】:2017-05-23 17:39:11
【问题描述】:

我已在 StackOverflow 中咨询过有关此问题的所有问题,但均未成功。我不断收到以下错误 `Route 'Home' 应该声明一个屏幕。

这个错误似乎表明如果我想将一个组件用作屏幕,我必须将它导入到我的工作文件中,是这样吗?如果是这样,为什么?这可能是我的代码有问题,否则,我不确定这里有什么问题:我想了解为什么这不起作用,我已经咨询了有关该主题的多个指南。

我的index.android.js

import './app/index.js'

我的index.js(不完整):

import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View, Button, Image, TextInput } 
from 'react-native';
import { StackNavigator } from 'react-navigation';

const RallyMobileNavigator = StackNavigator({
  Home: { screen: RallyMobile },
  LogIn: { screen: LogIn }
  },{
  initialRouteName: 'Home'
});

class RallyMobile extends Component {
  static navigationOptions = {
  title: 'Welcome',
  };
  state = {
  initialPosition: {},
  lastPosition: {},
  userData: [],
  }

render() {
  return (
    <View style={styles.container}>
      <View style={styles.header}>
        <Button
          onPress={() => navigate('LogIn')}
          title='Sign In'
        />
      </View>
      <View style={styles.EventListContainer}>
        <EventList
          location={this.state.lastPosition.location ?
          this.state.lastPosition.location : 
          this.state.initialPosition.location}
        />
      </View>
    </View>
    );
  };
};

class LogIn extends Component {
  state = {
    userName: '',
    password: ''
  }
   static navigationOptions = {
     title: 'Sign In',
   };
   logInUser = () => {
     console.log("test");
   }

   render() {
     return(
       <View>
       <View style={{padding: 10}}>
         <TextInput
           style={{height: 40}}
           placeholder="Username"
           onChangeText={(text) => this.setState({userName})}
         />
         <TextInput
            style={{height: 40}}
            placeholder="Password"
            onChangeText={(text) => this.setState({password})}
         />
        </View>
        <View>
          <button
            onPress={logInUser()}
            title='Sign In'
          />
        </View>
     </View>
     )
   }
 }

AppRegistry.registerComponent('RallyMobile', () => RallyMobileNavigator);

【问题讨论】:

  • 更新:我在Chrome调试器中遍历stackNavigator.js,发现routeConfigMap.Home的screen属性未定义,但我还是不知道为什么。

标签: react-native-android react-navigation


【解决方案1】:

移动

const RallyMobileNavigator = StackNavigator({
  Home: { screen: RallyMobile },
  LogIn: { screen: LogIn }
  },{
  initialRouteName: 'Home'
});

到正上方

AppRegistry.registerComponent('RallyMobile', () => RallyMobileNavigator);

信用: https://github.com/react-community/react-navigation/issues/571#issuecomment-284207331

【讨论】:

  • 非常感谢,解决了问题,是否解决了问题,因为必须先定义组件才能被导航器使用?
  • 我真的没有办法解决这个问题,也许我没有足够的知名度?我看到的唯一方法是回答我自己的问题,但这不会给你应有的信任。
猜你喜欢
  • 1970-01-01
  • 2014-12-07
  • 2010-11-09
  • 2021-10-25
  • 2020-04-28
  • 2022-06-22
  • 1970-01-01
  • 2018-06-21
  • 1970-01-01
相关资源
最近更新 更多