Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-native-share promise rejection error

I am using shareSingle() from https://github.com/EstebanFuentealba/react-native-share to get the facebook share functionality in my application.

I wrote the code below following the documentation, but it throws:

Possible Unhandled Promise Rejection error. TypeError: undefined is not an object

[exp] undefined is not an object (evaluating '_reactNative.NativeModules.RNShare.shareSingle')

import React, {Component} from 'react';
import {StyleSheet, View} from 'react-native';
import Share, {ShareSheet, Button} from 'react-native-share';

export default class PreviewImage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            visible: false
        }
    }

    onCancel = () => {
        this.setState({visible:false});
    }

    onOpen = () => {
        this.setState({visible:true});
    }

    render() {
        let shareOptions = {
            title: "React Native",
            message: "Hola mundo",
            url: "http://facebook.github.io/react-native/"
        };

        return (
            <View style={styles.container}>
                <View style={styles.shareImageButton}>
                    <Button
                        buttonStyle={{
                            backgroundColor: "rgba(92, 99,216, 1)",
                            borderColor: "transparent",
                            borderWidth: 0,
                            borderRadius: 5
                        }}
                        onPress={() => this.onOpen()}
                        textStyle={{textAlign: 'center'}}
                        title={'Share'}/>
                </View>
                <ShareSheet
                    visible={this.state.visible}
                    onCancel={this.onCancel}
                >
                    <Button iconSrc={{ uri: FACEBOOK_ICON }}
                            onPress={
                                () => {
                                    this.onCancel();
                                    setTimeout(() => {
                                        Share.shareSingle(Object.assign({}, shareOptions, {"social": "facebook"})).catch(err => console.log(err));
                                    }, 300);
                                }
                            }
                    >
                        Facebook
                    </Button>
                </ShareSheet>
            </View>
        )
    }
}

//  facebook icon
const FACEBOOK_ICON = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADwAAAA8CAMAAAANIilAAAAAYFBMVEUAAAAAQIAAWpwAX5kAX5gAX5gAX5gAXJwAXpgAWZ8AX5gAXaIAX5gAXpkAVaoAX5gAXJsAX5gAX5gAYJkAYJkAXpoAX5gAX5gAX5kAXpcAX5kAX5gAX5gAX5YAXpoAYJijtTrqAAAAIHRSTlMABFis4vv/JL0o4QvSegbnQPx8UHWwj4OUgo7Px061qCrcMv8AAAB0SURBVEjH7dK3DoAwDEVRqum9BwL//5dIscQEEjFiCPhubziTbVkc98dsx/V8UGnbIIQjXRvFQMZJCnScAR3nxQNcIqrqRqWHW8Qd6cY94oGER8STMVioZsQLLnEXw1mMr5OqFdGGS378wxgzZvwO5jiz2wFnjxABOufdfQAAAABJRU5ErkJggg==";

const styles = StyleSheet.create({
    container: {
        flex: 1,
    },
    shareImageButton: {
        flex: 1,
        justifyContent: 'center',
    }
});

Leads here is appreciated.

Please note that Share.open() works perfectly fine for me.

My package.json

{
  "name": "TrashCan",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "react-native-scripts": "1.11.1",
    "jest-expo": "25.0.0",
    "react-test-renderer": "16.2.0"
  },
  "main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
  "scripts": {
    "start": "react-native-scripts start",
    "eject": "react-native-scripts eject",
    "android": "react-native-scripts android",
    "ios": "react-native-scripts ios",
    "test": "node node_modules/jest/bin/jest.js"
  },
  "jest": {
    "preset": "jest-expo"
  },
  "dependencies": {
    "axios": "^0.18.0",
    "expo": "^25.0.0",
    "react": "16.2.0",
    "react-native": "0.52.0",
    "react-native-elements": "^0.19.0",
    "react-native-share": "^1.0.26",
    "react-navigation": "^1.1.2",
    "react-redux": "^5.0.7",
    "redux": "^3.7.2",
    "redux-thunk": "^2.2.0",
    "react-native-animate-number": "^0.1.2"
  }
}
like image 404
Lokesh Agrawal Avatar asked Mar 03 '18 09:03

Lokesh Agrawal


1 Answers

I think you need to add RNShare.xcodeproj to your Project' Libraries and then go to General->Linked Frameworks and Libraries, add libRNShare.a. It's fixed my problem.

like image 192
Bao Truong Avatar answered Oct 10 '22 06:10

Bao Truong