react-native-share / react-native-share

Social share, sending simple data to other apps.
https://react-native-share.github.io/react-native-share
MIT License
3.61k stars 935 forks source link

You attempted to set the key 'social' with the value #134

Closed ahyong87 closed 7 years ago

ahyong87 commented 7 years ago

Hi,

i get this error, You attempted to set the key 'social' with the value "twitter" on an object that is meant to be immutable and has been frozen.

screen shot 2017-06-28 at 2 27 33 pm

ataillefer commented 7 years ago

The problem comes from the SharePage.js file at line 78 which is probably part of your app since it isn't part of react-native-share nor react-native.

ahyong87 commented 7 years ago

i solve the problem already.

const shareOptions = { title: this.state.ShareTitle, message: this.state.ShareMessage, url: this.state.ShareUrl, subject: this.state.ShareSubject, // for email social: this.state.ShareType };

Share.shareSingle(shareOptions);

kenvandemar commented 7 years ago

What's content of ShareType did you declare on this.state? I'm also facing the same issues like you, I found solution which similars yours, it works well on iPhone but not iPad.

ahyong87 commented 7 years ago

@kenvandemar ShareType i pass twitter,facebook,whatsapp

handleFacebookButtonOnClicked(){ this.setstate({ShareType:'facebook'}) setTimeout(() => { this.CallShareFunction(); },300); }

handleTwitterButtonOnClicked(){ this.setstate({ShareType:'twitter'}) setTimeout(() => { this.CallShareFunction(); },300); }

CallShareFunction(){ const shareOptions = { title: this.state.ShareTitle, message: this.state.ShareMessage, url: this.state.ShareUrl, subject: this.state.ShareSubject, // for email social: this.state.ShareType };

Share.shareSingle(shareOptions); }

kenpaxtonlim commented 7 years ago

whatsapp image 2017-07-10 at 5 05 18 pm

I have the same error as well. Is there a specific fix for this?

export default class SocialShare extends Component{

  constructor(props){
    super(props)

    this.shareOptions = {
      title: "Promotion",
      message: "Pasta on 20% discount, valid for the rest of time!",
      url: "http://facebook.github.io/react-native/",
      subject: "Promotion" //  for email
    }
  }

  _share = (type) => {
    switch(type){
      case 'phone': //take screenshot
        break;
      case 'wechat': //current module do not support
        break;
      default:
      Share.shareSingle(Object.assign(this.shareOptions, {"social": type})).catch(err => console.log(err))
    }
  }

  render(){
    return(
      <View style={styles.container}>
        <Icon.Button
          name='facebook-official'
          backgroundColor="#3b5998"
          onPress={() => this._share('facebook')}
        >
          Share on Facebook
        </Icon.Button>

        <Icon.Button
          name='twitter-square'
          backgroundColor="#0084b4"
          onPress={() => this._share('twitter')}
        >
          Share on Twitter
        </Icon.Button>

        <Icon.Button
          name='whatsapp'
          backgroundColor="#25d366"
          onPress={() => this._share('whatsapp')}
        >
          Share on WhatsApp
        </Icon.Button>

        <Icon.Button
          name='envelope'
          backgroundColor="#0084b4"
          onPress={() => this._share('email')}
        >
          Share on Email
        </Icon.Button>

        <Icon.Button
          name='mobile'
          backgroundColor="black"
          onPress={() => this._share('phone')}
        >
          Save to Phone
        </Icon.Button>

        <Icon.Button
          name='plus'
          backgroundColor="gray"
          onPress={() => Share.open(this.shareOptions).catch(err => console.log(err))}
          >
            Share more...
          </Icon.Button>
      </View>
    )
  }
}
hungdev commented 7 years ago

@kenpaxtonlim i get same error

hungdev commented 7 years ago

i solve the problem. it's error when assign object. It can't set key 'social' when you share a second time. i fixed it like this:

import React, { Component } from 'react'
import {
  Clipboard,
  ToastAndroid,
  AlertIOS,
  Platform
} from 'react-native'
import Share, { ShareSheet, Button } from 'react-native-share'

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

  componentWillMount () {
    this.setState({ shareOptions: this.props.shareOptions })
  }

  onCancel () {
    console.log('CANCEL')
    this.setState({ visible: false })
  }
  onOpen () {
    console.log('OPEN')
    this.setState({ visible: true })
  }
  render () {
    return (
      <ShareSheet visible={this.state.visible} onCancel={this.onCancel.bind(this)}>
        <Button iconSrc={{ uri: TWITTER_ICON }}
          onPress={() => {
            this.onCancel()
            setTimeout(() => {
              Share.shareSingle({
                title:this.state.shareOptions.title,
                message: this.state.shareOptions.message,
                url: this.state.shareOptions.url,
                subject: this.state.shareOptions.subject,
                social: 'twitter'
              })
            }, 300)
          }}>Twitter</Button>
        <Button iconSrc={{ uri: FACEBOOK_ICON }}
          onPress={() => {
            this.onCancel()
            setTimeout(() => {
              Share.shareSingle({
               title:this.state.shareOptions.title,
                message: this.state.shareOptions.message,
                url: this.state.shareOptions.url,
                subject: this.state.shareOptions.subject,
                social: 'facebook'
              })
            }, 300)
          }}>Facebook</Button>
        <Button iconSrc={{ uri: WHATSAPP_ICON }}
          onPress={() => {
            this.onCancel()
            setTimeout(() => {
              Share.shareSingle({
                title:this.state.shareOptions.title,
                message: this.state.shareOptions.message,
                url: this.state.shareOptions.url,
                subject: this.state.shareOptions.subject,
                social: 'whatsapp'
              })
            }, 300)
          }}>Whatsapp</Button>
        <Button iconSrc={{ uri: GOOGLE_PLUS_ICON }}
          onPress={() => {
            this.onCancel()
            setTimeout(() => {
              Share.shareSingle({
                title:this.state.shareOptions.title,
                message: this.state.shareOptions.message,
                url: this.state.shareOptions.url,
                subject: this.state.shareOptions.subject,
                social: 'googleplus'
              })
            }, 300)
          }}>Google +</Button>
        <Button iconSrc={{ uri: EMAIL_ICON }}
          onPress={() => {
            this.onCancel()
            setTimeout(() => {
              Share.shareSingle({
                title:this.state.shareOptions.title,
                message: this.state.shareOptions.message,
                url: this.state.shareOptions.url,
                subject: this.state.shareOptions.subject,
                social: 'email'
              })
            }, 300)
          }}>Email</Button>
        <Button
          iconSrc={{ uri: CLIPBOARD_ICON }}
          onPress={() => {
            this.onCancel()
            setTimeout(() => {
              if (typeof this.props.shareOptions['url'] !== 'undefined') {
                Clipboard.setString(this.props.shareOptions['url'])
                if (Platform.OS === 'android') {
                  ToastAndroid.show('Link copiado al portapapeles', ToastAndroid.SHORT)
                } else if (Platform.OS === 'ios') {
                  AlertIOS.alert('Link copiado al portapapeles')
                }
              }
            }, 300)
          }}>Copy Link</Button>
      </ShareSheet>
    )
  }
}
kenpaxtonlim commented 7 years ago

@hungdev May I know which part exactly is the fix?

hungdev commented 7 years ago

Dont use assign and write like me. Look my shareSingle()

kenpaxtonlim commented 7 years ago

@hungdev Thank you very much, it works! :)

pigflymoon commented 7 years ago

Change Share.shareSingle(Object.assign(this.shareOptions, {"social": type})).catch(err => console.log(err)) to Share.shareSingle(Object.assign({},shareOptions, { "social": "email" })).catch(err => console.log(err)); Props are already frozen in development mode, assign properties of several objects on to a new target object, so we don't mutate any exist data.

agrawalo commented 6 years ago

I am still getting this error

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

My code https://stackoverflow.com/questions/49082623/react-native-share-promise-rejection-error