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

React router dom: Route and Router not working at all

I am fairly new to react and trying to make a redirect using react-router-dom I did everything as the documentation but my code doesn’t seem to work. I get an error regarding no element found which I don’t understand what is causing it. Also, I am just trying to reach the page using the address bar and not any button or link for now.
Error

Matched leaf route at location "/" does not have an element. This means it will render an <Outlet /> with a null value by default resulting in an "empty" page.

Same error for /register

index.js

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

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { Route, BrowserRouter as Router, Routes } from 'react-router-dom';
import Register from "./components/Register";

ReactDOM.render( 
  <Router>
    <React.StrictMode>
      <Routes>
        <Route exact path="/register" component={Register} />
        <Route exact path="/" component={App} />
      </Routes>
      <App />
    </React.StrictMode>
  </Router>,
  document.getElementById('root')
);

reportWebVitals();

App.js

import { Component } from 'react';

class App extends Component {
  render(){
    return (
      <div>
        HELLO WORLD
      </div>
    );
  }
}


export default App;

Register.js

import { useState } from "react";
import { useNavigate } from "react-router-dom";

export default function Register(){
    const history = useNavigate();
    const initialFormData = Object.freeze({
          email: '',
          password: '',
      });

      const [formData, updateFormData] = useState(initialFormData);

    const handleChange = (event) => {
      updateFormData({
              ...formData,
              // Trimming any whitespace
              [event.target.name]: event.target.value.trim(),
          });
    }

    const handleSubmit = (event) => {
      event.preventDefault();
      console.log(event.target.email.value);
      console.log(event.target.password.value);
    }

    return (
      <form onSubmit={handleSubmit}>
        <div>
          <label>
            Email: 
            <input type="text" value={formData.email} name="email" onChange={handleChange} />
          </label>
        </div>
        <div>
          <label>
            Password: 
            <input type="text" value={formData.password} name="password" onChange={handleChange}/>
          </label>
        </div>
        <input type="submit" value="Submit" />
      </form>
  );
}

>Solution :

You are not rendering the route components correctly. They should be rendered as JSX, not a reference to the component, on the element prop. This is a breaking change between versions 5 and 6 of react-router-dom. Note also that Route components also no longer take an exact prop, they are now always exactly matched.

ReactDOM.render( 
  <Router>
    <React.StrictMode>
      <Routes>
        <Route path="/register" element={<Register />} />
        <Route path="/" element={<App />} />
      </Routes>
      <App />
    </React.StrictMode>
  </Router>,
  document.getElementById('root')
);
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