反应原生导航组件路径问题

时间:2017-06-25 19:14:46

标签: javascript android react-native

新的反应原生用户在这里。我遇到了一个问题,我不知道该怎么办。我能够正常运行react-navigation,然后开始收到一个错误:“路由的组件必须是一个React组件”,但除非我遗漏了什么,我相信我引用的组件反应成分。请参阅下面的index.android.js和我的Home.js:

//index.android.js
import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View
} from 'react-native';
import {
  TabNavigator,
  StackNavigator
} from 'react-navigation';

import Home from './app/components/Home/Home';
import Search from './app/components/Search/Search';

export default class demoApp extends Component {
  render() {
    return (
      <SimpleNavigation/>
    );
  }
}

export const SimpleNavigation = StackNavigator({
  Home: { 
    screen: Home,
    header: { visible: false },
    navigationOptions: {
      title: 'Home',
      header: null
    },
  },
  Search: { 
    screen: Search,
    navigationOptions: {
      title: 'test'
    },
  },
},{});


//Home.js
import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  Image,
  TextInput,
  Button,
  TouchableHighlight
} from 'react-native';

class Home extends Component {
    constructor(props){
        super(props);
        this.state = {zipCode: ''}
    }
    navigate = (zipCode) => {
        this.props.navigation.navigate('Search', zipCode);
    }
    render() {
        return (
            <View style={styles.container}>
                <View style={[styles.boxContainer, styles.boxOne]}>
                    <Image style={styles.logo} source {require('../../images/Logo.png')} />
                    <Text style={styles.title}>An application to do things</Text>
                    <TextInput 
                        style={styles.textInput} 
                        placeholder='Enter a Zip Code' 
                        onChangeText={(zipCode) => this.setState({zipCode})}
                        >
                    </TextInput>
                </View>
                <View style={[styles.boxContainer, styles.boxTwo]}>
                    <TouchableHighlight onPress={() => this.navigate(this.state.zipCode)}>
                        <Text style={styles.searchBox}>
                            Search
                        </Text>
                    </TouchableHighlight>
                </View>
            </View>
        );
    }
}

enter image description here

任何帮助/反应指针都非常感激。谢谢!

4 个答案:

答案 0 :(得分:30)

我认为问题在于 home.js ,因为您没有导出它。试试这个:

export default class Home extends Component { ... } 
^^^^^^^^^^^^^^

添加或只添加

 export default Home; 

home.js文件的末尾

答案 1 :(得分:4)

对于其他任何人,您可能会收到“用于路由的组件必须是React组件”错误,因为您没有默认导出,对我来说就是这种情况。

export HomeScreen extends React.Component {
 ...

vs

export default HomeScreen extends React.Component {
 ...

希望这对某人有帮助!

答案 2 :(得分:0)

  

const MyNavigator = createStackNavigator({

RouteNameOne: {


  screen:() => <HomeScreen/>



},


RouteNameTwo: {


  screen:() => <NewScreen/>


},   },   {   initialRouteName: 'RouteNameOne',   } );

它将起作用

答案 3 :(得分:0)

就我而言,将此代码块放入home.js中即可解决问题

 static navigationOptions = {
    navigationOptions: {
      title: "scren title",
    }
  };