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.
Comments
Post a Comment