Is there a Share library like this one out there? for React native - react-native

I have been seeing this Share library in many new Apps and I was wondering if there is one like it already. if not, how does one trigger opening WhatsApp and Facebook in React Native? thank you

You can use import { Share} from 'react-native'
import React from 'react';
import { Share, View, Button } from 'react-native';
const ShareExample = () => {
const onShare = async () => {
try {
const result = await Share.share({
message:
'React Native | A framework for building native apps using React',
});
if (result.action === Share.sharedAction) {
if (result.activityType) {
// shared with activity type of result.activityType
} else {
// shared
}
} else if (result.action === Share.dismissedAction) {
// dismissed
}
} catch (error) {
alert(error.message);
}
};
return (
<View style={{ marginTop: 50 }}>
<Button onPress={onShare} title="Share" />
</View>
);
};
export default ShareExample;
or
react-native-share library
npm i react-native-share --save to get your desired result.

One solution is to use Linking.openURL and social_icon together
(A) For social icons, you may install and use by referring to this official link:
https://reactnativeelements.com/docs/social_icon/
(B) For opening social media / email, etc thru Linking.openURL, the following are some examples:
Linking.openURL('fb://page/PAGE_ID');
Linking.openURL('http://instagram.com/_u/USER_NAME');
Linking.openURL('http://instagram.com/_p/PICTURE');
Linking.openURL('mailto:support#example.com')
Note: you have to import Linking , such as follows:
import { Button, Linking, View } from 'react-native';

Related

Firebase react native-Could not get a document field from firestore to display in react native app

