Invariant Violation: "main" has not been registered While running react-native app - react-native

This is my folder structure. I have started this app with expo. Somehow i am not able to run it because of the following error.
Rootstack.js
import React from 'react';
import { createSwitchNavigator } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import LoginComponent from "../component/login/login";
import HomeComponent from "../component/home/home"
const AuthNav = createStackNavigator({
Login:{
screen : LoginComponent,
navigationOptions:{
headerShown: false
}
},
});
const AppNav = createStackNavigator({
Home:{
screen : HomeComponent,
tabBarLabel: 'Home',
navigationOptions:{
headerShown: false
}
},
});
const switchNav = createSwitchNavigator({
AuthNav,
AppNav
},{
initialRouteName:"AuthNav"
});
export default switchNav;
App.js inside stack folder
import { createAppContainer } from 'react-navigation'
import App from './stack/RootStack'
import { AppRegistry } from 'react-native';
AppRegistry.registerComponent('main',() => App);
export default createAppContainer(App);
main app.js
import App from './src/App.js';
export default App;
Error
Invariant Violation: Tried to register two views with the same name RNCSafeAreaProvider
Error 2
Invariant Violation: "main" has not been registered. This can happen if:
* Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project.
* A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called.
Which part i am doing wrong here .

The meaningful exception that you are seeing here is:
Invariant Violation: Tried to register two views with the same name RNCSafeAreaProvider
The "main" has not been registered is a red herring, it only happened because the other error.
The RNCSafeAreaProvider exception is happening because you have multiple copies of react-native-safe-area-context in your app. You may be using a library that includes it as a dependency in addition to having it installed in your app. If you use yarn, you can run yarn why react-native-safe-area-context to see where it's coming from. Try running expo install react-native-safe-area-context and if that doesn't help you can use yarn resolutions to override the version used by the package that depends on it.

The issue is most likely linked to registering your App Multiple times.
Keep all your navigation logic in RootStack.js and avoid using createAppContainer() in your app.js
AppRegistry.registerComponent('main',() => App) should be the final statement after configuring your navigation.
Importing App.js into main app.js means you are registering the main component twice(you should only have one output file). React uses index.js as the default output.
Use this instead App.js inside stack folder:
import { createAppContainer } from 'react-navigation'
import App from './stack/RootStack'
//Remove these 2 lines below
import { AppRegistry } from 'react-native';
AppRegistry.registerComponent('main',() => App);
export default createAppContainer(App);
In your main app.js
import App from './src/App.js';
// Use it here
import { AppRegistry } from 'react-native';
AppRegistry.registerComponent('main',() => App);
export default App;

Related

How to Fix Expo Snackbar error for React Stack Navigator

