Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I make it so that the React router shows multiple elements when routing to the same address?

Right now I’m adding a login part for my website and when I click login, I want the Publish and Verify elements to not show on the website when my source is routing to /login. Here’s my code:

import { Navbar, Welcome, Footer, Publish, Verify, Login } from './components';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';

const App = () => {
  return (
    <div className="min-h-screen">
      <div className="gradient-bg-welcome">
        <Navbar />
        <Router>
          <Routes>
            <Route path='/' exact element={<Welcome/> } />
            <Route path='/' element={<Publish /> } />
            <Route path='/' element={<Verify />} />
            <Route path='/login' element={<Login/>} />
          </Routes>
        </Router>
      </div>
      <Footer />
    </div>
  )
}

export default App

How do I make it so that when the path="/" which is just the home page basically, only Welcome Publish and Verify show and not Login. The code I have above doesn’t work as it only shows Welcome when the path is ‘/’, but I also want it to show Publish and Verify. How can i do this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can achieve that by doing the following.


    import { Navbar, Welcome, Footer, Publish, Verify, Login } from './components';
    import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
    
    const App = () => {
      return (
        <div className="min-h-screen">
          <div className="gradient-bg-welcome">
            <Navbar />
            <Router>
              <Routes>
                <Route path='/' element={
                  <>
                    <Welcome/>
                    <Publish/>
                    <Verify/>
                  </>} />
                <Route path='/login' element={<Login/>} />
              </Routes>
            </Router>
          </div>
          <Footer />
        </div>
      )
    }
    
    export default App

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading