React Native "TypeError: Object(...) is not a function" Error for react navigation stack

Viewed 5746

I'm having an issue with my react-navigation-stack, I believe it could be a problem with dependencies, but I'm not sure if that's it. I am simply trying to have some text redirect to another page. If there is code that is irrelevant to the issue, such as a button, I apologize as I am trying to learn react native. The problem is being pointed at homeStack.js at the first import, "import { createStackNavigator } from '@react-navigation/stack';" , but previously I just used react-navigation-stack there, which I believe was a part of old dependencies, but it gave me a module not found error at first, which changed to what I have now when I put @react-navigation/stack instead. I was learning from a video tutorial, but the code from the tutorial was not compiling. I redownloaded react navigation multiple times and have tried some thing that did not work. I will post my code below and would really appreciate help and input as to what can help my problem. Thanks you! Picture of error

homeStack.js

import { createStackNavigator } from '@react-navigation/stack';
//import { createAppContainer } from '@react-navigation/native';
//import { createStackNavigator } from 'react-navigation-stack';
import { createAppContainer } from 'react-navigation';
import Home from '../screens/home';
import ReviewDetails from '../screens/reviewDetails';

const screens = {
Home: {
screen: Home,
},
ReviewDetails: {
screen: ReviewDetails,
},
};

// home stack navigator screens
const HomeStack = createStackNavigator(screens);

export default createAppContainer(HomeStack);

home.js

import React from 'react';
import { StyleSheet, View, Text, } from 'react-native';
import { globalStyles } from '../styles/global';

export default function Home() {
  return (
    <View style={globalStyles.container}>
       <Text style={globalStyles.titleText}>Home Screen</Text>
    </View>
  );
}

App.js

//import React from 'react';
import React, { Component } from 'react';
import { StyleSheet, Text, View, Image, Button } from 'react-native';
import Buttonwithbackground from './src/Buttonwithbackground';
import { StackNavigator } from 'react-navigation';
//import Expo from 'expo';
//import Screen from 'screen2';
import { AppLoading } from 'expo';
import Navigator from './routes/homeStack';
import { render } from 'react-dom';
//
//import {Link} from 'react-router-dom';

class HomeScreen extends React.Component{
  static NavigationOptions = {
    title: 'Home',
  };
  render(){
    const { navigate} = this.props.navigation;
    return(
      <View style={styles.container}>
        <Text
         onPress= { ()=> navigate('Home') }>Navigate to Profile
         </Text>
      </View>
    );
    }
  }

class ProfileScreen extends React.Component{
  static NavigationOptions = {
    title: 'Profile',
  };
  render(){
    const { navigate} = this.props.navigation;
    return(
      <View style={styles.container}>
        <Text
         onPress= { ()=> navigate('Profile') }>Navigate to Profile
         </Text>
      </View>
    );
    }
  }


//export default function App() {
export default class App extends Component {

  editUser = () => {
    this.props.navigation.navigate("Screen");
  //    this.navigation.navigate("screen2");
  //     window.location.href = 'screen2';
        };

   editUser2 = () => {
  //if the second button is clicked, it will redirect to yahoo.com
        window.location.href="http://yahoo.com"
        };     

  render(){
  return (
    <View style={styles.container}>
      <Image
        style={{width: 350, height: 200}}
        source = {require('./assets/dolanduckjoker.jpg')}
      />
      <Navigator />
      <Buttonwithbackground text='Login' color='black' onPress={this.editUser2}/>
      <p><br/></p>
      <Button title='Login' color='black' onPress={this.editUser2}/>
      <Button title='Login' color='black' onPress={() => navigation.navigate('screen2.js')}/>
      <p><br/></p>
      <Button
                    style={styles.cancelButton} 
                    onPress={this.editUser}
                    title="Register"
                    color="#343434"
                    accessibilityLabel="Register a User."/>
    </View>
  );
}
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: 'white',
    alignItems: 'center',
    justifyContent: 'center',
  },
  title: {
    fontSize: 32,
    textAlign: 'center',
    margin: 10,
  },
});
1 Answers

The error says what's wrong -

Object(...) is not a function

createStackNavigator expects a function and you are passing an object to it as a parameter. According to docs

Your code should look like -

import { createStackNavigator } from '@react-navigation/stack';
//import { createAppContainer } from '@react-navigation/native';
//import { createStackNavigator } from 'react-navigation-stack';
import { createAppContainer } from 'react-navigation';
import Home from '../screens/home';
import ReviewDetails from '../screens/reviewDetails';

const Stack = createStackNavigator();

function MyStack() {
  return (
    <Stack.Navigator>
      <Stack.Screen name="Home" component={Home} />
      <Stack.Screen name="ReviewDetails" component={ReviewDetails} />
    </Stack.Navigator>
  );
}

export default MyStack; //you need to render this

Now in your root file, It should be something like this (excluding your additional code) -

import { createStackNavigator } from '@react-navigation/stack';
import MyStack from "yourPath";

export default function App() {
  return (
    <NavigationContainer>
      <MyStack />
    </NavigationContainer>
  );
}
Related