Avoid Concat URL in React-Redux-Router

3.3k views Asked by At

I am using react-router + react-router-redux in my app. What I want to achieve is every time user click a tab in the dashboard it will change the URL to /dashboard/messages, for instance.

However, using push('dashboard/' + page), every time I click a tab it will append the link.

My current situation:

  1. Current URL is /dashboard.
  2. Click a Messages tab.
  3. The URL changes into /dashboard/messages.
  4. Click a Home tab.
  5. The URL changes into /dashboard/messages/dashboard/home.

Is there any way to just replace the /dashboard/messages into /dashboard/home when we click Home tab after cliked the Messages tab?

Many thanks!

3

There are 3 answers

0
Bagus Trihatmaja On

I found the silly mistake, sorry for this.

So apparently it has to be push('/dashboard/' + page) instead of push('dashboard/' + page).

1
Kinnza On

use this

push('/dashboard/' + page)
0
adriaanbd On

If you're coming in here from a react-router-dom perspective, please note that <Link to='stringUrl' /> concatenates whatever you place on the to attribute to the current path. So, if you are currently on /home path, it will produce /home/StringUrl as a result.

However, if you place a forward slash / instead, i.e. <Link to='/stringUrl' /> it won't do that, thus producing /StringUrl path.