I have just started using react native and expo. I keep getting the error that is:
(0 , _reactNavigationStack.createNativeStackNavigator) is not a function
Evaluating routes/homeStack.js
Evaluating App.js
Loading App.js
TypeError: (0 , _reactNavigationStack.createNativeStackNavigator) is not a function
at Object.eval (routes/homeStack.js.js:16:46
at eval (routes/homeStack.js.js
at eval (routes/homeStack.js.js
This is my code:
import { createAppCointainer } from '#react-navigation/native';
import {createNativeStackNavigator } from 'react-navigation-stack';
import Home from '../screens/homeScreen';
import Learn from '../screens/learn';
const screens = {
Home: {
screen: Home,
},
Learn: {
screen: Learn,
},
};
const HomeStack = createNativeStackNavigator(screens);
export default createAppCointainer(HomeStack);
I tried changing createAppCointainer to navigation container but that doesn't seem to work either so now I do not know what to do
import { NavigationContainer } from '#react-navigation/native';

index.js configuration with react-native?

I'm currently trying to implement react-navigation into my RN app. RN documentation seems to say that index.js is a required file for RN projects but the first react-navigation example shows all of the initial code in App.js with no index.js file:
https://snack.expo.io/#react-navigation/auth-flow-v3
Is the index.js file only required for standalone RN projects which are not built on Expo? Maybe Expo handles this automatically behind the scenes? The index.js file appears to be required for my standalone RN app and the render() implementation appears to be required in index.js as well. The problem appears to be that the render() implementation in my index.js appears to override whatever is in App.js. Ie I'm expecting initialRouteName: 'AuthLoading' in the App.js configuration to redirect to my AuthLoading component but App.js logic appears to get superceded by the index.js render() implementation.
So what is the proper way to get my index.js and App.js to support react-navigation integration? I'm open to ditching index.js for an alternative implementation/configuration, I just have it in my project because my understanding is that it's required.
I don't really get your confusion but still, this is how it would look.
First, your index.js would be completely normal. It has to look like this or similar:
//index.js
import { AppRegistry } from 'react-native'
import App from './App'
import { name as appName } from './app.json'
AppRegistry.registerComponent(appName, () => App)
This is just normal, right? Then, let's see how it would be in App.jsx
//App.jsx
import React, { Component } from 'react'
import AppContainer from './fromSomeRoute'//replace by your own
export default class App extends Component {
render() {
return (
<AppContainer />
)
}
}
And what is exactly AppContainer? just the component you exported from the file you are using for react-navigation. In here, there is no need to create a class or something like that, you can just create it like this
//fromSomeRoute.jsx
import React from 'react'
import { createStackNavigator, createSwitchNavigator, createAppContainer } from 'react-navigation'
import YourScreen from './YourPathToYourScreen'
const YourScreenStack = createStackNavigator({
YourScreen: {
screen: YourScreen,
navigationOptions: () => ({ //Look documentation for further info
header: null,
}),
},
})
const AppContainer = createAppContainer(
createSwitchNavigator(
{
Screen: YourScreenStack,
//Create more Stacks and add them in here
},
{
initialRouteName: 'Screen',
},
),
)
export default AppContainer

Reference Error: can't find variable in React Native

I'm getting this error when i run the app on android
My AppNavigation code as follow
import React, {Component} from 'react'
import { createStackNavigator } from 'react-navigation-stack'
import Home from '../pages/Home'
import DetailsAdd from '../pages/DetailsAdd'
import ImagesAdd from '../pages/ImagesAdd'
const AppNavigation = createStackNavigator(
{
DetailsAdd: { screen: DetailsAdd },
ImagesAdd: { screen: ImagesAdd },
Home: { screen: Home },
},
{
initialRouteName: 'Home'
}
)
export default AppNavigation
class App extends React.Component {
render() {
return <AppNavigator/>;
}
}
ImagesAdd Class path is correct, How can I fix this issue?
Have you got any app container? if you not, you should check this link.
important note from React Navigation Docs
createStackNavigator is a function that returns a React component. It takes a route configuration object and, optionally, an options object (we omit this below, for now). createAppContainer is a function that returns a React component to take as a parameter the React component created by the createStackNavigator, and can be directly exported from App.js to be used as our App's root component.

navigation prop is undefined (React Navigation)

I am simply trying to learn React Navigation. I installed packages below;
expo install react-navigation react-native-gesture-handler react-native-reanimated react-native-screen
Then I am trying to put a button and navigate with it but
onPress={() => this.props.navigation.navigate('Example')}
My project does not recognize "navigation" after "props" ? I installed all packages still same
app.js file:
import { createAppContainer } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import Chat from './screens/Chat.js';
import Home from './screens/Home.js'
const RootStack = createStackNavigator({
Home: Home,
Chat: Chat
});
const AppContainer = createAppContainer(RootStack);
export default AppContainer;
Thanks for your help

Stack Navigator Works On Android But iOS Broken

Our React Native Project, uses react-navigation Stack navigator. It perfectly running in Android but in ios it gives an error which "Raw Text must be wrapped in an explicit " When we try to navigate between Page.
In ios only first page loading. For example, Default 'Main Page' is the first page in order so android and ios emulator both open the first page. Then if we try to navigate between them in android navigaton works but in ios we get "Raw text Error".
And if we change firs page of stack navigator ios also loaded first page correctly but not navigate between them as expected.
We think navigated pages may be problem, so we create a blank page and navigate to there, but issue still continue in iOS.
I am really glad to hear suggestions.
This is how we navigate between pages:
this.props.navigation.navigate('ProfileSayfa');
This is our index.android.js and also index.android.ios:
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
import {StackNavigator,DrawerNavigator } from "react-navigation";
import LoginPage from "./src/pages/login-page/login-page";
import MainPage from "./src/pages/main-page/main-page";
import MekanAraSonuc from "./src/pages/MekanAra/MekanAraSonuc";
import MekanAra from "./src/pages/main-page/tabs/mekan_ara";
import NewsTab from "./src/pages/main-page/tabs/news-tab";
import StartPage from "./src/pages/start-page/start-page";
import AllMekan from "./src/pages/AllMekan/AllMekan";
import MekanSayfa from "./src/pages/MekanSayfa/MekanSayfa";
import RezervasyonSayfa from "./src/pages/MekanSayfa/RezervasyonSayfa";
import FirsatSayfa from "./src/pages/MekanSayfa/FirsatSayfa";
import EtkinlikSayfa from "./src/pages/MekanSayfa/EtkinlikSayfa";
import ProfileSayfa from "./src/pages/MekanSayfa/ProfileSayfa";
import EtkinlikSonuc from "./src/pages/AllMekan/EtkinlikSonuc";
import FirsatlarSonuc from "./src/pages/AllMekan/FirsatSonuc";
import IconME from 'react-native-vector-icons/FontAwesome';
import Drawer from "./src/pages/MyComponents/Drawer";
import SearchForm from "./src/pages/MyComponents/SearchForm";
import RezervasyonForm from "./src/pages/Forms/RezervasyonForm";
import FirsatForm from "./src/pages/Forms/FirsatForm";
import CalendarListView from "./src/pages/Forms/CalendarListView";
export default class moreAwesome extends Component {
render() {
return (
<LoginApp
ref={nav => {
this.navigator = nav;
}}
/>
);
}
}
const LoginApp = StackNavigator({
MainPage: {
screen: MainPage,
},
LoginPage: { screen: LoginPage },
MekanAraSonuc:{screen:MekanAraSonuc},
MekanAra:{screen:MekanAra},
NewsTab:{screen:NewsTab},
AllMekan:{screen:AllMekan},
MekanSayfa:{screen:MekanSayfa},
FirsatSayfa:{screen:FirsatSayfa},
EtkinlikSayfa:{screen:EtkinlikSayfa},
EtkinlikSonuc:{screen:EtkinlikSonuc},
FirsatlarSonuc:{screen:FirsatlarSonuc},
ProfileSayfa:{screen:ProfileSayfa},
RezervasyonSayfa:{screen:RezervasyonSayfa},
SearchForm:{screen:SearchForm},
RezervasyonForm:{screen:RezervasyonForm},
FirsatForm:{screen:FirsatForm},
CalendarListView:{screen:CalendarListView},
});
AppRegistry.registerComponent('myproje', () => moreAwesome);
Apperantly, when you create static Navigation Options for an page, "title" can be empty string in Android but not on iOS.
When we change this,
static navigationOptions = ({ navigation }) => {
return {
title: '',
};
};
To this:
When we change this,
static navigationOptions = ({ navigation }) => {
return {
title: ' ',
};
};
And Also be careful intial page gives this error not target page!