TypeError: undefined is not an object (evaluating 'navigator.geolocation.requestAuthorization') - react-native

I am attempting to call:
`navigator.geolocation.requestAuthorization();`
to request geolocation permission.
But, this is resulting in error when running in iOS simulator
This was working at one point, but stopped. I attempted to delete and create a new project. I also tried to uninstall/reinstall node and react-native-cli.
import React, {Fragment, Component} from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
} from 'react-native';
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
export default class App extends Component {
constructor(props)
{
super(props);
navigator.geolocation.requestAuthorization();
}
render() {
return (
<View style={styles.MainContainer}>
<SafeAreaView style={{flex: 1, backgroundColor: '#fff'}}>
<Text style={styles.sectionTitle}>Hello World!</Text>
</SafeAreaView>
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer :{
justifyContent: 'center',
flex:1,
margin: 5,
marginTop: (Platform.OS === 'ios') ? 20 : 0,
},
sectionTitle: {
fontSize: 24,
fontWeight: '600',
color: Colors.black,
},
});
I am getting this error:
[error][tid:com.facebook.react.JavaScript] TypeError: undefined is not an object (evaluating 'navigator.geolocation.requestAuthorization')
This error is located at:
in App (at renderApplication.js:40)
in RCTView (at View.js:35)
in View (at AppContainer.js:98)
in RCTView (at View.js:35)
in View (at AppContainer.js:115)
in AppContainer (at renderApplication.js:39)

geolocation has been extracted from react native .60 version. If you need an alternative solution
install react-native-community/geolocation
npm install #react-native-community/geolocation --save
react-native link #react-native-community/geolocation
then
IOS
You need to include the NSLocationWhenInUseUsageDescription key in Info.plist to enable geolocation when using the app.
In order to enable geolocation in the background, you need to include the 'NSLocationAlwaysUsageDescription' key in Info.plist and add location as a background mode in the 'Capabilities' tab in Xcode.
Android
To request access to location, you need to add the following line to your app's AndroidManifest.xml
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
usage
import Geolocation from '#react-native-community/geolocation';
Geolocation.getCurrentPosition(info => console.log(info));

Please ensure you have added libRCTGeolocation.a in Build Phases --> Link Binary With Libraries on your Xcode Project settings and RCTGeolocation.xcodeproj under Libraries of your Xcode project.

From RN 0.6 or later, geolocation is not included in the RN core.If you want to use its API, you should do follow the steps in this link: https://github.com/react-native-community/react-native-geolocation

you should use #react-native-community/geolocation
first install it
yarn add #react-native-community/geolocation
then import
import Geolocation from '#react-native-community/geolocation';
in your function
Geolocation.setRNConfiguration(config);

I was also dealing with the same issue. Later on, I simply changed the package and everything was fine.
yarn add react-native-geolocation-service
// import Geolocation from '#react-native-community/geolocation';
import Geolocation from 'react-native-geolocation-service';

Related

How can use expo icons in react native app created by create-react-native-app my-project

I used create-react-native-app my-project but I want to use icons from #expo/vector-icons
Code
import { Ionicons} from '#expo/vector-icons';
export default function App(){
return <Ionicons name={Platform.OS === 'ios'?"ios-happy-outline":"md-happy"} size={100}/>}
Actual result
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.
Expected result
Icons should be visible on my User interface
You can use react-native-vector-icons from https://oblador.github.io/react-native-vector-icons/
first Install the Dependency (react-native-vector-icons) by
npm install react-native-vector-icons
Create assets/fonts directory in android/app/src/main
Once you create the fonts directory copy all the font files from node_modules/react-native-vector-icons/Fonts into it
Now in App.js you can react-native-vector-icons like below
import React from "react";
import {Text, View, StyleSheet, TouchableOpacity} from 'react-native';
import Icon from 'react-native-vector-icons/FontAwesome';
// https://icons.expo.fyi for icons
export default function toDoItems({item, pressHandler}){
return(
<TouchableOpacity onPress={()=> pressHandler(item.key)}>
<View>
<Icon name="rocket" size={30} color="#900" />
<Text style={styles.item}>{item.text}</Text>
</View>
</TouchableOpacity>
)
}
const styles= StyleSheet.create({
item : {
padding : 30,
borderColor : 'black',
borderWidth : 1,
borderStyle : 'dashed',
marginVertical : 20,
}
})

React Native Chrome Debugger Error: NativeUIManager.getConstantsForViewManager('Text') threw an exception

I'm newly developing in react native.
To start I used npx react-native init hello_world --template react-native-template-typescript to create the basic code and then I replaced app.tsx with this code:
import React from 'react';
import {StyleSheet, Text, View} from 'react-native';
const App = () => {
return (
<View style={styles.helloWorldContainer}>
<Text style={{fontSize: 18}}>Hello, world!</Text>
</View>
);
};
const styles = StyleSheet.create({
helloWorldContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
},
});
export default App;
And these are the errors that I get when I am in debug mode:
If I remove the style of the component Text the error disappears.
My question is how can I fix the error while keeping the style in the component Text.
I saw that chrome has a lot of errors, exist another better debugger?
As what Lucas Azambuja Santos referred to, it's a bug in react-native 0.65.* check out the new issue https://github.com/facebook/react-native/issues/32197
it should be fixed with 0.66, to workaround downgrade to 0.64.x and rebuild:
cd android && ./gradlew clean cleanBuildCache

