Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I open an external link in new tab in react native?

I am doing:

import React from 'react';
import PropTypes from 'prop-types';
import { TouchableOpacity, Linking } from 'react-native';

/**
 * Used to create external link to other websites
 */
class ExternalLink extends React.Component {
  _openLink = async () => {
    const { href } = this.props;
    if (await Linking.canOpenURL(href)) {
      await Linking.openURL(href);
    }
  };

  render() {
    const { href, children, ...rest } = this.props;
    return (
      <TouchableOpacity
        accessibilityRole="link"
        onPress={this._openLink} // eslint-disable-line no-underscore-dangle
        href={href}
        {...rest}
      >
        {children}
      </TouchableOpacity>
    );
  }
}

ExternalLink.propTypes = {
  /** External URL */
  href: PropTypes.string.isRequired,
  /** Any node */
  children: PropTypes.node.isRequired,
};

export default ExternalLink;

However, this open in current tab, is there a way to open the URL in a new tab?

like image 501
Dimitri Kopriwa Avatar asked Apr 14 '20 00:04

Dimitri Kopriwa


People also ask

How do I open an external URL in react native?

Try this: import React, { useCallback } from "react"; import { Linking } from "react-native"; OpenWEB = () => { Linking. openURL(url); }; const App = () => { return <View onPress={() => OpenWeb}>OPEN YOUR WEB</View>; }; Hope this will solve your problem.

How do you add an external link in React?

To add an external link with React Router, we can set the to prop to an object with the pathname property set to the external URL we go to when the link is clicked. to set the to prop to { pathname: "https://example.com" } to go to https://example.com when we click on the link.

How do you open a component in new window in React?

To open a component in new window on a click in React, we can call window. open with the element we created. to create the RenderInWindow component that opens a new window when it mounts. To do this, we create the container state with useState .


1 Answers

I guess there are two primary options:

You can use only the window.open() function in the web:

if(Platform.OS == 'web'){
   window.open(url, '_blank');
} else {
   Linking... // normal Linking react-native
}

Or you can create a custom Linking from the react-native-web (https://github.com/necolas/react-native-web/blob/master/packages/react-native-web/src/exports/Linking/index.js):

CustomLinking.js :

import { canUseDOM } from 'fbjs/lib/ExecutionEnvironment';
import invariant from 'fbjs/lib/invariant';

const initialURL = canUseDOM ? window.location.href : '';

const CustomLinking = {
  addEventListener() {},
  removeEventListener() {},
  canOpenURL(): Promise<boolean> {
    return Promise.resolve(true);
  },
  getInitialURL(): Promise<string> {
    return Promise.resolve(initialURL);
  },
  openURL(url: string, target = '_self'): Promise<Object | void> {
    try {
      open(url, target);
      return Promise.resolve();
    } catch (e) {
      return Promise.reject(e);
    }
  },
  _validateURL(url: string) {
    invariant(typeof url === 'string', 'Invalid URL: should be a string. Was: ' + url);
    invariant(url, 'Invalid URL: cannot be empty');
  }
};

const open = (url, target) => {
  if (canUseDOM) {
    window.open(
      url,
      target // <- This is what makes it open in a new window.
    );
  }
};

export default CustomLinking;

Usage:

import Linking from "./CustomLinking";

Linking.openURL(href, '_blank');
like image 153
Eduardo Ramos Avatar answered Sep 28 '22 03:09

Eduardo Ramos