Philomath
Philomath

Reputation: 1163

React Bootstrap Navbar Routing

I am new to react and I am trying to create a simple navigation with bootstrap and routing using navbar. Though the URL changes, the UI of the page is not rendered. Here is my code

Index.js

import React from 'react';
import ReactDOM from 'react-dom';
import admin from './admin';
import App from './App';
import reportWebVitals from './reportWebVitals';
import weighbride from './weighbride';
import 'bootstrap/dist/css/bootstrap.min.css'
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";


ReactDOM.render(
  <React.StrictMode>
    
    <App/>
  </React.StrictMode>,
  document.getElementById('root')
);

App.js

import logo from './logo.svg';
import React from 'react'
import './App.css';
import Header from './header'
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

import Weighbridge from './weighbride'
import Admin from './admin'

class App extends React.Component{
  render()
  {
    return(

      <div className='App'>
       
        <Router>
        <Header/>
        <Routes>
          <Route exact path="/" component={App} />
          <Route path="/weighbridge" component={Weighbridge} />
          <Route path="/admin" component={Admin} />
          
        </Routes>
        </Router>

      </div>
      
    )
  }
}

export default App;

Admin.js

import React from 'react'
import "bootstrap/dist/css/bootstrap.min.css";
import Form from "react-bootstrap/Form";
import Button from "react-bootstrap/Button";

class Admin extends React.Component
{
    render()
    {
        return(
            <Form>
            <Form.Group className="mb-3" controlId="formBasicEmail">
              <Form.Label>Email address</Form.Label>
              <Form.Control type="email" placeholder="Enter email" />
              <Form.Text className="text-muted">
                We'll never share your email with anyone else.
              </Form.Text>
            </Form.Group>
          
            <Form.Group className="mb-3" controlId="formBasicPassword">
              <Form.Label>Password</Form.Label>
              <Form.Control type="password" placeholder="Password" />
            </Form.Group>
            <Form.Group className="mb-3" controlId="formBasicCheckbox">
              <Form.Check type="checkbox" label="Check me out" />
            </Form.Group>
            <Button variant="primary" type="submit">
              Submit
            </Button>
          </Form>

        )
    }
}


export default Admin

output

The URL is changing but the UI of the admin page is not rendered. Please help! I am using react bootstrap to create the navigation.

Upvotes: 2

Views: 1251

Answers (1)

Drew Reese
Drew Reese

Reputation: 202608

In react-router-dom@6 the Route component API changed. Instead of using a component, or render and children function props, only a single element prop taking a ReactNode, a.k.a. JSX is used. Removed also was the exact prop as all routes are now always exactly matched using a route ranking system.

Example:

<Router>
  <Header/>
  <Routes>
    <Route path="/" element={<App />} />
    <Route path="/weighbridge" element={<Weighbridge />} />
    <Route path="/admin" element={<Admin />} />
  </Routes>
</Router>

Upvotes: 1

Related Questions