How to avoid having createStackNavigator crash react-native app? - react-native

I'm trying to implement a basic stack navigation using createStackNavigator:
App.js
import { createStackNavigator } from '#react-navigation/stack';
const Stack = createStackNavigator();
function MyStack() {
return (
<Stack.Navigator>
<Stack.Screen name="Home" component={Home} />
<Stack.Screen name="Notifications" component={Notifications} />
<Stack.Screen name="Profile" component={Profile} />
<Stack.Screen name="Settings" component={Settings} />
</Stack.Navigator>
);
}
When I run this on my Mac (Big Sur), I get this error in the Metro console:
[Mon Jan 25 2021 19:21:42.268] BUNDLE ./index.js
[Mon Jan 25 2021 19:21:44.595] ERROR TypeError: null is not an object (evaluating '_RNGestureHandlerModule.default.Direction')
[Mon Jan 25 2021 19:21:44.596] ERROR Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication)
[Mon Jan 25 2021 19:21:45.600] ERROR Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication)
Here is my package.json:
{
"name": "stack_navigation",
"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-navigation/stack": "^5.14.2",
"react": "16.13.1",
"react-native": "0.63.4"
},
"devDependencies": {
"#babel/core": "7.12.10",
"#babel/runtime": "7.12.5",
"#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"
}
}
Is there anything I can do to be able using createStackNavigator without crashing?

In react-navigation installation guide they mentioned that, with react navigation you have to install some other dependencies as well.
So first install these library as well with react-natvigation :
npm install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context #react-native-community/masked-view
If you are on android auto linking will done automatically, if you are on ios you have to run:
pod install
Now, import react-native-gesture-handle on your app top of the file like in app.js or index.js :
import 'react-native-gesture-handler';
As documentation mentioned you have to do this, you can not skip this installation step if you are building app for android or ios:
Note: If you are building for Android or iOS, do not skip this step, or your app may crash in production even if it works fine in development. This is not applicable to other platforms.
And last you need to wrap the whole app in NavigationContainer. Usually you'd do this in your entry file, such as index.js or App.js:
import 'react-native-gesture-handler';
import * as React from 'react';
import { NavigationContainer } from '#react-navigation/native';
export default function App() {
return (
<NavigationContainer>{/* Rest of your app code */}</NavigationContainer>
);
}

in the app.js you have to import the components if they are not present in that file. also you will need a navigationContainer wrapping the app.js code, indicating that these areas can be navigated. and you will need an event to navigate of course, a button perhaps, igniting the event and you will need a function. So the navigation.navigate in the docs isnt working but when you change that to push it works. However, it just pushes another screen to your stack. You are not navigating among the existing screens you've created. I'd say just go for the react-router. maybe use an older version of react-nav. v5 seems to be broken.

Related

I am trying to use the #react-navigation/drawer but it was not working properly

This is my app.js code
import React from 'react';
import {
SafeAreaView,
ScrollView,
StatusBar,
StyleSheet,
Text,
useColorScheme,
View,
} from 'react-native';
import { createDrawerNavigator } from '#react-navigation/drawer';
import { NavigationContainer } from '#react-navigation/native';
import Home from './src/components/Home';
import Login from './src/components/Login';
const Drawer = createDrawerNavigator();
function App() {
return(
<NavigationContainer>
<Drawer.Navigator>
<Drawer.Screen name="Home" component={Home} />
<Drawer.Screen name="Login" component={Login} />
</Drawer.Navigator>
</NavigationContainer>
)
}
export default App;
This is my dependency package
"dependencies": {
"#react-navigation/bottom-tabs": "^6.2.0",
"#react-navigation/drawer": "^6.3.1",
"#react-navigation/native": "^6.0.8",
"#react-navigation/native-stack": "^6.5.1",
"react": "17.0.2",
"react-native": "^0.65.0",
"react-native-gesture-handler": "^1.10.3",
"react-native-reanimated": "^1.13.2"
},
Error
ERROR TypeError: null is not an object (evaluating '_ReanimatedModule.default.createNode')
ERROR Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication). A frequent cause of the error is that the application entry file path is incorrect.
This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native.
ERROR Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication). A frequent cause of the error is that the application entry file path is incorrect.
This can also happen when the JS bundle is corrupt or there is an early initialization error when loading React Native.
When i try to start react-native this error comes...
Please any one help me and rectify me
I was getting the same error.
Upgrading the react-native-reanimated version to react-native-reanimated#2.2.0 solved my issue
I used npm install react-native-reanimated#2.2.0, after uninstalling the previous version

How to fix Expo Print.printToFileAsync(options) undefined is not an object error

