react navigation issue with versions - react-native

I have a problem when I try to add a bottomnavigation in my app on the main screen:
This is the main screen code:
// Main.js
import React from 'react';
import { View, Text, StyleSheet, Button } from 'react-native';
import firebase from 'react-native-firebase';
import { NavigationContainer } from '#react-navigation/native';
import { createBottomTabNavigator } from '#react-navigation/bottom-tabs';
export default class Main extends React.Component {
state = { currentUser: null }
componentDidMount() {
const { currentUser } = firebase.auth()
this.setState({ currentUser })
}
render() {
const { currentUser } = this.state
return (
<View style={styles.container}>
<Text>
Hi {currentUser && currentUser.email}!
</Text>
</View>
)
}
}
const bottomTabNavigator = createBottomTabNavigator(
{
Main: Main,
},
{
initialRouteName: 'Main'
}
);
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
}
})
No when I run it I get this error:
Error: Creating a navigator doesn't take an argument. Maybe you are trying to use React Navigation 4 API with React Navigation 5? See https://reactnavigation.org/docs/en/hello-react-navigation.html for usage guide.
<unknown>
index.bundle?platform=ios&dev=true&minify=false:109707:24
<global>
Main.js:24
loadModuleImplementation
require.js:322:6
guardedLoadModule
require.js:201:45
runUpdatedModule
require.js:657:17
metroHotUpdateModule
require.js:533:8
define
require.js:53:24
global code
Main.bundle?platform=ios&dev=true&minify=false&modulesOnly=true&runModule=false&shallow=true:1:4
<unknown>
[native code]:0
inject
injectUpdate.js:62:35
forEach
[native code]:0
injectUpdate
injectUpdate.js:71:26
on$argument_1
HMRClient.js:40:21
emit
index.js:202:37
_ws.onmessage
WebSocketHMRClient.js:72:20
EventTarget.prototype.dispatchEvent
event-target-shim.js:818:39
_eventEmitter.addListener$argument_1
WebSocket.js:232:27
emit
EventEmitter.js:190:12
callFunctionReturnFlushedQueue
[native code]:0
So i checked my package file which is:
{
"name": "kowop",
"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.6",
"#react-navigation/bottom-tabs": "^5.0.3",
"#react-navigation/material-bottom-tabs": "^5.0.1",
"#react-navigation/native": "^5.0.1",
"email-validator": "^2.0.4",
"react": "16.9.0",
"react-native": "0.61.5",
"react-native-firebase": "^5.6.0",
"react-native-gesture-handler": "^1.5.6",
"react-native-paper": "^3.6.0",
"react-native-reanimated": "^1.7.0",
"react-native-safe-area-context": "^0.6.4",
"react-native-screens": "^2.0.0-beta.2",
"react-native-vector-icons": "^6.6.0",
"react-navigation": "^4.1.1",
"react-navigation-stack": "^2.0.16",
"typescript": "^3.7.5"
},
"devDependencies": {
"#babel/core": "7.8.3",
"#babel/runtime": "7.8.3",
"#react-native-community/eslint-config": "0.0.5",
"babel-jest": "24.9.0",
"eslint": "^6.8.0",
"jest": "24.9.0",
"metro-react-native-babel-preset": "0.56.4",
"react-test-renderer": "16.9.0"
},
"jest": {
"preset": "react-native"
}
}
But apparently react-navigation can't be updated above 4.11 or at least that is what the npm page says.
244,663
Version
4.1.1
License
MIT
Unpacked Size
41 kB
Total Files
12
Issues
133
Pull Requests
3
So I am a bit lost here. Does anyone see what I do wrong?
Thanks a lot!
Tim

You are using react-navigation v5 dependencies but in your code you implement with v4 api way.
You should change react-navigation dependencies to v4 to make your code work.
I made you code work on snack : demo

Related

How to use container in react native?

