Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Share text string using expo-sharing

I'm trying to use Share dialog to share a piece of text on Expo. Though from the current documentation I cannot find anything related to this.

Right now, my workaround is saving the text as .txt to user's device and sharing that. But surely there must be a better way-- even iOS has a native way of doing it. But how do I implement something similar with Expo's expo-sharing package?

My current workaround (modified for brevity):

import * as Sharing from 'expo-sharing';
import * as FileSystem from 'expo-file-system';

...


  const shareText = async (text) => {
    if (!await Sharing.isAvailableAsync()) {
        alert("Sharing is not available on this platform");
        return;
    }

    const fileName = FileSystem.documentDirectory + "text"+new Date().getDate()+"-"+new Date().getMonth()+"-"+new Date().getFullYear()+"_"+new Date().getHours()+"-"+new Date().getMinutes()+".txt";
    await FileSystem.writeAsStringAsync(fileName, text);

    try {
        await Sharing.shareAsync(fileName);
    } catch (error) {
        console.error("Failed sharing: ", error.message);
    }
};
like image 451
Coder Gautam YT Avatar asked May 08 '26 10:05

Coder Gautam YT


1 Answers

Update: Its possible using React Native share (documentation). Works fine in Expo.

import { Share } from 'react-native';

...

const shareText = async (text) => {
    try {
        await Share.share({
            message: text
        });
    } catch (error) {
        console.error('Error sharing:', error);
    }
};

like image 148
Coder Gautam YT Avatar answered May 10 '26 01:05

Coder Gautam YT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!