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
}
} elseif (result.action === Share.dismissedAction) {
// dismissed
}
} catch (error) {
alert(error.message);
}
};
return (
<Viewstyle={{marginTop:50 }}><ButtononPress={onShare}title="Share" /></View>
);
};
exportdefault ShareExample;
import React, { Component } from'react';
import { Share, View, Button } from'react-native';
classShareExampleextendsComponent{
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
}
} elseif (result.action === Share.dismissedAction) {
// dismissed
}
} catch (error) {
alert(error.message);
}
};
render() {
return (
<Viewstyle={{marginTop:50 }}><ButtononPress={this.onShare}title="Share" /></View>
);
}
}
exportdefault ShareExample;
Reference
Methods
share()
staticshare(content, options)
Open a dialog to share text content.
In iOS, returns a Promise which will be invoked with an object containing action and activityType. If the user dismissed the dialog, the Promise will still be resolved with action being Share.dismissedAction and all the other keys being undefined. Note that some share options will not appear or work on the iOS simulator.
In Android, returns a Promise which will always be resolved with action being Share.sharedAction.