我试图使用'react-native'中的Share,并希望传递shop object以使用其属性显示消息。但是,在运行应用程序时,它是未定义的。您能帮我解决这个问题吗?
import { StyleSheet, View, Image, Button, FlatList, Dimensions, SafeAreaView, ScrollView, Share } from 'react-native';
...
const [shop, setShop] = useState(
{
id: 1,
eventName: "Wild Rose Special Offer",
eventDesc: "25% off! vs Regular price",
name: "Oak & Vine",
desc: "This is amazing opportunity!",
location: "1026 16 Ave NW, Calgary",
image: "https://i.imgur.com/UPrs1EWl.jpg",
dueDate: "2020-04-30",
officeHour: "11:00 ~ 20:30 (Mon~Sun)",
call: "(587) 438-0275"
}
);
const onShare = async ({ shop },) => {
try {
const result = await Share.share({
message:
`[${shop.eventName}] at ${shop.name} by ${shop.dueDate}`,
});
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);
}
};
...
您可以尝试https://github.com/react-native-community/react-native-share
按照他们的安装说明。