javascript - Programmatic relative links with react-router -


did manage find way how programmatically navigate relative link react-router?

i've used react-router-relative-links declarative links, can't find way programmatically.

i know done manually resolve-pathname , router.push(…), require access location, available on route handler components. component deep down tree, therefore i'd avoid wiring top.

is window.location.pathname right way location , use router.push available through withrouter?

currently i'm using utility function:

import resolvepathname 'resolve-pathname';  function getpathnamefromrelativelocation(relativelocation) {   let {pathname} = window.location;   let basepath = pathname.endswith('/') ? pathname : pathname + '/';   return resolvepathname(relativelocation, basepath); } 

and in event handler of react component:

// current path `/books/123` let path = getpathnamefromrelativelocation('write-review'); this.props.router.push(path); // current path `/books/123/write-review` 

react-router 3.0 provides location on context it's easy take there.

https://github.com/reacttraining/react-router/issues/3325


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -