2
import React from "react";
import { BrowserRouter as Router, Route } from "react-router-dom";

import Users from "./users/pages/Users";

const App = () => {
  return (
    <Router>
    
      <Route path="/">
        <Users />
      </Route>
   
    </Router>
    
  );
};

export default App;

I tried replacing router with routes still getting same error.

M0nst3R
  • 5,186
  • 1
  • 23
  • 36
  • What error? Please add information to your question and choose a more precise and shorter title. – M0nst3R Aug 31 '23 at 14:55
  • Does this answer your question? [Error "Error: A is only ever to be used as the child of element"](https://stackoverflow.com/questions/69832748/error-error-a-route-is-only-ever-to-be-used-as-the-child-of-routes-element) – isherwood Aug 31 '23 at 15:27

2 Answers2

0

You need to wrap Route elements in a Routes element

import React from "react";
import { BrowserRouter as Router, Route, Routes } from "react-router-dom";

import Users from "./users/pages/Users";

const App = () => { 
  return (
    <Router>
      <Routes>
        <Route path="/">
          <Users />
        </Route>
      </Routes>
    </Router>
  ); 
};
Anh Tran
  • 332
  • 6
0

Issues

  1. All Route components can only be rendered by a parent Routes component, or another Route component in the case of nested routes.
  2. Only other Route components are valid children of the Route component, so Users can't be rendered as a child component.

Solution

  1. Wrap the Route component in a Routes component.
  2. Render Users as the element of its Route.
import React from "react";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";

import Users from "./users/pages/Users";

const App = () => {
  return (
    <Router>
      <Routes>
        <Route path="/" element={<Users />} />
        ... other routes ...
      </Routes>
    </Router>
  );
};

export default App;
Drew Reese
  • 165,259
  • 14
  • 153
  • 181