2
votes

I have just started learning how to create apps using React Native. After learning about the Stack Navigator, I attempted to try it out but I am getting this error. Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s%s, undefined, You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports.

App.js

import React from 'react';
import { Image, StyleSheet, Text, TouchableOpacity, View } from 'react-native';
import { NavigationNativeContainer } from '@react-navigation/native';
import {createStackNavigator}  from '@react-navigation/stack';


const Stack = createStackNavigator();
export default function App() {
    return( 
    <NavigationNativeContainer>
      <Stack.Navigator>
      <Stack.Screen name="Home" component={HomeScreen}/>
       </Stack.Navigator>
      </NavigationNativeContainer>
    );
}
const HomeScreen=(props)=> {
  return (
    <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
      <Text>Home Screen</Text>
    </View>
  );
}

Package.json

{
  "main": "node_modules/expo/AppEntry.js",
  "scripts": {
    "start": "expo start",
    "android": "expo start --android",
    "ios": "expo start --ios",
    "web": "expo start --web",
    "eject": "expo eject"
  },
  "dependencies": {
    "@react-native-community/masked-view": "^0.1.10",
    "@react-navigation/core": "^5.7.0",
    "@react-navigation/native": "^5.4.0",
    "@react-navigation/stack": "^5.3.7",
    "expo": "~37.0.3",
    "expo-font": "~8.1.0",
    "native-base": "^2.13.12",
    "react": "~16.9.0",
    "react-dom": "~16.9.0",
    "react-native": "https://github.com/expo/react-native/archive/sdk-37.0.1.tar.gz",
    "react-native-gesture-handler": "^1.6.1",
    "react-native-reanimated": "^1.8.0",
    "react-native-safe-area-context": "^1.0.0",
    "react-native-screens": "^2.7.0",
    "react-native-web": "~0.11.7"
  },
  "devDependencies": {
    "@babel/core": "^7.8.6",
    "babel-preset-expo": "~8.1.0"
  },
  "private": true
}

3

3 Answers

1
votes

change

import { NavigationNativeContainer } from '@react-navigation/native';

to

import { NavigationContainer } from '@react-navigation/native';


  <NavigationContainer>
  <Stack.Navigator>
  <Stack.Screen name="Home" component={HomeScreen}/>
   </Stack.Navigator>
  </NavigationContainer>

When react-navigation was in beta, it was NavigationNativeContainer,now it is NavigationContainer

Hope this helps!

1
votes

I have an alternative solution that worked for me. In Stack.Navigator, I set the headerMode prop to "none".

<HabitStack.Navigator
  headerMode="none"
  initialRouteName="HabitManagerScreen"
>
  <HabitStack.Screen
    name="HabitManagerScreen"
    component={HabitManagerScreen}
  />
  <HabitStack.Screen name="HabitStatsScreen" component={HabitStatsScreen} />
</HabitStack.Navigator>
0
votes

This error can happen when you use native-base Image component:

import {Image} from 'native-base';

<Image source={{uri: 'https://i.pravatar.cc/300?u=1'}} />

Use react-native Image component instead:

import {Image} from 'react-native';

<Image source={{uri: 'https://i.pravatar.cc/300?u=1'}} />