D
D
denis_213213212020-09-25 15:17:24
React
denis_21321321, 2020-09-25 15:17:24

When using NavLink, the path is put on top of another?

Clicking e.g. on message changes the path correctly ( beta-site/modernWelcome/messages ), but if you click on friends instead of becoming beta-site/modernWelcome/friends it changes to beta-site/modernWelcome/modernWelcome/ friends if done then it changes correctly but I need with modernWelcome . The page is on the path beta-site/modernWelcome . after<NavLink to='/message'>Message</NavLink>

5f6ddf27cdcd6243364537.png5f6ddf31b10d2266733281.png

const App = (props) => {
    return (
        <Router>
            <div>
              <Route exact path='/modernWelcome/friends' render={() => <Friends friends={props.state.friends}/>}/>
              <Route exact path='/modernWelcome/messages' render={() => <Messages messages={props.state.messages}/>}/>
              <NavLink to='modernWelcome/messages'>Message</NavLink>
              <NavLink to='modernWelcome/friends'>Friends</NavLink>
            </div>
        </Router>

    )
};

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Z
Zhanna_K, 2020-09-25
@Zhanna_K

Why use Route and Navlink on the same level?

const App = () => {
  return (
    <div>
      <Route exact path='/modernWelcome' component={MainPage} />
      <Route exact path='/modernWelcome/messages' component={Messages} />
     <Route exact path='/modernWelcome/friends' component={Friends} />
    </div>
  );
};

And in the MainPage component there will already be navigation links
const MainPage = (props) => {
  return (
    <div>
      <h1>MainPage</h1>
      <NavLink to='modernWelcome/messages'>Message</NavLink>
      <NavLink to='modernWelcome/friends'>Friends</NavLink>
    </div>
  );
};

Everything should work as it should.

K
Kirill Makarov, 2020-09-26
@kirbi1996

Navlink is used if I'm not mistaken in the menu so that it can be highlighted, although this can be achieved with other methods. And at the same level as routes, routes should be used. To bypass matches, you can add the exact attribute

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question