Ad

Share Images To Social Media Using React Native

I am developing a react native app. I want to share the selected images which I fetch from firebase storage and listed in the app to social media like WhatsApp. For that, I am using an npm package called

react-native-share

. Using that I was able to share text but no image. the official page is telling that I should convert the image first to base64 and I have done that, and the app started to crash. can anyone please tell me how to do it.

Ad

Answer

You no longer need to use react-native-share. Use the native Share component.

check this post: React Native - can we share an image and text into whatsapp?

Here you can find an example of code:

import React, { Component } from 'react';
import {
  Share,
  Text,
  TouchableOpacity
} from 'react-native';

const shareOptions = {
  title: 'Title',
  message: 'Message to share', // Note that according to the documentation at least one of "message" or "url" fields is required
  url: 'www.example.com',
  subject: 'Subject'
};

export default class ShareExample extends React.Component {

  onSharePress = () => Share.share(shareOptions);

  render(){
    return(
      <TouchableOpacity onPress={this.onSharePress} >
        <Text>Share data</Text>
      </TouchableOpacity>
    );
  }
}

Finally you have to options to send the image + text message: - You can use the url field of the shareOptions adding the remote URI of the image so it can be previewed in the WhatsApp message, and the title or subject fields to add the text. - You can share a base64 file url like this: url: 'data:image/png;base64,'

Ad
source: stackoverflow.com
Ad