在当前窗口中打开链接react-native链接
我正在尝试将react-native 用于网络\应用程序。我想创建一个到不同屏幕的简单链接,但使用 url 而不是元素。
我设法让它与 2 个烦人的空洞一起工作:
- 网址的尾部显示一秒钟,然后从浏览器网址输入中消失(即使
window.location.href
和Linking .getInitialURL()
确实返回完整链接, Linking.openURL
仅打开新选项卡,我希望它在单击时保留在同一选项卡中
屏幕 1:
import { Linking, StyleSheet, Text, View } from "react-native";
import React from "react";
import WebView from "react-native-webview";
const Infromation = () => {
const uri = "links";
return (
<Text
onPress={() => {
Linking.openURL("feed");
}}
>
AboutReact
</Text>
);
};
export default Infromation;
const styles = StyleSheet.create({});
Feed 屏幕代码:
import {
KeyboardAvoidingView,
Linking,
StyleSheet,
Text,
TextInput,
TouchableOpacity,
View,
} from "react-native";
import React, { useEffect } from "react";
const Feed = () => {
const url = new URL(window.location.href);
Linking.getInitialURL().then((url) => {
console.log(url);
});
return (
<Text>{url.href}</Text>
);
};
export default Feed;
I'm trying to use react-native for a web\app. I would like to create a simple link to a different screen, but using urls and not elements.
I managed to get it to work with 2 annoying caviats:
- the tail of the url is showing for a second and then disappears from the browser url input (even though
window.location.href
andLinking.getInitialURL()
does return the full link - the
Linking.openURL
only opens new tabs, and I would like it to stay in the same tab when clicking.
Screen 1:
import { Linking, StyleSheet, Text, View } from "react-native";
import React from "react";
import WebView from "react-native-webview";
const Infromation = () => {
const uri = "links";
return (
<Text
onPress={() => {
Linking.openURL("feed");
}}
>
AboutReact
</Text>
);
};
export default Infromation;
const styles = StyleSheet.create({});
That link opens the feed screen, but in a new tab and url tail not showing:
Feed screen code:
import {
KeyboardAvoidingView,
Linking,
StyleSheet,
Text,
TextInput,
TouchableOpacity,
View,
} from "react-native";
import React, { useEffect } from "react";
const Feed = () => {
const url = new URL(window.location.href);
Linking.getInitialURL().then((url) => {
console.log(url);
});
return (
<Text>{url.href}</Text>
);
};
export default Feed;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将第二个参数传递给
openURL()
并将_self
作为值传递
You can pass second parameter to
openURL()
and pass_self
as the valueSomething like this