简体   繁体   中英

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?? 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

Just add exact prop to all Route Component. It will work

<Route exact path='/yourpath' />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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