D
D
Daniil2018-06-25 21:37:17
React
Daniil, 2018-06-25 21:37:17

How can I adequately solve my problem in React Router?

Good evening.
The problem is this:
Let's take Twitter as a basis.
We have a route and a redirect in App.js that serve as the starting point for our routing. Inside the Profile component, everything happens that a regular user has on Twitter. In terms of links the same. And then the question creeps up,
ddfa01ca17d6.png
If we click, we will follow the link twitter.com/username Which
means that our version with a redirect will not work.
How can I make it so that when selecting a user and going to his profile, the Profile component continues to be displayed, but with the url of the new user?
Help me please!

<BrowserRouter>
      <React.Fragment>
        <Nav />
        <Switch>
          <Route path="/EveryInteract" component={Profile} />
          <Redirect exact from="/" to="/EveryInteract" />
        </Switch>
      </React.Fragment>
    </BrowserRouter>

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Spirin, 2018-06-25
@TchernyavskD

More or less like this:

<Switch>
  <Route path="/:user_slug" component={Profile} />
  {me && <Redirect exact from="/" to={'/' + me.slug} />}
</Switch>

me is your authorized user. If the user is authorized, then me exists and the root path is redirected to his page.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question