I am new to react native and firestore
Context:
Firestore has a document "Users" and "Location" is a field.
Requirement : I want the location of one user/logged in user to display in the react native app.
I have written the following code, however post login the Location field is empty.
Please suggest where am I wrong.
import React from "react";
import { Image, Button, TextInput, ScrollView, Stylesheet, Text, View } from "react-native";
import Constants from 'expo-constants';
import * as firebase from 'firebase';
import 'firebase/firestore';
constructor(props) {
super(props);
this.state = {
profileuser: [],
profileuserlocation:''
};
}
componentDidMount() {
firebase.firestore().collection('users').doc(firebase.auth().currentUser.uid).get().then(snapshot => {
let profileuserlocation = snapshot.data().Location;
this.setState({ profileuserlocation: snapshot.data().Location });
});
}
render() {
const profileuserlocation=this.state.profileuserlocation;
return (
<View>
<Text style={{ fontSize: 20, fontWeight: "bold" }}> {this.state.profileuserlocation}</Text>
</View>
)
I know it's a bit late but just in case you never found the answer...
The standard way to get the current user's uid is:
firebase.auth().onAuthStateChanged(user => {
if (user) {
firebaseUser = {
uid: user.uid,
name: user.displayName,//this and the email can also be got from firebaseUser
email: user.email
};
this.setState({ uid: firebaseUser.uid });
}
});
Then, if you just want to display the field from your doc you don't need to use snapshot - that's really for if you want to keep watching for changes in the field and is used in a different way. If you just want to retrieve the field one time then use:
const docRef = firebase.firestore().collection('users').doc(this.state.uid)
this.docRef
.doc(this.state.uid)
.get().then(function (doc) {
if (doc.exists) {
//useful ES6 'destructuring' technique for if there are many fields you are
//saving
const {
Location
} = doc.data();
this.setState({
Location
})
//or for one field you can use "this.setState({Location:doc.data().Location})"
} else {
// doc.data() will be undefined in this case
console.log("No such document!");
}
}).catch(function (error) {
console.log("Error getting document:", error);
});
You can get some good examples in the documents: firestore docs
Hope this helps someone...

react-native-image-picker vs expo ImagePicker

I have tried many attempts to get react-native-image-picker up and working with my RN app. I am using Expo and VS Code and am not running the app with Xcode or Android Studio. There seems to be many options to getting the camera roll available in an app and I am not sure which is the best path to take. None seem to be working for me so I would like to pick the best path and focus on making that one route work.
I am following the documentation: https://github.com/react-native-community/react-native-image-picker
Things I have tried:
React Native Camera Roll
Expo ImagePicker
expo-image-picker
react-native-image-picker
react-images-upload
react-native-photo-upload
My code:
import React, {useState, useEffect} from 'react';
import {StyleSheet, View, TextInput, TouchableOpacity, Text, CameraRoll } from 'react-native'
import ImagePicker from 'react-native-image-picker';
// import * as ImagePicker from 'expo-image-picker';
import Constants from 'expo-constants';
const PicturesScreen = ({navigation}) => {
const [pictures, setPictures] = useState([]);
getPermissionAsync = async () => {
if (Constants.platform.ios) {
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status !== 'granted') {
alert('Sorry, we need camera roll permissions to make this work!');
}
}
};
useEffect(() => {
getPermissionAsync();
}, []);
selectPhotoTapped = () => {
const options = {
quality: 1.0,
maxWidth: 500,
maxHeight: 500,
storageOptions: {
skipBackup: true,
},
};
ImagePicker.showImagePicker(options, response => {
if (response.didCancel) {
console.log('User cancelled photo picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton);
} else {
let source = {uri: response.uri};
console.log('source: ' + source);
// You can also display the image using data:
// let source = { uri: 'data:image/jpeg;base64,' + response.data };
setPictures({
picture: source
});
}
});
};
return (
<View style = {styles.container}>
<TouchableOpacity style = {styles.buttonContainerPhoto} onPress={()=> selectPhotoTapped()}>
<Text style={styles.buttonText} >
Upload Photos
</Text>
</TouchableOpacity>
<TouchableOpacity style = {styles.buttonContainer} onPress={()=> navigation.navigate('NextScreen')}>
<Text style={styles.buttonText} >
Next
</Text>
</TouchableOpacity>
</View>
);
};
const styles = StyleSheet.create({
container: {
...
}
});
export default PicturesScreen;
I have made sure to link the packages, I have also uninstalled and reinstalled and started from scratch a few times. I have downgraded the version to make it work but I still continue to get one of these error messages:
react-native-image-picker: NativeModule.ImagePickerManager is null
or
Can not read property 'showImagePicker' of undefined.
or
undefined is not an object(evaluating 'imagepickerManager.showimagepicker')
Is it causing issues because I am using Expo? Should I just be using CameraRoll with react-native?
Use expo-image-picker if you're using Expo.
Anything that requires the use of react-native link will not work with Expo, unless stated that it is already included in Expo.
I was stuck with the same issue, and now I have resolved this. React native cli and expo cli both have some package differences. Similarly image-picker for both works differently.
For expo cli, you can check the documentation here: https://docs.expo.io/versions/latest/sdk/imagepicker/#__next
You can try for uploading image via camera using ImagePicker.launchCameraAsync and from photo library using ImagePicker.launchImageLibraryAsync.
While in react-native cli I was able to write a single function to choose from both camera and photo library but could not find a way for the same in expo yet.

What does it call for the menu show as slide up?

[React Native]
Can anyone help to elaborate what should that kind of menu called? When onclick, it slided up. Please also introduce what are other menu's available in react native? (Must work for both android and iOS)
Example
It is share dialog and you can easily implement it in React Native
Try this code
import React, {Component} from 'react';
import {Share, Button} from 'react-native';
class ShareExample extends Component {
onShare = async () => {
try {
const result = await Share.share({
message:
'React Native | A framework for building native apps using React',
});
if (result.action === Share.sharedAction) {
if (result.activityType) {
// shared with activity type of result.activityType
} else {
// shared
}
} else if (result.action === Share.dismissedAction) {
// dismissed
}
} catch (error) {
alert(error.message);
}
};
render() {
return <Button onPress={this.onShare} title="Share" />;
}
}
React Native Modal
1-
react-native-modal
2-
react-native-modalbox

React native camera not working as expected with expo

I want to make a qrcode reader with react native,and i'm using expo.I'm following the guide based on the official documentation here : react-native-qrcode-scanner
I've installed react-native-camera succesfully by running npm install react-native-camera --save and the following is the commande react-native link react-native-camera in the console,and it gives me the following error:
By reading the doc if find that many things depend on react-native link like react-native link react-native-qrcode-scanner
react-native link react-native-permissions an also after that there are some configurations like <uses-permission android:name="android.permission.VIBRATE"/> that i cannot do because i don't use react-native-cli.
Expo already has what you want. expo-barcode-scanner
If your version of Expo is 33, you should download it individually.
You can run expo install expo-barcode-scanner
You must request permission to access the user's camera before attempting to get it. To do this, you will want to use the Permissions API. You can see this in practice in the following example.
Basic Example
import * as React from 'react';
import { Text, View, StyleSheet, Button } from 'react-native';
import Constants from 'expo-constants';
import * as Permissions from 'expo-permissions';
import { BarCodeScanner } from 'expo-barcode-scanner';
export default class BarcodeScannerExample extends React.Component {
state = {
hasCameraPermission: null,
scanned: false,
};
async componentDidMount() {
this.getPermissionsAsync();
}
getPermissionsAsync = async () => {
const { status } = await Permissions.askAsync(Permissions.CAMERA);
this.setState({ hasCameraPermission: status === 'granted' });
};
render() {
const { hasCameraPermission, scanned } = this.state;
if (hasCameraPermission === null) {
return <Text>Requesting for camera permission</Text>;
}
if (hasCameraPermission === false) {
return <Text>No access to camera</Text>;
}
return (
<View
style={{
flex: 1,
flexDirection: 'column',
justifyContent: 'flex-end',
}}>
<BarCodeScanner
onBarCodeScanned={scanned ? undefined : this.handleBarCodeScanned}
style={StyleSheet.absoluteFillObject}
/>
{scanned && (
<Button title={'Tap to Scan Again'} onPress={() => this.setState({ scanned: false })} />
)}
</View>
);
}
handleBarCodeScanned = ({ type, data }) => {
this.setState({ scanned: true });
alert(`Bar code with type ${type} and data ${data} has been scanned!`);
};
}
props
onBarCodeScanned (function)
Callback that is invoked when a bar code has been successfully scanned. The callback is provided with an object of the shape { type: BarCodeScanner.Constants.BarCodeType, data: string }, where the type refers to the bar code type that was scanned and the data is the information encoded in the bar code (in this case of QR codes, this is often a URL). See BarCodeScanner.Constants.BarCodeType for supported values.
barCodeTypes (Array)
An array of bar code types. Usage: BarCodeScanner.Constants.BarCodeType.<codeType> where codeType is one of the listed above. Default: all supported bar code types. For example:
barCodeTypes={[BarCodeScanner.Constants.BarCodeType.qr]}
type (string)
-- Camera facing. Use one of BarCodeScanner.Constants.Type. Use either Type.front or Type.back. Same as Camera.Constants.Type. Default: Type.back.
Note: Passing undefined to the onBarCodeScanned prop will result in no scanning. This can be used to effectively "pause" the scanner so that it doesn't continually scan even after data has been retrieved.
if you are using expo, you will have npx. You can run any react-native command with npx. Hence:
npx react-native link react-native-camera

how could I swipe the page from left to right in order to go back in a webview?

In a web view,I want to swipe the page from left to right in order to go back,just like what safari did.
what should I do?
TL/DR: Currently, you'll need a third party package for that. Use the react-native-wkwebview-reborn package and set the allowsBackForwardNavigationGestures prop to true. Example:
WebView.ios.js:
import React from 'react';
import WKWebView from 'react-native-wkwebview-reborn';
export default (props) => <WKWebView allowsBackForwardNavigationGestures {...props} />
WebView.js
import { WebView } from 'react-native';
export default WebView;
It's a drop-in replacement, so you won't need to change much code.
Why:
The WebView component from React Native uses UIWebView under the hood, which is not recommended by Apple anymore:
It has worse performance and does not support a lot of features, like 3D Touch and swipe back gesture.
Join this discussion so react native updates their core component.
You can use react-native-webview package. And just pass allowsBackForwardNavigationGestures prop to WebView:
import WebView from 'react-native-webview';
<WebView
allowsBackForwardNavigationGestures
...
/>
I know it's been a long time but I came across with the same problem and couldn't find any working solutions for android. So I came up with mine. Also, this post is first to show up when you google 'react native Webview swipe back', so I thought it would help more people posting my solution here.
import * as React from "react";
import { WebView } from "react-native-webview";
import { StyleSheet, Platform, BackHandler } from "react-native";
export default function App() {
const webViewRef = React.useRef();
const onAndroidBackpress = () => {
if (webViewRef.current) {
webViewRef.current.goBack();
return true;
}
return false;
};
return (
<WebView
onTouchStart={e => {
if(Platform.OS === 'android'){
this.touchX = e.nativeEvent.pageX;
this.touchY = e.nativeEvent.pageY
} else {
return null
}}
}
onTouchEnd={e => {
if(Platform.OS === 'android' && this.touchX - e.nativeEvent.pageX < -20){
if(this.touchY - e.nativeEvent.pageY > -20 && this.touchY - e.nativeEvent.pageY < 20){
onAndroidBackpress()
}
} else {
return null
}}
}
allowsBackForwardNavigationGestures // only works with iOS
allowsInlineMediaPlayback
ref={webViewRef}
style={styles.container}
source={{ uri: "https://<yourwebsite>.com" }}
/>
);
}
const styles = StyleSheet.create({
container: {
flex: 1
}
});