简体   繁体   English

如何在 React Native 共享中应用动态数据

[英]How to apply dynamic data in React Native share

For my recipe app, I want to enable users to share recipes with their friends.对于我的食谱应用程序,我希望用户能够与他们的朋友分享食谱。 For this, I'm using the official React Native share function.为此,我使用了官方的 React Native 共享 function。 I'm trying to pass props to the message and title, without any success.我正在尝试将道具传递给消息和标题,但没有成功。 Here's the code I'm using for sharing.这是我用来分享的代码。

 onShare = async () => {
// const result = await
const { caption } = this.props.section;
Share.share(
  {
    message: caption,
    title: "{section.caption}"
  },
  {
    excludedActivityTypes: [
      // 'com.apple.UIKit.activity.PostToWeibo',
      // 'com.apple.UIKit.activity.Print',
      // 'com.apple.UIKit.activity.CopyToPasteboard',
      // 'com.apple.UIKit.activity.AssignToContact',
      // 'com.apple.UIKit.activity.SaveToCameraRoll',
      // 'com.apple.UIKit.activity.AddToReadingList',
      // 'com.apple.UIKit.activity.PostToFlickr',
      // 'com.apple.UIKit.activity.PostToVimeo',
      // 'com.apple.UIKit.activity.PostToTencentWeibo',
      "com.apple.UIKit.activity.AirDrop"
      // 'com.apple.UIKit.activity.OpenInIBooks',
      // 'com.apple.UIKit.activity.MarkupAsPDF',
      // 'com.apple.reminders.RemindersEditorExtension',
      // 'com.apple.mobilenotes.SharingExtension',
      // 'com.apple.mobileslideshow.StreamShareService',
      // 'com.linkedin.LinkedIn.ShareExtension',
      // 'pinterest.ShareExtension',
      // 'com.google.GooglePlus.ShareExtension',
      // 'com.tumblr.tumblr.Share-With-Tumblr',
      // 'net.whatsapp.WhatsApp.ShareExtension', //WhatsApp
    ]
  }
);

}; };

The way data is loaded into this page is via {section.XXX}.数据加载到此页面的方式是通过 {section.XXX}。 Example:例子:

 <Caption style={styles.Caption}>{section.caption}</Caption>

This is how I trigger the 'onShare' func.这就是我触发“onShare”函数的方式。

<Button title="Share recipe" onPress={this.onShare} />

I hope someone can help me with this challenge.我希望有人能帮助我应对这个挑战。 Thanks in advance!!提前致谢!!

I see two problems with your code, the first one is string interpolation which is unnecessary and wrongly executed and the second is that you're referencing an inexistant prop.我看到你的代码有两个问题,第一个是字符串插值,它是不必要的并且执行错误,第二个是你引用了一个不存在的道具。 You're saying in a comment that this.props.section is undefined, that's because the data seems to be passed via the special children prop.您在评论中说this.props.section未定义,这是因为数据似乎是通过特殊的children道具传递的。

onShare = () => {
    const { children } = this.props;

    if (typeof children !== "string") {
        return;
    }

    Share.share({
        message: children,
        title: children
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM