微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

当用户单击共享按钮时,我想共享此 JSON 数据

如何解决当用户单击共享按钮时,我想共享此 JSON 数据

这是 JSON 数据

{
  "sad": [
    { "id": "1","title": "hello this is no.1" },{
      "id": "2","title": "आँखें थक गई है आसमान को देखते देखते पर वो तारा नहीं टूटता,जिसे देखकर तुम्हें मांग लूँ"
    },{
      "id": "3","title": "मेरी हर आह को वाह मिली है यहाँ…..,कौन कहता है दर्द बिकता नहीं है !"
    },{
      "id": "4","title": "तुझसे अच्छे तो जख्म हैं मेरे । उतनी ही तकलीफ देते हैं जितनी बर्दास्त कर सकूँ"
    },{
      "id": "5","title": "रुठुंगा अगर तुजसे तो इस कदर रुठुंगा की ये तेरीे आँखे मेरी एक झलक को तरसेंगी !!"
    },{
      "id": "6","title": "बेवफा लोग बढ़ रहे हैं धीरे धीरे,इक शहर अब इनका भी होना चाहिए"
    },{
      "id": "7",{
      "id": "8",{
      "id": "9",इक शहर अब इनका भी होना चाहिए"
    }
  ]
}

这是我的代码,我想知道当用户按下共享按钮时我应该怎么做才能共享此特定数据

这里我导入了所有的库

import React,{ Component,useState,useEffect } from "react";
import {
  StyleSheet,View,Text,FlatList,ListView,TouchableOpacity,Share,} from "react-native";
import Icon from "react-native-vector-icons";
import SadJson from "./sad.json";

var d = SadJson.sad;

const sad = ({ navigation }) => {
  const [data,setData] = useState([]);

  // This is share method code
  const onShare = async () => {
    try {
      const result = await Share.share({
        message: data.toString(d),});
    } catch (error) {
      alert(error.message);
    }
  };

  useEffect(() => {
    setData(d);
  });

  // This is return section where I use Flatlist for show Data
  return (
    <View style={styles.container}>
      <FlatList
        data={data}
        removeClippedSubviews={false}
        renderItem={({ item }) => (
          <View>
            <Text>
              {item.id}
              {item.title}
            </Text>

            <View style={{ flexDirection: "row" }}>
              <TouchableOpacity style={styles.sharebtn} onPress={onShare}>
                <Text>Share{item.id}</Text>
              </TouchableOpacity>
              <TouchableOpacity style={styles.sharebtn}>
                <Text>copy</Text>
              </TouchableOpacity>
            </View>
          </View>
        )}
      />
    </View>
  );
};

// This is Style section
const styles = StyleSheet.create({
  container: {
    flex: 1,alignItems: "center",},sharebtn: {
    width: 60,height: 30,padding: 5,backgroundColor: "#2BDB25",borderRadius: 10,btntext: {
    textAlign: "center",});

export default sad;

请帮助我,请让我知道如何在社交媒体或任何其他应用程序上分享这些数据

解决方法

如果我正确理解您的问题,您希望共享来自 data 状态的特定数据元素。要将特定对象从 data 传递到 Share.share,我建议您将 整个 item 对象从地图回调传递给 onShare 处理程序在 FlatList 中。

更新 onShare 以使用 item 对象。此外,由于您实际上并未使用等待的返回值,因此该函数可能不需要为 async(如果您确实使用 await 值以供稍后使用,则可以将其添加回来)。

const onShare = item => {
  try {
    const result = await Share.share({
      message: item,});
  } catch (error) {
    alert(error.message);
  }
};

并将 item 传递给回调

<TouchableOpacity
  style={styles.sharebtn}
  onPress={() => onShare(item)} // <-- pass item to callback
>
  <Text>Share{item.id}</Text>
</TouchableOpacity>

另一个建议 - 由于您的数据是静态的,因此不需要从 useEffect 挂钩填充您的状态,尤其是因为该效果没有任何依赖项和更新状态,除非您有副本/粘贴问题,正如所写的,这个效果看起来应该触发渲染循环。您可以使用初始值填充您的状态。

const d = SadJson.sad;
...
const [data,setData] = useState(d); // <-- initialize state with the imported JSON data

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。