I am very new to React native I am trying to show content it is showing from starting. But I have
To show the content from the container, but I don't know how to apply this in React native. So
So someone please help me how to use container in React native.
In this component I tried some code please check it once
This is App.js
import React from 'react';
import {View, Text} from 'react-native';
const App = props => {
return (
<View>
<Text>Hi Mark</Text>
</View>
);
};
export default App;
These are the packages I installed in my React native project
{
"name": "testone",
"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.3.2",
"#react-navigation/stack": "^5.3.6",
"react": "16.11.0",
"react-native": "0.62.2",
"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"
},
"devDependencies": {
"#babel/core": "7.9.6",
"#babel/runtime": "7.9.6",
"#react-native-community/eslint-config": "0.0.5",
"babel-jest": "24.9.0",
"eslint": "6.8.0",
"jest": "24.9.0",
"metro-react-native-babel-preset": "0.58.0",
"react-test-renderer": "16.11.0"
},
"jest": {
"preset": "react-native"
}
}
Note I am not using expo I am using pure React native for app development.
Add margin style property to root View to make it like container
import React from "react";
import { Text, View } from "react-native";
const App = () => {
return (
<View style={{ margin:100 }}>
<Text>Hi There!</Text>
</View>
);
};
export default App;

React Native: Invalid hook call