Native module cannot be null - React Native, Expo

I made expo initial app
My code:
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import NfcManager, {NfcEvents} from 'react-native-nfc-manager';
export default function App() {
return (
<View style={styles.container}>
<Text>Open up App.js to start working on your app!</Text>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
When I trying to import NFC manager Im getting error: Native module cannot be null
screenshot
Google said that its error with IOS notifications, Im coding from windows VScode and cannot link push notifications, how I can solve this error? I cannot code without expo because this is only one way to simulate my app from windows on iphone as I understand
I solve it: expo not allowing additional libraries

Functions are not valid as a React child -----> from a React-Native Noobie

I have gotten to grips with React, but have just started a React-Native project today for the first time. My code is very simple at the moment, but I can't seem to get it to work. I get the following error:
Warning: Functions are not valid as a React child. This may happen if you return a Component instead of <Component /> from render. Or maybe you meant to call this function rather than return it.%s,
in RCTView (at View.js:45)
in View (at App.js:10)
in App (at withExpoRoot.js:20)
in RootErrorBoundary (at withExpoRoot.js:19)
in ExpoRootComponent (at renderApplication.js:35)
in RCTView (at View.js:45)
in View (at AppContainer.js:98)
in RCTView (at View.js:45)
in View (at AppContainer.js:115)
in AppContainer (at renderApplication.js:34)
- node_modules\react-native\Libraries\YellowBox\YellowBox.js:59:8 in error
- node_modules\expo\build\environment\muteWarnings.fx.js:26:24 in error
- ... 24 more stack frames from framework internals
Here is my code:
app.js
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Profile from './Profile/Profile.js';
export default function App() {
return (
<React.Fragment>
<View>{Profile}</View>
</React.Fragment>
);
}
profile.js
import React from 'react';
const Profile = (props) =>{
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
});
//style={this.styles.container}
return(
<React.Fragment>
<View>
<Text>Hi Guys</Text>
</View>
</React.Fragment>
);
}
export default Profile;
I have tried removing React Fragment in case it isn't supported in React Native, but this didn't do the trick. Any suggestions?
Also on a side note, what is the best way to a debug React-Native project? This error description doesn't make a lot of sense to me.
Thanks in advance.
You are using Profile Component incorrectly.
import React from 'react';
import { StyleSheet, Text, View } from 'react-native';
import Profile from './Profile/Profile.js';
export default function App() {
return (
<React.Fragment>
<View>
<Profile/> // <---- The correct way
</View>
</React.Fragment>
);
}
Profile is a component you created and component are used by other component with the JSX syntax, like this: <MyComponent prop1={someValueHere} prop2={somethingElse} />.
So your use of Profile is incorrect, you should write:
<View><Profile /></View>
instead of
<View>{Profile}</View>.
You are using Profile component in a wrong way.
Please remove {Profile} and change to <Profile/>

Upgraded react-native-fontawesome to 6.0.1, icons showing up as question marks

react-native-fontawesome was working just fine for me at version 5. I upgraded to 6.0.1 but downloading the new font-awesome files, adding the new tff files into the assets/fonts folder, running react-native link react-native-fontawesome.
I closed my simulators and ran react-native run-ios and see that the icons that worked before are now question marks. They don't show up on Android either
Firstly you need to install the dependency and link the library by the following ways...
Run the following command to install the dependency.
npm install react-native-vector-icons --save
Linking of Dependency...
After the updation of React Native 0.60, they have introduced the auto-linking feature means we do not require to link the library but they have also mentioned that some libraries need linking and react-native-vector-icons is one of those cases. So for that, we need to link the library using.
react-native link react-native-vector-icons
And copy the given App.js file that shows below.
/*Example of React Native Vector Icon*/
import React, {Component} from 'react';
//import React
import {Platform, StyleSheet, Text, View} from 'react-native';
//import all the basic components
import Icon from 'react-native-vector-icons/FontAwesome';
//import vector icons
type Props = {};
export default class App extends Component<Props> {
render() {
Icon.getImageSource('user', 20, 'red').then((source) => this.setState({ userIcon: source }));
return (
<View style={styles.container}>
<Text>Example of Vector Icon</Text>
<View style={{marginTop:16, marginBottom:16, justifyContent:'center', alignItems:'center', textAlign:'center'}}>
<Text><Icon name="rocket" size={'{30}'} color="#900" /></Text>
{/*Icon Component*/}
<Icon name="rocket" size={30} color="#900" />
</View>
<View style={{marginTop:16, marginBottom:16}}>
{/*Icon.Button Component*/}
<Icon.Button name="facebook" backgroundColor="#3b5998" onPress={()=>alert("Login with Facebook")}>
Login with Facebook
</Icon.Button>
</View>
</View>
);
}
}
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
}
});
or you can follow the following link...
https://aboutreact.com/react-native-vector-icons/