-
-
Save maretekent/c8d303fc4e1c3bf93988d521a07be1d7 to your computer and use it in GitHub Desktop.
Get the base url to a react router route handler.
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 } from 'react-router' | |
import getRouteHandlerBaseUrl from 'js/helpers/get-route-handler-base-url' | |
class Something extends React.Component { | |
componentWillMount() { | |
this._baseUrl = getRouteHandlerBaseUrl(this.props) | |
} | |
render() { | |
return ( | |
<div> | |
<Link to={this._baseUrl+"/something-else"}>Link to [url to this handler]/something-else</Link> | |
</div> | |
) | |
} | |
} | |
export default Something |
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
// calculate the base url for a router handler from the props the router provides to it | |
export default function(props) { | |
if (!props.route || !props.routes || !props.location) { | |
throw new Error("Missing props from React router.") | |
} | |
var route = props.route | |
var routeDepth = props.routes.indexOf(route) | |
return props.location.pathname.split("/").slice(0, routeDepth+1).join("/") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment