Invariant Violation: The navigation prop is missing - react-native

I have two flows in my app in the main flow.. one is stack navigation and the other is drawer navigation. Stack navigation is working okay but drawer one is giving me this error -
Invariant Violation: The navigation prop is missing for this navigator. In react-navigation version 3 you must set up your app container directly. More info:
https://reactnavigation.org/docs/en/app-containers.html
App.js
import { createAppContainer,
createStackNavigator ,
createBottomTabNavigator,
createDrawerNavigator,
createSwitchNavigator
} from "react-navigation";
import LoginScreen from "./src/screens/LoginScreen";
import RegisterScreen from "./src/screens/RegisterScreen";
import StartScreen from './src/screens/StartScreen';
import { HomeDrawerNavigator } from "./src/components/HomeDrawerNavigator";
const MainNavigator = createSwitchNavigator(
{
loginFlow: {
screen: createStackNavigator(
{
StartScreen :{screen:StartScreen
},
LoginScreen : {screen : LoginScreen},
RegisterScreen : {screen : RegisterScreen}
}
)
},
homeflow: {
screen:HomeDrawerNavigator
}
}
)
const App = createAppContainer(MainNavigator);
export default App;
HomeDrawerNavigator.js
import { createDrawerNavigator } from "react-navigation";
import HomeScreen from "../screens/HomeScreen";
import PolicyScreen from "../screens/PolicyScreen";
export const HomeDrawerNavigator = createDrawerNavigator({
HomeScreen: { screen: HomeScreen },
PolicyScreen: { screen: PolicyScreen }
})
I have also added <HomeDrawerNavigator/> component in my Homescreen.js.

Related

How can I wrap my React Navigator in a redux store?

In my app.js file, I have a Navigator and I am attempting to wrap it in a redux store in order to control the state throughout my app. I am receiving this error though:
"Invariant Violation: The navigation prop is missing for this navigator. In react-navigation v3 and v4 you must set up your app container directly."
How can I properly wrap my navigator in the Redux store so I can use it?
App.js
import React from 'react';
import { registerRootComponent } from 'expo';
import { View, Text, Button } from 'react-native';
import { createAppContainer, createStackNavigator } from 'react-navigation';
import { createStackNavigator } from 'react-navigation-stack';
import { createStore, applyMiddleware } from 'redux'
import { Provider } from 'react-redux'
import thunkMiddleware from 'redux-thunk'
import reducer from './store/reducers'
// Components
import Home from './Screens/Home';
import Landing from './Screens/Landing';
import Tasks from './Screens/Tasks';
import Login from './Screens/Login';
const middleware = applyMiddleware(thunkMiddleware)
const store = createStore(reducer, middleware)
const Navigator = createStackNavigator({
Tasks: {screen: Tasks},
Landing: {screen: Landing},
Home: { screen: Home },
});
export default class App extends React.Component {
render() {
return (
<Provider store={store}>
<Navigator />
</Provider>
)
}
}
I figure out how to accomplish this. Below is the updated code with my App wrapped in a store container.
import { createStore, applyMiddleware } from 'redux'
import { Provider } from 'react-redux'
import thunkMiddleware from 'redux-thunk'
import reducer from './store/reducers'
// Components
import Home from './Screens/Home';
import Landing from './Screens/Landing';
import Tasks from './Screens/Tasks';
import Login from './Screens/Login';
const middleware = applyMiddleware(thunkMiddleware)
const store = createStore(reducer, middleware)
const Navigator = createStackNavigator({
Tasks: {screen: Tasks},
Landing: {screen: Landing},
Home: { screen: Home },
});
//ADDED THIS
const App_1 = createAppContainer(Navigator);
export default class App extends React.Component {
render() {
return (
<Provider store={store}>
<App_1 /> //UPDATED
</Provider>
)
}
}

undefined is not an object (evaluating '(0 _reactnavigation.DrawerNavigator)')

I have created customized sidebar and have 5 to 6 screens. I am using NativeBase and want to open Sidebar with DrawerNavigator option but when I applied following code it was getting issue.
Drawer Navigator code
import React, { Component } from "react";
import WelcomeScreen from './screens/WelcomeScreen';
import ContactScreen from './screens/ContactScreen';
import DepartmentScreen from './screens/DepartmentScreen';
import EmailServiceScreen from './screens/EmailServiceScreen';
import MoreScreen from './screens/MoreScreen';
import SideBar from "./SideBar.js";
import { DrawerNavigator } from "react-navigation";
const SidebarNavigator = DrawerNavigator(
{
Home: { screen: WelcomeScreen },
Contact: { screen: ContactScreen },
Department: { screen: DepartmentScreen },
EmailService: { screen: EmailServiceScreen },
More: { screen: MoreScreen }
},
{
contentComponent: props => <SideBar {...props} />
}
);
export default SidebarNavigator;
Calling them in APP.Js as propos. See following code from APP.JS
import SidebarNavigator from './src/SidebarNavigator';
class App extends Component {
render() {
const store = createStore(reducers, {}, applyMiddleware(ReduxThunk));
return (
<Provider store={createStore(reducers)}>
<Root>
<Router /> // It's a stack navigator which is working fine
<SidebarNavigator />
</Root>
</Provider>
);
}
}
console.disableYellowBox = true;
export default App;
The error here is that you are using DrawerNavigation instead of createDrawerNavigator.
Just do those changes and you should be good
import { createDrawerNavigator } from "react-navigation";
...
const SidebarNavigator = createDrawerNavigator( ... )
Source: https://reactnavigation.org/docs/en/drawer-navigator.html
You can try this code
You can use createDrawerNavigator and createAppContainer
import { createDrawerNavigator, createAppContainer } from "react-navigation";
...
const SidebarNavigator = createDrawerNavigator( ... )
const AppContainer = createAppContainer(SidebarNavigator);
// Now AppContainer is the main component for React to render
export default AppContainer;

