1

I created a dynamic menu that is accessible to both public and registered users. The problem is that I have this:

{pages.length > 0 ? (
  pages.map(page => (
    <li key={page._id} className="nav-item">
      {page._id ? (
        <Link to={`pages/${page._id}`} className="nav-link">{page.title}</Link>
      ) : (
          <Link to={`pages/${page._id}`} className="nav-link">Hola 2</Link>
        )}
    </li>
  ))
) : (
    <li className="nav-item">
      <a className="nav-link" href="#!">No Pages Found</a>
    </li>
  )}

Now when I'm in the homepage and a click a URL, the links works great:

localhost:3000/pages/5d2ea3a1ef0ba93f9cd1b980

Once I'm inside an URL, I try to click a different link and suddenly it now behaves like this:

localhost:3000/pages/pages/5d2ea3a1ef0ba93f9cd1b980

and so on:

localhost:3000/pages/pages/pages/5d2ea3a1ef0ba93f9cd1b980

Does anyone has an idea why is this happening?

2
  • 1
    try with <Link to={`/pages/${page._id}`} className="nav-link">Hola 2</Link> Commented Jul 17, 2019 at 6:21
  • I can not believe it was so easy to solve, thank you!. I'm going to mark your comment as useful as you did not publish an answer. Commented Jul 17, 2019 at 6:33

1 Answer 1

1

Use to={/pages/${page._id}} instead of to={pages/${page._id}}. If the link doesn't start with a /, it is treated as relative to current url.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.