Home > database >  Error "Uncaught Error: [App] is not a <Route> component. All component children of <Ro
Error "Uncaught Error: [App] is not a <Route> component. All component children of <Ro

Time:08-07

I am trying to use routing for the first time and followed the exact instructions from Youtube

App.jsx

import React, { Component } from "react";
import { Route, Routes } from "react-router-dom";
import "./App.css";
import Customer from "./components/customer";
import Movies from "./components/moviesComponent";
import NotFound from "./components/not-found";
import Rentals from "./components/rentals";

class App extends Component {
  render() {
    return (
      <Routes>
        <Route path="/movies" element={<Movies />}></Route>
        <Route path="/rentals" element={<Rentals />}></Route>
        <Route path="/customers" element={<Customer />}></Route>
        <Route path="/not-found" element={<NotFound />}></Route>
      </Routes>
    );
  }
}

export default App;

index.js

import React from "react";
import ReactDOM from "react-dom/client";
import "./index.css";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
import "bootstrap/dist/css/bootstrap.css";
import "font-awesome/css/font-awesome.css";
import "hover.css";
import "./App.css";
import { BrowserRouter, Routes } from "react-router-dom";

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <BrowserRouter>
    <Routes>
      <App />
    </Routes>
  </BrowserRouter>
);

// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
reportWebVitals();

I get the following error:

Uncaught Error: [App] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>

I am using react-router latest version.

CodePudding user response:

Just render App in the router, not a Routes component. Only Route and React.Fragment components are valid children of the Routes component.

const root = ReactDOM.createRoot(document.getElementById("root"));
root.render(
  <BrowserRouter>
    <App />
  </BrowserRouter>
);

CodePudding user response:

Looks like you have Routes tag at 2 places. One in App.js and one in index.js. You should remove it from index.js file and than give it a try. https://reactrouter.com/docs/en/v6/getting-started/overview

  • Related