I am trying to use Expo's Print.printToFileAsync(options but I keep on getting [Unhandled promise rejection: TypeError: undefined is not an object (evaluating '_expoPrint.Print.printToFileAsync')].
I looked for a lot of solutions online but couldn't find a solution to this. I started using React Native libraries but as I searched, turns out I can only use Expo's library so I switched to Print.printToFileAsync().
App.js
import React, {Component} from 'react';
import { StyleSheet, Text, View, TouchableHighlight } from 'react-native';
import { Print } from 'expo-print';
export default class App extends Component {
async createPDF() {
let filePath = await Print.printToFileAsync({
html: "<h1>PDF TEST</h1>",
width : 612,
height : 792,
base64 : false
});
alert('PDF Generated', filePath.uri);
}
render() {
return(
<View>
<TouchableHighlight onPress={this.createPDF} style={styles.Main}>
<Text>Create PDF</Text>
</TouchableHighlight>
</View>
)
}
}
const styles = StyleSheet.create({
Main : { marginTop : 100 }
});
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": {
"expo": "^33.0.0",
"expo-print": "^5.0.1",
"react": "16.8.3",
"react-dom": "^16.8.6",
"react-native": "https://github.com/expo/react-native/archive/sdk-33.0.0.tar.gz",
"react-native-html-to-pdf": "^0.7.0",
"react-native-share": "^1.2.1",
"react-native-web": "^0.11.4"
},
"devDependencies": {
"babel-preset-expo": "^5.1.1"
},
"private": true
}
My end goal is to make a PDF file using HTML in my Expo project.
Please use -
import * as Print from 'expo-print';
instead of -
import { Print } from 'expo-print';
The Expo sdk33 requires the module to be installed and run directly.
expo install expo-print
Modular Imports
With SDK 33, we’re deprecating imports of most modules from the expo package. This means that in a future release, you won’t be able to write, for example, import { FileSystem } from 'expo';. Rather, you’ll need to install the individual packages for each module you use and import from them instead.
You can use the new expo install command to install modules; this command is a wrapper around npm install/yarn add that automatically installs a module version compatible with your SDK version. For example, for the FileSystem module, you would run expo install expo-file-system and then use import * as FileSystem from 'expo-file-system';. This change paves the way for tree-shaking and smaller JavaScript bundles. It also makes moving between the managed and bare workflows simpler.
Imports from the expo package will continue to work in SDK 33 but will generate a warning in the console, and you’ll need to import from the individual packages to make the warning disappear. To make this change easier, we’re providing a codemod that’ll automatically update all of your imports for you.
Home page with description of SDK33
follow this link https://stackoverflow.com/a/71569236/14448694
import * as Print from "expo-print"; import { shareAsync } from "expo-sharing";

how to add #expo/vector-icons in react native app?

i am using #expo/vector-icons here is my package.json file
"dependencies": {
"#expo/vector-icons": "^4.1.1",
"color": "^1.0.3",
"expo": "^30.0.1",
"moment": "^2.22.2",
"react": "16.3.1",
"react-native": "https://github.com/expo/react-native/archive/sdk-
30.0.0.tar.gz",
"react-native-extended-stylesheet": "^0.4.0" },
when i import #expo/vector-icon library in my component
import { Ionicons } from '#expo/vector-icons';
it gives error
The library comes by default so you don't need to install #expo/vector-icons.
You can just do this for example:
import { Ionicons } from '#expo/vector-icons';
and then you can use it like:
<Ionicons name="ios-pizza" color="red" size={200} />
FYI, this directory is helpful in finding different icons https://expo.github.io/vector-icons/
In your dependencies (package.json) remove #expo/vector-icons. They are included in the expo package and different versions of expo and expo/vector-icons can cause errors like this one.
From the official docs:
This library is installed by default on the template project that get
through expo init -- it is part of the expo package. It includes
popular icon sets and you can browse all of the icons using the
#expo/vector-icons directory.
Update 2021:
The official docs changed a bit, and instead of searching for icons in:
#expo/vector-icons directory
You can now search using:
icons.expo.fyi
This makes it far easier than before.
Go to 'https://icons.expo.fyi/' choose whatever you want, then import via copy, then use it.
import React from 'react'
import { Entypo } from '#expo/vector-icons';
import { View } from 'react-native';
export const Example = () => {
return(
<View>
<Entypo name="plus" size={24} color="black" />
</View>
)
}
Delete your node module folder and run expo init. And run the project

React-Native, BleManager "Native Module cannot be null"

