How to use share API in react native
How to use share API in react native
- First install the community package known as react-native-share
command: yarn add react-native-share
- keep mind that React-native-share is promise based. So so you can use async await or use then,catch
- Now simply import it and use it
import Share from 'react-native-share'; Share.open(options) .then((res) => { console.log(res); }) .catch((err) => { err && console.log(err); });
- Options reference : https://react-native-share.github.io/react-native-share/docs/share-open#supported-options
- Example of My use case: Sharing the screenshot via whatsapp from share api.
Here urlString is a image path returned from view-shot plugin.
Title: the title you want in whatsapp message. Usually not required.
Message: the message u want to use on sharing the image.
Resources:
- For the other options, refer to the docs https://react-native-share.github.io/react-native-share/docs/share-open
- Use of view shot with share https://www.gkmit.co/blog/mobile-development/capture-and-share-screenshot-in-react-native