简体   繁体   English

如何根据 react-router-dom 单击的链接更改组件

[英]How to change a component on the basis of the link clicked by react-router-dom

Please have a look at this code and tell me if you can help me on how can I fix it,请查看此代码并告诉我您是否可以帮助我解决此问题,

import React from 'react';
import  {Switch, Route, BrowserRouter as Router, Link} from "react-router-dom";




function App(){
return(
    <Router>
        <div className = "sidebar">
            <Link to = "/"><h1 className = "heading ">TODOS</h1></Link>
            <Link to= "/login"> <span className = "links login">LogIn</span></Link>
            <Link to = "/signup"><span className = "links signup">SignUp</span></Link>
            <Link to = "contact"><span className = "links contact">Contact Us</span></Link>
            <Link to = "/about"><span className = "links about">About Developer</span></Link>
        </div>
        <Switch>
            <Route path = "/">
                <h1>home route</h1>
            </Route>
            <Route path = "/login">
                {/* <Login/> */}
                <div>
                 <h1>this is login route</h1>
                </div>
            </Route>    
            <Route path = "/signup">
                <h1>signup page</h1>
            </Route>
            <Route path = "/contact">
                <h1>contact page</h1>
            </Route>
            <Route path = "/about">
                <h1>About page</h1>
            </Route>
        </Switch>
    </Router>
)
}

export default App;

NMw doesn't matter which links you click it shows the same text how can I fix this?? NMw 无论您单击哪个链接,它都会显示相同的文本我该如何解决这个问题? For testing and a better understanding of question you guys can please go on this link here and just copy-paste my code and it runs there and please let me know my mistake为了测试和更好地理解问题,你们可以在链接上请 go 并复制粘贴我的代码并在那里运行,请让我知道我的错误

Just add exact prop to all Route Component.只需将确切的道具添加到所有路由组件。 It will work它会工作

<Route exact path='/yourpath' />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM