Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Navigation Component Route Issue

Tags:

New react native user here. I'm running into an issue and I am not sure how to proceed. I was able to get react-navigation running properly and then began receiving an error: "The component for route must be a a React Component" but unless I'm missing something, I believe that the component I am referencing is a react component. See my index.android.js below and my Home.js below:

//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

Any help/react pointers much appreciated. Thank you!

like image 303
saupton Avatar asked Jun 25 '17 19:06

saupton


People also ask

Why react navigation is not working?

This might happen if you have an old version of the metro-react-native-babel-preset package. Try upgrading it to the latest version. If you have @babel/core installed, also upgrade it to latest version.

Can you use routes in react-native?

React Navigation is a popular library for routing and navigation in a React Native application. This library helps solve the problem of navigating between multiple screens and sharing data between them.


1 Answers

I think the problem is with home.js since you aren't exporting it. Try this :

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

Add those or just add

 export default Home; 

at the end of the home.js file

like image 94
A-J-A Avatar answered Sep 21 '22 08:09

A-J-A