I am trying to make android application with react native.After creating project and install all dependencies i want to use use Hook inside navigator like this:
const MainNavigator = createStackNavigator({
Dashboard: DashboardScreen,
ListSurveyor: {
screen: topSurvayorsNavigator,
navigationOptions: ({ navigation }) => {
const dispatch = useDispatch();
const [term, setTerm] = useState('');
const searchServeyor = (item) => {
console.log('nav: ', item);
dispatch(survayouActions.searchSurveyouList(item))
}
switch (navigation.state.routes[navigation.state.index]["routeName"]) {
case "ActiveSurveyor":
return {
header: () => <CustomHeader
title='Active'
// term={term}
// onTermChange={setTerm}
onTermSubmit={searchServeyor}
/>
}
case "DeActiveSurveyor":
return {
header: () => <CustomHeader
title='DisActive'
onTermSubmit={searchServeyor}
/>
}
default:
return { title: (navigation.state.routes[navigation.state.index]["routes"])[(navigation.state.routes[navigation.state.index]["index"])].routeName }
}
}
},
Audited: AuditedFileNumSurveyor,
}, {
defaultNavigationOptions: defaultNavOptions
});
I imported useState and useDispatch like below:
import React, { useState } from 'react';
import { useDispatch } from "react-redux";
But When i run application i got this error:
Invalid hook call. Hooks can only be called inside of the body of a function component. This could happen for one of the following reasons: 1. You might have mismatching versions of React and the renderer (such as React DOM) 2. You might be breaking the Rules of Hooks 3. You might have more than one copy of React in the same app See for tips about how to debug and fix this problem.
I used hook inside method navigationOptions: ({ navigation }) => { but what is happen?
This is package.json information:
{
"name": "RNMngm",
"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": {
"base-64": "^0.1.0",
"prop-types": "^15.7.2",
"react": "16.9.0",
"react-native": "0.61.5",
"react-native-charts-wrapper": "^0.5.7",
"react-native-gesture-handler": "^1.5.2",
"react-native-paper": "^3.4.0",
"react-native-reanimated": "^1.4.0",
"react-native-svg": "^10.0.0",
"react-native-vector-icons": "^6.6.0",
"react-navigation": "^4.0.10",
"react-navigation-drawer": "^2.3.3",
"react-navigation-header-buttons": "^3.0.4",
"react-navigation-material-bottom-tabs": "^2.1.5",
"react-navigation-stack": "^1.10.3",
"react-navigation-tabs": "^2.7.0",
"react-redux": "^7.1.3",
"redux": "^4.0.5",
"redux-thunk": "^2.3.0"
},
"devDependencies": {
"#babel/core": "7.7.7",
"#babel/runtime": "7.7.7",
"#react-native-community/eslint-config": "0.0.5",
"babel-jest": "24.9.0",
"eslint": "6.8.0",
"jest": "24.9.0",
"metro-react-native-babel-preset": "0.56.3",
"react-test-renderer": "16.9.0"
},
"jest": {
"preset": "react-native"
}
}
Hooks can only be used in a component, and cannot be used in navigator or navigationOptions. If u need to use state in navigator or navigationOptions, u need to setParams in the component and then use the getParam method in navigationOptions

When I Press Back Button(from the stack header) or swipe left to right, ios app crashes

When I Press Back Button(In the stack header) or swipe left to right in a physical iPhone device my react native app crashes. The App is working fine in the simulator but not in a physical device.
So, how can I fix this issue?
Here is the code for my main navigator
import { themeColor, themeColorLite } from "../styles/common";
import Login from "../views/Login"
import ForgetPass from "../views/forgetPass"
import Registration from "../views/Registration"
import UserDashboard from "../views/user/dashboard"
import ProductDetails from "../views/user/productDetails"
import ShoppingMall from "../views/user/shoppingMalls"
import Shops from "../views/user/shops"
import NormalProducts from "../views/user/normalProducts"
import ShoppingCart from "../views/user/shoppingCart"
import UserProfile from "../views/user/profile"
import { createSwitchNavigator, createAppContainer } from 'react-navigation'
import { createStackNavigator } from 'react-navigation-stack'
import { createDrawerNavigator } from 'react-navigation-drawer'
import UserDrawerNav from '../views/drawerNav'
const ProductStackNavigator = createStackNavigator({
UserDashboard: UserDashboard,
ProductDetails: ProductDetails,
ShoppingCart: ShoppingCart,
ShoppingMall: ShoppingMall,
Shops: Shops,
NormalProducts: NormalProducts
})
const DrawerNavigator = createDrawerNavigator(
{
UserDashboard: ProductStackNavigator,
ShoppingMall: ProductStackNavigator,
UserProfile: UserProfile
},
{
contentComponent: UserDrawerNav,
hideStatusBar: true,
drawerBackgroundColor: 'white',
overlayColor: themeColorLite,
contentOptions: {
activeTintColor: '#fff',
activeBackgroundColor: themeColor,
}
}, {
unmountInactiveRoutes: true
}
);
const AuthStack = createSwitchNavigator({
Login: { screen: Login } ,
Registration: { screen: Registration } ,
ForgetPass: { screen: ForgetPass }
});
const AppContainer = createAppContainer(createSwitchNavigator(
{
App: DrawerNavigator,
Auth: AuthStack
},
{
initialRouteName: 'Auth',
}
));
export default AppContainer;
Here is my package.json
{
"name": "ENC",
"version": "0.0.1",
"private": true,
"scripts": {
"android": "react-native run-android",
"ios": "react-native run-ios",
"ipadAir": "react-native run-ios --simulator 'iPad Air (3rd generation)'",
"start": "react-native start",
"iphone11": "react-native run-ios --simulator 'iPhone 11'",
"cleanStart": "react-native start --reset-cache",
"link": "react-native link",
"podInstall": "cd ios && pod install && cd ..",
"podUpdate": "cd ios && pod update && cd ..",
"upgradeRN": "react-native upgrade",
"regenDir": "react-native upgrade --legacy true",
"test": "jest"
},
"dependencies": {
"#react-native-community/async-storage": "^1.6.1",
"axios": "^0.19.0",
"react": "16.10.1",
"react-native": "0.61.2",
"react-native-flip-card": "^3.5.5",
"react-native-fs": "^2.14.1",
"react-native-gesture-handler": "^1.4.1",
"react-native-image-picker": "^1.1.0",
"react-native-photo-upload": "^1.3.0",
"react-native-really-awesome-button": "^1.4.2",
"react-native-reanimated": "^1.2.0",
"react-native-tiny-toast": "^1.0.3",
"react-native-webview": "^7.4.0",
"react-native-xml2js": "^1.0.3",
"react-navigation": "^4.0.5",
"react-navigation-drawer": "^2.2.1",
"react-navigation-stack": "1.5.1"
},
"devDependencies": {
"#babel/core": "^7.5.0",
"#babel/runtime": "^7.5.0",
"#react-native-community/eslint-config": "^0.0.5",
"babel-jest": "^24.1.0",
"jest": "^24.1.0",
"metro-react-native-babel-preset": "^0.56.0",
"react-test-renderer": "16.10.1"
},
"jest": {
"preset": "react-native"
}
}
The above code is also available at my gist here
Finally found the solution after profiling the issue in xcode
this solution is work for me fine :
https://github.com/kmagiera/react-native-gesture-handler/issues/320#issuecomment-447534290

Blank Screen while using react-navigation

I am beginner is React Native, I have created a sample application and trying to add react-navigation
here is my code in App.js
import React, {Fragment} from 'react';
import {
StyleSheet,
View,
StatusBar,
} from 'react-native';
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
import Container from './ScreenContainer';
const App = () => {
return (
<View style={styles.container}>
<StatusBar backgroundColor="#819ca9" barStyle="light-content" hidden={true}></StatusBar>
</View>
);
};
const styles = StyleSheet.create({
container:{
justifyContent: 'center',
backgroundColor:'#819ca9',
flex: 1,
// alignItems:"center"
},
});
export default App;
And this is my ScreenContainer.js
import { createAppContainer, createStackNavigator, StackActions, NavigationActions } from 'react-navigation'; // Version can be specified in package.json
import login from './pages/login'; //import Login from './pages/login';
const NavigationStack = createStackNavigator({
SignUp: login
});
const Container = createAppContainer(NavigationStack);
export default Container;
When I use this js in my App.js everything goes blank, after research a lot I found if I comment alignItems:"center" then my screen comes bt everything get distorted, search a lot on internet couldn't found the proper solution
Here is my package.json
{
"name": "MobileAPP",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "react-native start",
"test": "jest",
"lint": "eslint ."
},
"dependencies": {
"react": "16.8.6",
"react-native": "0.60.4",
"react-native-gesture-handler": "^1.3.0",
"react-native-router-flux": "^4.0.6",
"react-navigation": "^3.11.1"
},
"devDependencies": {
"#babel/core": "7.5.5",
"#babel/runtime": "7.5.5",
"#react-native-community/eslint-config": "0.0.3",
"babel-jest": "24.8.0",
"eslint": "^6.0.1",
"jest": "24.8.0",
"metro-react-native-babel-preset": "0.54.1",
"react-test-renderer": "16.8.6"
},
"jest": {
"preset": "react-native"
}
}
Can anyone help me how to make this working without disturbing my UI.
Thanks a lot
I see that you’ve imported Container in your App.js but I can’t see it being used anywhere.

