Does react-router support relative links?

后端 未结 6 1358
不思量自难忘°
不思量自难忘° 2020-12-29 04:40

I\'m on this url:

/path1/path2

Then:


With this code the address in the

6条回答
  •  生来不讨喜
    2020-12-29 05:12

    in react-router v4 relative paths are supported. The history library resolves relative paths 'just like a browser would' so that a

    
    

    or a

    history.push({ pathname: '..', search: 'foo=bar' });
    

    at the url site/path/one will redirect to site.com/path/two and site.com/three?foo=bar.

    However NavLink does not work with relative pathnames because it doesn't resolve it's own relative path (which is nontrivial to do). However there are some community packages for this, but I haven't tested them.

    Alternatively, and this is what I do, you can get a match object from your parent Route (or from the context router.route.match) and use it to build your relative to in NavLink:

    
    

    this way your component can work independent of it's parent Route(s).

提交回复
热议问题