Open link in current window react-native Linking

1.3k Views Asked by At

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;
0

There are 0 best solutions below