我找到了this我想用它来从 url 获取标题的模块。 问题是我不知道如何访问该模块为我提供的数据。
基本上我想从给定的网址获取标题并将其打印在 View 中。
最佳答案
您可以使用该模块的getPreview方法获取标题。 例如,如果您想获取 YouTube 视频的标题,请使用以下代码:
import * as React from 'react';
import { Text, View } from 'react-native';
import LinkPreview from 'react-native-link-preview';
export default class App extends React.Component {
constructor(props) {
super(props);
this.state = {
title : "sample title"
}
}
getTitle() {
LinkPreview.getPreview('https://www.youtube.com/watch?v=aqz-KE-bpKQ')
.then(data => this.setState({title: data.title}));
}
componentDidMount() {
this.getTitle();
}
render() {
return (
<View>
<Text>Title is : {this.state.title}</Text>
</View>
);
}
}
希望这有帮助!
关于javascript - 如何使用react-native-link-preview从url获取标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52127864/