Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
123 views
in Technique[技术] by (71.8m points)

reactjs - React router taking 'new' as id

I am using ReactRouter in my react project. I am using routing like this:

  <Switch>
    <Route exact path="/users/new">
      <CreateUser />
    </Route>
    <Route exact path="/users/:userId">
      <Profile />
    </Route>
  <Switch>

Here I am facing problem when I am trying to load localhost:3000/users/new the <Profile /> component load as well as <CreateUser /> component and show error 'new' is not a valid userId. Without changing the path how can I solve this?

question from:https://stackoverflow.com/questions/66059538/react-router-taking-new-as-id

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should change the order of routes. Route with users/new should come first, followed by users/:userID. In your case router interprets users/new as users/:userID


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...