在当前窗口中打开链接react-native链接

发布于 2025-01-12 23:15:14 字数 1341 浏览 3 评论 0原文

我正在尝试将react-native 用于网络\应用程序。我想创建一个到不同屏幕的简单链接,但使用 url 而不是元素。

我设法让它与 2 个烦人的空洞一起工作:

  1. 网址的尾部显示一秒钟,然后从浏览器网址输入中消失(即使 window.location.hrefLinking .getInitialURL() 确实返回完整链接,
  2. 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:

  1. the tail of the url is showing for a second and then disappears from the browser url input (even though window.location.href and Linking.getInitialURL() does return the full link
  2. 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:
enter image description here

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

卖梦商人 2025-01-19 23:15:14

您可以将第二个参数传递给 openURL() 并将 _self 作为值

传递

`openURL("your url here", "_self")`

You can pass second parameter to openURL() and pass _self as the value

Something like this

`openURL("your url here", "_self")`
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文