I wanted to try out the react-native-ble-plx library from Polidea
https://github.com/Polidea/react-native-ble-plx
So I created a new app in terminal and installed the library as explained by Polidea
create-react-native-app playground
npm install --save react-native-ble-plx
react-native link react-native-ble-plx
I then ran npm start and then i (for opening in iOS simulator)
and then I got the following error (in the terminal and in the simulator)
Native module cannot be null
node_modules/react-native/node_modules/fbjs/lib/invariant.js:44:24 in invariant
node_modules/react-native/Libraries/EventEmitter/NativeEventEmitter.js:31:16 in NativeEventEmitter
node_modules/react-native-ble-plx/src/BleManager.js:52:25 in BleManager
App.js:8:19 in App
node_modules/react-native/Libraries/Renderer/ReactNativeStack-dev.js:1679:33 in
node_modules/react-native/Libraries/Renderer/ReactNativeStack-dev.js:1610:15 in measureLifeCyclePerf
... 52 more stack frames from framework internals
Here is the code in App.js:
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import {BleManager} from 'react-native-ble-plx';
//importing BleManager without calling new BleManager() works
export default class App extends React.Component {
constructor(){
super();
this.manager = new BleManager();
}
//calling new BleManager() leads to error
render() {
return (
<View style={styles.container}>
<Text>nothing, just sucking errors</Text>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
Here is the file package.json
{
"name": "playground",
"version": "0.1.0",
"private": true,
"devDependencies": {
"react-native-scripts": "1.5.0",
"jest-expo": "^21.0.2",
"react-test-renderer": "16.0.0-alpha.12"
},
"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 --watch"
},
"jest": {
"preset": "jest-expo"
},
"dependencies": {
"expo": "^21.0.0",
"react": "16.0.0-alpha.12",
"react-native": "^0.48.4",
"react-native-ble-plx": "^0.6.3"
}
}
I already looked for help and tried
npm -rm rf node_modules
npm install
but it didn't help.
Here is my system information:
macOS High Sierra
iMac (27-inch, Mid 2010)
Processor 2.8 GHz Intel Core i5
Memory 16 GB 1333 MHz DDR3
I also got the same error when opening the app in my iPhone(6s, 10.3.1) (after scanning QR code in terminal from npm start)
Other apps without the react-native-ble-plx library work normally.
Looking forward receiving some help.
Thanks in advance.
Frank
You have used create-react-native-app to create your React Native project. So you can not use react-native-ble-plx library with this project. Because to access Bluetooth hardware, react-native-ble-plx package has some native code written in android and ios seperately.
If you want to build your app, you'll need to eject from create-react-native-app and use Xcode and Android Studio.
Use the below command:--
npm install -g react-native-cli
npm run eject
react-native link react-native-ble-plx
1st command is to install react-native-cli globally
2nd command is to convert your project from Expo project to regular React Native project
3rd command is to link react-native-ble-plx. Because most probably your previous link was failed.
What I did was (after completely removing ble-plx by discarding git changes before I started)
navigating to /node_modules/react-native-ble-plx/ios/BleClient.xcodeproj and
I pulled that into the Libraries folder in the project navigator
Then I went to my target -> build phases -> link binary with libraries and added libBleClient.a

React Native with Native Base (Unexpected Token Error)

I have React Native and Native Base configured. When I deploy the app for Android it throws me an error stating that an unexpected token was found near where Container component is present in my code.
My package.json file is:
{
"name": "React Native POC",
"version": "0.0.1",
"private": true,
"scripts": {
"start": "node node_modules/react-native/local-cli/cli.js start",
"test": "jest"
},
"dependencies": {
"native-base": "^0.5.18",
"react": "15.4.1",
"react-native": "0.39.2",
"react-redux": "^4.4.6",
"redux": "^3.6.0"
},
"devDependencies": {
"babel-jest": "17.0.2",
"babel-preset-react-native": "1.9.0",
"jest": "17.0.3",
"react-test-renderer": "15.4.1"
},
"jest": {
"preset": "react-native"
}
}
Could it be that certain versions are not working well with the other?
Additionally (if it matters) I am using Node version 6.8.1, npm version 3.10.8 and react-native-cli 2.0.0. Also, I have yarn, sinopia and browserify installed globally.
Still a beginner with React Native and I cannot tell if any dependency clashes may be present (obvious or otherwise)
My js file is:
import React, {Component} from 'react';
import {Container, Content} from 'native-base';
export default class ReactNativePOC extends Component {
render() {
return {
<Container> // Error here
<Content>
</Content>
</Container>
}
}
}
There does not seem to be any problem with my setup (excluding native base) as I can run a react native app with default controls however I seem to be getting this error only for native base controls
The return statement should use parentheses instead of braces.
render() {
return (
<Container>
<Content>
</Content>
</Container>
);
}
Please check the basic syntax from docs of React Native
Check NativeBase KitchenSink - An example app with all the UI components of NativeBase.
Since you said you are beginner in React Native, you can check Native Starter Kit - A Starter Kit for React Native + NativeBase + Navigation Experimental + Redux + CodePush Apps