Home > database >  React router dom: Route and Router not working at all
React router dom: Route and Router not working at all

Time:11-10

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

CodePudding user response:

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

CodePudding user response:

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.

  • Related