Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

reactjs - How to change URL path in react-router-dom?

I use several route in my app And I have conditions for bringing information in that route. I created an array and the array values are created according to those conditions. Part of my code:

  for (var i = 0; i < res.length; i++){
                var arr = res[i];
                switch (arr) {                 
                    case "MsgManagementTab":
                        routearray.push({path :"/main/messages" ,component:<Messages isSidebarActive={this.state.isSidebarActive} />})     
                      break;
                    case "AlgorithmManagementTab":
                        routearray.push({path :"/main/algorithms",component:<Algorithm isSidebarActive={this.state.isSidebarActive}/>})
                      break;
                    case "SocialNetworkManagementTab":
                        routearray.push({path :"/main/socialNetworks",component:<SocialNetWork isSidebarActive={this.state.isSidebarActive}/>})
                  }
               }

and :

   <Switch>
     {this.state.routearray ?this.state.routearray.map(item=>{
return <Route path={item.path}>{item.component}</Route>
   </Switch>   

In this case, for example, I have access to the Route of the message when the MsgManagementTab exists. What I want to do is if the MsgManagementTab does not exist, and if the user wants to go to the page via URL, go to the path I want to be redirected to. This is the path I want to redirected :

 <Route path="/main/access/deny"><NoAccess  isSidebarActive={this.state.isSidebarActive} /></Route>

Someone knows how to do this?

question from:https://stackoverflow.com/questions/65867553/how-to-change-url-path-in-react-router-dom

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

1 Reply

0 votes
by (71.8m points)

If I understand your question/issue, you want the app to redirect from "/main/messages" to "/main/access/deny" when the Switch isn't rendering the Route for "/main/messages".

Solution

After the mapped routes include a Redirect component to redirect from a specific path to another path. This works by allowing the Switch to match the Route for "/main/messages" first if it exists, otherwise since that route won't be available the Redirect will handle it.

Redirect from

<Switch>
  ...

  {this.state.routearray ? this.state.routearray.map(item => (
    <Route path={item.path}>{item.component}</Route>
  ))}

  ...

  <Redirect from="/main/messages" to="/main/access/deny />

  ...

  <Route path="/main/access/deny">
    <NoAccess isSidebarActive={this.state.isSidebarActive} />
  </Route>

  ...
</Switch>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...