undefined is not an object(evaluating'_react2.Proptypes.store')?

I am trying to build the taxi booking app. From i get the error
undefined is not an object (evaluating'_react2.Proptypes.store'). I have tried all the other solutions in previously asked but it was not helpful. Any help on this is appreciated.
import React, { Component, PropTypes} from "react";
import { Router } from "react-native-router-flux";
import scenes from "../routes/scenes";
import { Provider } from "react-redux";
export default class AppContainer extends Component{
static propTypes = {
store: PropTypes.object.isRequired
}
render(){
return(
<Provider store = {this.props.store}>
<Router scenes = {scenes} />
</Provider>
)
}
}
package.json
{
"name": "TaxiApp",
"version": "0.0.7",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"native-base": "^2.3.5",
"prop-types": "^15.6.0",
"react": "16.0.0",
"react-addons-update": "^15.6.2",
"react-native": "0.51.0",
"react-native-router-flux": "^4.0.0-beta.24",
"react-redux": "^5.0.6",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0"
},
"devDependencies": {
"babel-jest": "22.0.4",
"babel-preset-react-native": "4.0.0",
"jest": "22.0.4",
"react-test-renderer": "16.0.0",
"redux-logger": "^3.0.6"
},
"jest": {
"preset": "react-native"
}
}
Ever since react version 16.0.0 forward, prop types cannot be taken from React itself as you did on the first line. You need to do the following;
npm install prop-types --save
Add the following import and remove the PropTypes from first line:
import PropTypes from 'prop-types';
After this, it should work like a charm.