Forked from shprink/Link component that handle internal or external links
Created
July 28, 2016 10:56
-
-
Save kgrz/cec03fa15c94de981322d14e82f1d0bf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import React from 'react'; | |
| import {Link as ReactLink} from 'react-router'; | |
| export default class Link extends React.Component { | |
| parseTo(to) { | |
| let parser = document.createElement('a'); | |
| parser.href = to; | |
| return parser; | |
| } | |
| isInternal(toLocation) { | |
| return window.location.host === toLocation.host; | |
| } | |
| render() { | |
| const {to, children, ...rest} = this.props; | |
| const toLocation = this.parseTo(to); | |
| const isInternal = this.isInternal(toLocation); | |
| if (isInternal) { | |
| return (<ReactLink to={toLocation.pathname} {...rest}>{children}</ReactLink>); | |
| } else { | |
| return (<a href={to} target="_blank" {...rest}>{children}</a>); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment