You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

React Native Expo Sharing二次分享报错:存在待处理请求求助

解决Expo Sharing重复点击报错问题

问题根源

报错提示“Another share request is being processed now”,是因为首次分享的异步流程尚未完全终止(哪怕你已经回到应用),就触发了第二次shareAsync调用,导致系统判定存在并发请求冲突。

修复方案

通过添加状态变量跟踪分享状态,防止重复触发请求:

  • 新增isSharing状态标记当前是否正在执行分享操作
  • 触发分享前先检查状态,若正在分享则直接返回
  • 分享操作开始时标记状态为true,操作结束(成功/失败)后重置为false

完整修复代码

import ViewShot from "react-native-view-shot";
import * as Sharing from "expo-sharing";
import React, { useRef, useState } from "react";
import { TouchableOpacity, Image, StyleSheet } from "react-native";

const YourComponent = () => {
  const viewShot = useRef();
  const [isSharing, setIsSharing] = useState(false);

  const captureAndShareScreenshot = async () => {
    if (isSharing) return;

    try {
      setIsSharing(true);
      const uri = await viewShot.current.capture();
      console.log("捕获到截图URI:", uri);
      
      const options = {
        mimeType: 'image/jpeg',
        dialogTitle: "Check Awesome quotes ...",
      };

      await Sharing.shareAsync(`file://${uri}`, options);
    } catch (error) {
      console.error("分享失败:", error);
    } finally {
      setIsSharing(false);
    }
  };

  return (
    <ViewShot style={styles.container} ref={viewShot} options={{ format: "jpg", quality: 0.9 }}>
      <TouchableOpacity 
        style={styles.bubbleIcons} 
        onPress={captureAndShareScreenshot}
        disabled={isSharing} // 可选:禁用按钮避免重复点击
      >
        <Image source={require("./assets/share.png")} style={styles.iconImage} />
      </TouchableOpacity>
    </ViewShot>
  );
};

const styles = StyleSheet.create({
  container: {},
  bubbleIcons: {},
  iconImage: {}
});

export default YourComponent;

优化说明

  • 改用async/await替代嵌套then,让异步流程更易读和维护
  • finally块确保无论分享成功或失败,状态都会被重置,避免后续无法触发分享
  • 给按钮添加disabled属性,从UI层面防止用户重复点击,提升体验

内容的提问来源于stack exchange,提问作者armin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 21:33:17