在React Native中在单个onpress上调用lib函数和另一个函数

时间:2019-07-03 18:00:25

标签: react-native

我正在使用我使用react-native share的React Native应用程序。在这里,我正在拍摄组件的屏幕快照,并且想要在任何社交平台上共享屏幕快照。但是,我想截取屏幕截图,然后将其共享给我无法执行的onpress按钮。这是我现在拥有的代码:

import {RNViewShot,captureScreen}  from "react-native-view-shot"
import Share, {ShareSheet} from 'react-native-share'

class First extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
      uri:''
    }
  }
    screenshot = () => {
      captureScreen({
        format: "jpg",
        quality: 0.8
      }) 
      .then(
        uri => this.setState({ uri: uri }),
        error => console.error("Oops, snapshot failed", error)
      );
    }
  
  onCancel() {
    console.log("CANCEL")
    this.setState({visible:false});
  }
  onOpen() {
    console.log("OPEN")
    this.setState({visible:true});
  }

  render() {
    let shareOptions = {
      title: "React Native",
      message: "Hola shareOptions",
      url: "uri",
      subject: "Share Link" //  for email
    };

    let shareImageBase64 = {
      title: "React Native",
      message: "This is from VIP live 4D",
      url: this.state.uri,
      subject: "Share Link" //  for email
    };
    
    return (
        <View>
           <Button
              onPress={() => Share.open(shareImageBase64) } >
              <Text>Share </Text>
           </Button>
          </View>
         
    );
  }
}

export default withNavigation(First)

有什么方法可以通过按钮onpress调用已经拥有的lib函数的屏幕截图功能吗?

1 个答案:

答案 0 :(得分:0)

您能尝试一下吗(从您的代码修改而来):

screenshot=()=> {
   captureScreen({
     format: "jpg",
     quality: 0.8
   }).then(uri => {
     this.setState(()=>{
       return { uri: uri };
     ), ()=>{
       const shareImageBase64 = {
         title  : "React Native",
         message: "This is from VIP live 4D",
         url    : this.state.uri,
         subject: "Share Link" //  for email
       };
       Share.open(shareImageBase64);
     };         
   );       
 };

据我所知,问题是Share.open在设置之前被赋予了“ this.state.uri”值。通过从setState回调中的“ this.state”中获取值,我们可以确保已在此处设置了该值。