Cannot access redux props inside react-navigation v3 tab navigator (getting error this.props.<function_name> is not a function)

I am having trouble connecting the tab navigator (from React-Navigation v3) to the redux provider.
So this is how my react-navigation and redux is configured:
app/
/react-redux provider
/app-container (this is the AppContainer of the main SwitchNavigator)
/welcome-screen
/login-screen
/register-screen
/home-navigator (this is the TabNavigator that is inside the SwitchNavigator)
Home navigator holds the screens to which user is directed to when he logs-in. In the first 3 screends (welcome, login and register) I can access the functions that are "connected" from redux to the screen through standard mapDispatchToProps.
This is the app component:
/**
* Entry point for the app
*/
import React, { Component } from "react";
// redux
import { Provider } from "react-redux";
import { createStore, applyMiddleware } from "redux";
import reduxThunk from "redux-thunk";
import { composeWithDevTools } from "redux-devtools-extension";
import main_app_reducer from "./modules/core/reducers/main_app_reducer";
// create redux store
const create_store = composeWithDevTools(applyMiddleware(reduxThunk))(createStore);
export const store = create_store(main_app_reducer);
// navigation
import AppContainer from "./modules/core/components/AppNavigator";
import { configure_axios } from "./helpers/axios_config";
configure_axios();
class App extends Component {
render() {
return (
<Provider store={store}>
<AppContainer />
</Provider>
);
}
}
export default App;
And then, from there we go to AppContainer:
/**
* this components ensures app navigation so that user can jump from one screen to another
*/
// navigation
import { createSwitchNavigator, createAppContainer } from "react-navigation";
// screens
import WelcomeScreen from "../screens/WelcomeScreen";
import LoginScreen from "../../auth/screens/LoginScreen";
import RegisterScreen from "../../auth/screens/RegisterScreen";
import HomeNavigator from "./HomeNavigator";
const AppSwitchNavigator = createSwitchNavigator({
// for users that are not logged in yet
Welcome: { screen: WelcomeScreen },
Login: { screen: LoginScreen },
Register: { screen: RegisterScreen },
// for logged in users
HomeNavigator: { screen: HomeNavigator },
});
const AppContainer = createAppContainer(AppSwitchNavigator);
export default AppContainer;
All of the code above works perfectly with no errors, the problem occurs inside the HomeNavigator component that you see below.
This is the HomeNavigator component:
import { createBottomTabNavigator } from "react-navigation";
import { HomeScreen } from "../screens/HomeScreen";
import { DashboardScreen } from "../screens/DashboardScreen";
const HomeNavigator = createBottomTabNavigator(
{
Home: { screen: HomeScreen },
Dashboard: { screen: DashboardScreen },
},
{
navigationOptions: ({ navigation }) => {
const { routeName } = navigation.state.routes[navigation.state.index];
return {
headerTitle: routeName,
};
},
},
);
export default HomeNavigator;
NONE of the screens inside the HomeNavigator are connected to Redux altough they are defined THE SAME WAY as the screens inside AppContainer file. For comparison let's see Login screen from AppContainer and Home screen from HomeNavigator:
LoginScreen:
import React, { Component } from "react";
import { View, Text } from "react-native";
import { connect } from "react-redux";
import { get_user_servers } from "../actions/core_actions";
export class LoginScreen extends Component {
componentDidMount() {
this.props.get_user_servers(); // this function works absolutely fine
}
render() {
return (
<View>
<Text> LoginScreen </Text>
</View>
);
}
}
const mapStateToProps = (state) => ({});
const mapDispatchToProps = { get_user_servers };
export default connect(
mapStateToProps,
mapDispatchToProps,
)(LoginScreen);
HomeScreen:
import React, { Component } from "react";
import { View, Text } from "react-native";
import { connect } from "react-redux";
import { get_user_servers } from "../actions/core_actions";
export class HomeScreen extends Component {
componentDidMount() {
this.props.get_user_servers();
// the same function as in LoginScreen but here the function does not work
// I'm getting an error = TypeError: this.props.get_user_servers is not a function (see picture below)
}
render() {
return (
<View>
<Text> HomeScreen </Text>
</View>
);
}
}
const mapStateToProps = (state) => ({});
const mapDispatchToProps = { get_user_servers };
export default connect(
mapStateToProps,
mapDispatchToProps,
)(HomeScreen);
That function (get_user_servers) is defined properly and it works fine in LoginScreen.
thanks everyone who took their time to help me, in the end it was my mistake that I overlooked. If you check out the third code-block from this question you can see that I import my components like this:
import { HomeScreen } from "../screens/HomeScreen";
import { DashboardScreen } from "../screens/DashboardScreen";
Which is wrong, I changed that to:
import HomeScreen from "../screens/HomeScreen";
import DashboardScreen from "../screens/DashboardScreen";
And now everything works fine, thanks again :)
Assuming it's the same function you're importing, please could you try to check your import of get_user_servers function from both screen : LoginScreen and HomeScreen. According to your code it look like LoginScreen ("../../auth/screens/LoginScreen") and HomeScreen ("../screens/HomeScreen") are not in the same path.
As asked above, could check the path of the files, or post it so we can help. Also could you open the remote debugger, and inside the componentWillMount you can put:
console.log(this.props)
to check if the HomeScreen is geting the function inside props.

