Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: 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.
Check the render method of App.
This error is located at:
in App (created by withDevTools(App))
in withDevTools(App)
in RCTView (created by View)
in View (created by AppContainer)
in RCTView (created by View)
in View (created by AppContainer)
in AppContainer
in main(RootComponent)
App.js
import { View, Text } from 'react-native'
import React, { useState } from 'react';
import { NavigationContainer } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import HomeScreen from './pages/HomeScreen';
import { Font, AppLoading } from 'expo';
const Stack = createNativeStackNavigator();
const loadFonts = async () => {
return Font.loadAsync({
"open-sans": require("./assets/fonts/OpenSans-Regular.ttf"),
"open-sans-bold": require("./assets/fonts/OpenSans-Bold.ttf")
});
};
function App() {
const [fontLoaded, setFontLoaded] = useState(false);
if (!fontLoaded) {
return (
<AppLoading
startAsync={loadFonts}
onFinish={() => setFontLoaded(true)}
/>
);
}
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen}/>
</Stack.Navigator>
</NavigationContainer>
)
}
export default App;
HomeScreen
import { View, Text, SafeAreaView, StatusBar } from 'react-native'
import React from 'react';
import Header from './../components/Header';
import { Colors } from '../Values';
function HomeScreen() {
return (
<SafeAreaView style={{flex: 1, backgroundColor: Colors.bg}}>
<StatusBar backgroundColor={Colors.bg} barStyle={'dark-content'}/>
<Header/>
</SafeAreaView>
)
}
export default HomeScreen;
package.json
{
"name": "myapp",
"version": "1.0.0",
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web"
},
"dependencies": {
"#expo/vector-icons": "^13.0.0",
"#react-navigation/drawer": "^6.5.7",
"#react-navigation/native": "^6.1.2",
"#react-navigation/native-stack": "^6.9.8",
"expo": "~47.0.12",
"expo-status-bar": "~1.4.2",
"native-base": "2.13.8",
"react": "18.1.0",
"react-native-safe-area-context": "4.4.1",
"react-native-screens": "~3.18.0"
},
"devDependencies": {
"#babel/core": "^7.12.9"
},
"private": true
}
I am solving error but not solve.
Solve my question
Related
Tailwind CSS in the native wind is not being applied on Screen components.
Is there any package or dependency that is missing in my project .
This is a package.json
{
"name": "first-app",
"version": "1.0.0",
"main": "node_modules/expo/AppEntry.js",
"scripts": {
"start": "expo start",
"android": "expo start --android",
"ios": "expo start --ios",
"web": "expo start --web"
},
"dependencies": {
"#react-navigation/native": "^6.0.13",
"#react-navigation/native-stack": "^6.9.1",
"expo": "~46.0.13",
"expo-status-bar": "~1.4.0",
"nativewind": "^2.0.11",
"react": "18.0.0",
"react-native": "0.69.6",
"react-native-safe-area-context": "4.3.1",
"react-native-screens": "~3.15.0"
},
"devDependencies": {
"#babel/core": "^7.12.9",
"tailwindcss": "^3.2.0"
},
"private": true
}
Tailwind CSS is Applied on App.js but on screens components
import { StatusBar } from 'expo-status-bar';
import { NavigationContainer } from '#react-navigation/native';
import { createNativeStackNavigator } from '#react-navigation/native-stack';
import HomeScreen from './screens/HomeScreen';
export default function App() {
const Stack = createNativeStackNavigator();
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
This is Screen Component where native wind ( tailwind ) is not Applied
import { View, Text } from 'react-native'
import React from 'react'
const HomeScreen = () => {
return (
<View className="flex-1 text-center align-middle justify-center">
<Text className="bg-black text-white">App</Text>
</View>
)
}
export default HomeScreen
Firstly my tailwind.config.js Looks like this
module.exports = {
content: ["./App.{js,jsx,ts,tsx}", "./components/**/*.{js,jsx,ts,tsx}", "./screens/**/*.{js,jsx,ts,tsx}"],
theme: {
extend: {},
},
plugins: [],
}
Then I CHANGED IT WITH FOLLOWING CODE. THE MAIN CHANGE WAS IN content part. By applying the above change tailwindcss started applying both in screens and components. This worked for me
module.exports = {
content:[
"./screens/**/*.{js,jsx,ts,tsx}",
"./pages/**/*.{js,jsx,ts,tsx}",
"./components/**/*.{js,jsx,ts,tsx}",
],
theme: {
extend: {},
},
plugins: [],
};
I have been developing a sample mobile application from React native and I keep getting the following error but I cannot seems to put my finger on what's wrong there. The line does not show any errors and the details in the log do not indicate anything that I should be concern about either. But I keep getting this error.
ReferenceError: Can't find variable: useState
This error is located at:
in App (created by ExpoRoot)
in ExpoRoot (at renderApplication.js:45)
in RCTView (at View.js:34)
in View (at AppContainer.js:106)
in DevAppContainer (at AppContainer.js:121)
in RCTView (at View.js:34)
in View (at AppContainer.js:132)
in AppContainer (at renderApplication.js:39)
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
at node_modules/react-native/Libraries/Core/ExceptionsManager.js:171:19 in handleException
at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
at node_modules/expo-error-recovery/build/ErrorRecovery.fx.js:12:21 in ErrorUtils.setGlobalHandler$argument_0
at [native code]:null in flushedQueue
at [native code]:null in invokeCallbackAndReturnFlushedQueue
My App.js looks as follows
import "react-native-gesture-handler";
import { NavigationContainer } from "#react-navigation/native";
import { createStackNavigator } from "#react-navigation/stack";
import * as Font from "expo-font";
import AppLoading from "expo-app-loading";
import React, { useState } from "react";
import useFonts from "./hooks/useFonts";
import TravelPartner from "./src/components/mainPage";
const App = () => {
const [IsReady, SetIsReady] = useState(false);
const LoadFonts = async () => {
await useFonts();
};
if (!IsReady) {
return (
<AppLoading
startAsync={LoadFonts}
onFinish={() => SetIsReady(true)}
onError={() => {}}
/>
);
}
return (
<View styles={styles.container}>
{<Text>test run for the application</Text>}
</View>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});
export default App;
All the libraries have been installed and everything Is up to date. useFonts.js looks as follows
import * as Font from "expo-font";
export default useFonts = async () => {
await Font.loadAsync({
"OtomanopeeOne-Regular": require("../src/assets/fonts/OtomanopeeOne-Regular.ttf"),
"VeganStylePersonalUse-5Y58": require("../src/assets/fonts/VeganStylePersonalUse-5Y58.ttf"),
"Festive-Regular": require("../src/assets/fonts/Festive-Regular.ttf"),
"Pacifico-Regular": require("../src/assets/fonts/Pacifico-Regular.ttf"),
});
};
This is my package.json:
{
"main": "index.js",
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"web": "expo start --web",
"start": "react-native start"
},
"dependencies": {
"#react-navigation/native": "^6.0.1",
"#react-navigation/stack": "^6.0.1",
"expo": "~42.0.1",
"expo-app-loading": "^1.1.2",
"expo-font": "~9.2.1",
"expo-splash-screen": "~0.11.2",
"expo-status-bar": "~1.0.4",
"expo-updates": "~0.8.1",
"react": "16.13.1",
"react-dom": "16.13.1",
"react-native": "~0.63.4",
"react-native-elements": "^3.4.2",
"react-native-gesture-handler": "~1.10.2",
"react-native-reanimated": "~2.2.0",
"react-native-screens": "~3.4.0",
"react-native-unimodules": "~0.14.5",
"react-native-web": "~0.13.12"
},
"devDependencies": {
"#babel/core": "^7.9.0"
},
"private": true
}
The problem is in your React version. You are using "react": "16.13.1" but hooks are introduced in 16.8 version. Update your React version will solve the problem.
Please run:
npm install react#latest react-dom#latest
try to remove node_modules directory and package-lock.json and then run npm install
I have started learning react-native and I'm experiencing some issues. I don't seem to be able to use both components (createBottomTabNavigator and createStackNavigator). Export default can only be used once and I would like to render both components(at the moment only one or the other is rendering).
It would be great to get some help. thanks
Navigation.js
import { createAppContainer } from 'react-navigation'
import { createStackNavigator } from 'react-navigation-stack'
import { createBottomTabNavigator } from 'react-navigation-tabs'
import Search from '../Components/Search'
import Favourites from '../Components/Favourites'
import FilmDetail from '../Components/FilmDetail'
const SearchStackNavigator = createStackNavigator({
Search: {
screen: Search,
navigationOptions: {
title: 'Search Film'
}
},
FilmDetail: {
screen: FilmDetail,
navigationOptions: {
title: 'Film Details'
}
}
})
const MoviesTabNavigator = createBottomTabNavigator({
Search: {
screen: Search
},
Favourites: {
screen: Favourites
}
})
export default createAppContainer(MoviesTabNavigator)
I'm aware that I'm not using the latest version of React-Native
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",
"expo": "~39.0.2",
"expo-cli": "^3.28.5",
"expo-status-bar": "~1.0.2",
"moment": "^2.29.1",
"numeral": "^2.0.6",
"react": "16.13.1",
"react-dom": "^16.9.0",
"react-native": "https://github.com/expo/react-native/archive/sdk-39.0.3.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",
"react-navigation": "^4.3.9",
"react-navigation-stack": "^2.5.0",
"react-navigation-tabs": "^2.8.13",
"react-redux": "^7.2.2",
"redux": "^4.0.5"
},
"devDependencies": {
"#babel/core": "~7.9.0"
},
"private": true
}
here is my App.js
import React from 'react'
import Navigation from './Navigation/Navigation'
import { Provider } from 'react-redux'
import Store from './Store/configureStore'
export default class App extends React.Component {
render() {
return (
<Provider store={Store}>
<Navigation/>
</Provider>
);
}
}
Nesting your screen will solve your problem. I assume you want SearchStackNavigator within MoviesTabNavigator. Do something like below
const MoviesTabNavigator = createBottomTabNavigator({
Search: {
screen: SearchStackNavigator
},
Favourites: {
screen: Favourites
}
})
export default createAppContainer(MoviesTabNavigator)
To navigate between stacks and land in a specific screen use:
navigation.navigate('Stack', {screen: 'screenName')
Error seems to be that Search is not referring to your SearchStackNavigator but your "Search" component you import at the top.
Nested Stacks are possible. I'd recommend taking a look here: Master React Navigation 5
I recommend you to use react-navigation 5.x and follow this example:
SearchStackNavigator:
import {createStackNavigator} from '#react-navigation/stack';
const Stack = createStackNavigator();
const SearchStackNavigator = () =>
<Stack.Navigator>
<Stack.Screen
name="Search"
component={Search}
/>
<Stack.Screen
name="FilmDetail"
component={FilmDetail}
/>
</Stack.Navigator>
MoviesTabNavigator:
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
import SearchStackNavigator from "./SearchStackNavigator";
const Tab = createBottomTabNavigator();
const MoviesTabNavigator = () =>
<Tab.Navigator>
<Tab.Screen
name="Search"
component={SearchStackNavigator} />
<Tab.Screen
name="Other tab"
component={OTHER STACK OR SCREEN HERE} />
</Tab.Navigator>
react-navigation docs:
bottom-tab docs
stack-navigation docs
Hoping someone can help as i'm struggling to see what the issue is.
When using react-native-navigation to create a stack navigator, no matter what approach I use to export and import the component to pass into the <Stack.Screen/>, it seems to throw the below error.
React Native Issue
Stack Trace
Error: Couldn't find a 'component', 'getComponent' or 'children' prop for the screen 'Home'. This can happen if you passed 'undefined'. You likely forgot to export your component from the file it's defined in, or mixed up default import and named import when importing.
After seeing this stack trace, i done a console.log() and passed in the component just to see if undefined was being returned. This wasn't the case. I can see the HomeComp component for example be
package.json
"name": "myApp",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"#react-native-community/masked-view": "^0.1.10",
"#react-navigation/native": "^5.8.0",
"#react-navigation/stack": "^5.10.0",
"axios": "^0.21.0",
"body-parser": "^1.19.0",
"connect-flash": "^0.1.1",
"dotenv": "^8.2.0",
"express": "^4.17.1",
"express-validator": "^6.6.1",
"mongoose": "^5.10.10",
"react": "16.13.1",
"react-native": "0.63.3",
"react-native-gesture-handler": "^1.8.0",
"react-native-reanimated": "^1.13.1",
"react-native-safe-area-context": "^3.1.8",
"react-native-screens": "^2.12.0"
},
"devDependencies": {
"#babel/core": "7.12.3",
"#babel/runtime": "7.12.1",
"#react-native-community/eslint-config": "1.1.0",
"babel-jest": "25.5.1",
"eslint": "6.8.0",
"jest": "25.5.4",
"metro-react-native-babel-preset": "0.59.0",
"react-test-renderer": "16.13.1"
},
"jest": {
"preset": "react-native"
}
}
HomeComp.js
import React, { useState, useEffect } from 'react';
import { View, StyleSheet, Text, FlatList } from 'react-native';
function HomeComp() {
return (
<View>
<Text>Welcome!</Text>
</View>
)
}
const styles = StyleSheet.create({
container: {
//paddingTop: 30,
},
text: {
fontSize: 20,
margin: 10
},
})
export default HomeComp;
app.js
import React, { Component } from 'react';
import { Platform, StyleSheet, Text, View } from 'react-native';
import HomeComp from './src/components/HomeComp';
// import StatefulComponent from './src/components/api_comp';
import { createStackNavigator } from '#react-navigation/stack';
import { NavigationContainer } from '#react-navigation/native';
// To see all the requests in the chrome Dev tools in the network tab.
XMLHttpRequest = GLOBAL.originalXMLHttpRequest ?
GLOBAL.originalXMLHttpRequest :
GLOBAL.XMLHttpRequest;
// fetch logger
global._fetch = fetch;
global.fetch = function (uri, options, ...args) {
return global._fetch(uri, options, ...args).then((response) => {
console.log('Fetch', { request: { uri, options, ...args }, response });
return response;
});
};
function test(){
return(
<View>
<Text>home page</Text>
</View>
);
}
console.log({HomeComp})
export default class App extends Component {
render() {
Stack = createStackNavigator();
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen
name="Home"
Component={HomeComp}
/>
<Stack.Screen
name="test"
Component={test}
/>
</Stack.Navigator>
</NavigationContainer>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#1c8282'
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5
}
}); ```
The problem is you are using 'Component' instead of 'component'
you will have to change the prop name to simple case
<Stack.Screen
name="Home"
component={HomeComp}
/>
<Stack.Screen
name="test"
component={test}
/>
As the component prop is not being passed its undefined in the navigator, thats why you are getting the error
I've added the StackNavigator into the application via another separate AppNavigator.js file. Despite this the app still doesn't navigate but does however compile. The button is clickable but has no action.
Different versions of Expo and React-Native along with different Navigation plugins.
Here is a copy of the AppNavigator.js
import { StackNavigator } from 'react-navigation';
import Timer from './Timer';
import Main from './Main';
import Splash from './Splash';
const AppNavigator = StackNavigator({
Timer: {
screen: Timer
},
Splash: {
screen: Splash
},
Main: {
screen: Main
}
})
export default AppNavigator;
And here is my package.json
{
"name": "app",
"version": "0.1.0",
"private": true,
"devDependencies": {
"react-native-scripts": "1.13.1",
"jest-expo": "26.0.0",
"react-test-renderer": "16.3.0-alpha.1"
},
"main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
"scripts": {
"start": "react-native-scripts start",
"eject": "react-native-scripts eject",
"android": "react-native-scripts android",
"ios": "react-native-scripts ios",
"test": "node node_modules/jest/bin/jest.js"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"expo": "26.0.0",
"react": "16.3.0-alpha.1",
"lodash": "^4.17.4",
"react-native": "0.54.0",
"react-navigation": "1.2.1",
"#builderx/tab-view": "^0.1.5",
"#builderx/icons": "^0.1.5",
"#builderx/utils": "^0.1.6",
"#builderx/react-native-swiper": "^0.1.5"
}
}
Here is also a copy of the Button responsible for navigation:
import React, { Component } from "react";
import { Center } from "#builderx/utils";
import Button13 from "../symbols/button13";
import Button5 from "../symbols/button5";
import { createAppContainer } from 'react-navigation';
import { View, StyleSheet, Text, Image } from "react-native";
export default class Timer extends Component {
render() {
return (
<View style={styles.root}>
<Button13 style={styles.button13} />
<Center horizontal>
<Button5
style={styles.button5}
onPress={() => {
alert("hello");
this.props.navigation.navigate("Main");
}}
/>
</Center>
<Center horizontal>
<Image
source={require("../assets/capture.jpg")}
style={styles.image}
/>
</Center>
<View style={styles.rect}>
<Text style={styles.text}>[Meditation Name]</Text>
<Text style={styles.text2}>00:00</Text>
</View>
</View>
);
}
}
First .
Don's use StackNavigator , is deprecated, use createStackNavigator from React-navigation 3.0 instead
Second
Please show the button code, you should call something like this.props.navigation.navigate('Main')
the button doesn't have access to the navigation props and you can't have onPress to a custom component. add the code below to the button file then add it to the button like this then just call this.props.navigation.navigate("Main"); on the nested button of that component
import { withNavigation } from 'react-navigation';
export default withNavigation(Button5)