【发布时间】:2018-10-19 01:42:22
【问题描述】:
我创建了一个分享图标,点击分享的 pdf 或图像文件在社交帐户(如 facebook、whatsapp、gmail 等)上。我想在类组件中传递可共享文件的 URL 链接,但出现错误。如果我对 URL 进行硬编码,那么它可以正常工作,但是如何传递从反应导航收到的 URL?
工作代码:
import React, { Component } from 'react';
import { Share, View, TouchableOpacity } from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';
const shareOptions = {
title: 'Download Brochure',
url: 'https://cpbook.propstory.com/upload/project/brochure/5bc58ae6ca1cc858191327.pdf'
}
export default class Screen extends Component {
onSharePress = () => Share.share(shareOptions);
render() {
return (
<View>
<Text style={styles.infoTitle}>Share: </Text>
<TouchableOpacity onPress={this.onSharePress}>
<Icon style={{paddingLeft: 10, paddingTop: 10}}name="md-share" size={30} color="black"/>
</TouchableOpacity>
</View>
}
}
上面的代码工作正常,但下面的代码给出错误,说shareOptions 未定义。我怎样才能克服这个问题?我想在shareOptions 中传递文件URL。我从反应导航道具中获取文件网址,即brochure。
代码:
import React, { Component } from 'react';
import { Share, View, TouchableOpacity } from 'react-native';
import Icon from 'react-native-vector-icons/Ionicons';
export default class Screen extends Component {
onSharePress = () => Share.share(shareOptions); <---Getting error here shareOptions undefined
render() {
const project = this.props.navigation.getParam('project', null);
let { brochure } = project;
const shareOptions = {
title: 'Download Brochure',
url: brochure
}
return (
<View>
<Text style={styles.infoTitle}>Share: </Text>
<TouchableOpacity onPress={this.onSharePress}>
<Icon style={{paddingLeft: 10, paddingTop: 10}}name="md-share" size={30} color="black"/>
</TouchableOpacity>
</View>
)
}
如何克服上述问题以及如何传递道具,即上述情况下的文件 URL,URL 位于 brochure 道具中。
【问题讨论】:
标签: javascript reactjs react-native