The navigation prop is missing for this navigator react navigation 3.x

I am new to react native and I am using React navigation 3.x. This is
my project structure.
Mydemo
----routes
--Home.route.js
----src
--pages
--AddUser.js
----App.js
Below I am sharing my route configurations:-
Home.route.js code:-
import { createStackNavigator, createAppContainer } from "react-navigation";
import AddUser from '../src/pages/AddUser';
const HomeStack = createStackNavigator({
Add:{
screen: AddUser
}
})
export default createAppContainer(HomeStack);
App.js Code:-
import {createSwitchNavigator} from 'react-navigation';
import HomeStack from './routes/Home.route';
export default createSwitchNavigator({
Home: HomeStack
}, {
initialRouteName: 'Home',
});
But I am getting this error:-
The navigation prop is missing for this navigator.
can anyone please guide me about what I am doing wrong ?
Thank you.
createAppContainer must be on top of your navigation config =>
Home.route.js
import { createStackNavigator } from "react-navigation";
import AddUser from '../src/pages/AddUser';
export default createStackNavigator({
Add: AddUser
})
App.js
import {createSwitchNavigator, createAppContainer} from 'react-navigation';
import HomeStack from './routes/Home.route';
const AppNavigator = createSwitchNavigator({
Home: HomeStack
}, {
initialRouteName: 'Home',
});
export default createAppContainer(AppNavigator);

The Component for route must be a react component even after exporting component

I am new to React native and implementing react navigation but getting the component for route must be a react component even i exported my component
I have check all related answer but all in vain.
import {AppRegistry} from 'react-native';
import {name as appName} from './app.json';
import Launch from './Launch';
AppRegistry.registerComponent(appName, () => Launch);
Below is launch
import React from 'react';
import AppNavigator from './AppNavigator'
export default class Launch extends React.Component {
render() {
return (
<AppNavigator></AppNavigator>
);
}
}
App Navigator
//..........................
import React, { Component } from 'react';
import LoginScreen from './components/src/Login/LoginScreen'
import App from './components/src/App';
import Register from './components/src/Signup/Register';
import ViewPagerPage from './components/src/Settings/CPU/ViewPagerPage'
import TabIndicatorPage from './components/pages/TabIndicatorPage'
import TitleIndicatorPage from './components/pages/TitleIndicatorPage'
import {
createStackNavigator,
createAppContainer
} from 'react-navigation';
const RootStack = createStackNavigator({
LoginScreen: { screen: LoginScreen },
TabIndicatorPage: { screen: TabIndicatorPage },
TitleIndicatorPage: {
screen: TitleIndicatorPage, navigationOptions: {
header: null
}
},
ViewPagerPage: { screen: ViewPagerPage },
Register: {
screen: Register, navigationOptions: {
header: null
}
},
App: {
screen: App,
navigationOptions: {
header: null
}
},
ViewPagerPage: { screen: ViewPagerPage },
// Detail:{screen:Detail},
// SecondScreen:{screen:SecondScreen},
});
const AppNavigator = createAppContainer(RootStack);
export default AppNavigator;
Here it is Login Controller giving error I also used export.
import React, {Component} from 'react';
import {View,ScrollView,KeyboardAvoidingView,Animated,ImageBackground,
TouchableOpacity,TextInput, KeyboardAwareScrollView,StyleSheet,Image,Easing,Text,AsyncStorage,ToastAndroid} from 'react-native';
import App from '../App';
import bgSrc from './images/wallpaper.png';
import logoImg from './images/logo.png';
import { NavigationActions,StackActions } from 'react-navigation'
import usernameImg from './images/username.png';
import passwordImg from './images/password.png';
import spinner from './images/loading.gif';
const MARGIN = 40;
import Dimensions from 'Dimensions';
//Login Controller
export default class LoginScreen extends React.Component<{}> {
constructor(props) {
super(props);
this.state = { Login: false,showPass: true,
press: false,
isLoading: false,
navigator:this.props.navigator,
email:'',
Expected Login Screen should come
But I am getting red screen saying component must be react