2

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

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>
  );
}

darthmaim
  • 4,970
  • 1
  • 27
  • 40
RevTpark
  • 65
  • 1
  • 8

4 Answers4

7

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')
);
Drew Reese
  • 165,259
  • 14
  • 153
  • 181
1

can you slightly modify your index.js and try once again:

react-router-dom@5

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

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

reportWebVitals();

react-router-dom@6

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

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

reportWebVitals();

This should work.

Pradip Dhakal
  • 1,872
  • 1
  • 12
  • 29
0
import React from "react";
import LoginForm from "./authentication/LoginForm";
import RegisterForm from "./authentication/RegisterForm";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

function App() {
  return (
    <Router>
      <React.StrictMode>
        <Routes>
          <Route path="/" element={<LoginForm />} />
          <Route path="/register" element={<RegisterForm />} />
        </Routes>
      </React.StrictMode>
    </Router>
  );
}
export default App;

/This works for me/

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 17 '22 at 15:57
0

initially it was like this:

    <Route exact path="/register" component={Register} />

but now change to:

    <Route path="/register" element={<Register />} />

read more about react routes v6 => { https://www.freecodecamp.org/news/how-to-use-react-router-version-6/ }

iSteven Zion
  • 11
  • 1
  • 2