Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Error: A Route is only ever to be used as the child of element, never rendered directly. Please wrap your Route in a Route

Solution 1: Just Use route in this way

import { render } from "react-dom";
import {
  BrowserRouter,
  Routes,
  Route
} from "react-router-dom";
import App from "./App";
import Expenses from "./routes/expenses";
import Invoices from "./routes/invoices";
const rootElement = document.getElementById("root");
render(
  <BrowserRouter>
    <Routes>
      <Route path="/" element={<App />} />
      <Route path="expenses" element={<Expenses />} />
      <Route path="invoices" element={<Invoices />} />
    </Routes>
  </BrowserRouter>,
  rootElement
);



Solution 2: Another way to use route. Just wrap your routes by Routes


import { Route, Routes } from "react-router-dom";
import Welcome from "./Pages/child1";
import Game from "./Pages/child2";
function App() {
    return (
        <div>
          <Routes>
            <Route path = "/child1">
                <Welcome />
            </Route>
            <Route path = "/child2">
                <Game />
            </Route>
           </Routes>
        </div>
    );
}
export default App;



Solution 3: Here we solved this one  change that is the multiple routes are now enclosed within a <Switch> tag

import React from "react";
import "./App.css";
import {
BrowserRouter as Router,
Switch,
Route,
Redirect,
} from "react-router-dom";
function App() {
return (
	<div className="App">
	<Router>
		<Switch>
		<Route path="/child1">
			<div>
			<p>This is child route 1</p>
			</div>
		</Route>
		<Route path="/child2">
			<div>
			<p>This is child route 2</p>
			</div>
		</Route>
		</Switch>
	</Router>
	</div>
);
}
export default App;
Comment

PREVIOUS NEXT
Code Example
Javascript :: react router active link 
Javascript :: javascript response redirect 
Javascript :: get version from package.json 
Javascript :: enzyme-to-json 
Javascript :: mdn foreach 
Javascript :: javascript reduce 
Javascript :: wordpress disable jquery migrate 
Javascript :: js trim all spaces 
Javascript :: how to get last string in javascript 
Javascript :: expressjs async await 
Javascript :: Simple code example of adding two numbers in javascript 
Javascript :: set dynamic route in link react js 
Javascript :: javascript string to variable 
Javascript :: ajax file form 
Javascript :: get element innerhtml jquery 
Javascript :: js filter to remove empty string in array. 
Javascript :: javascript join object properties in array 
Javascript :: connecting nodejs using mongoose 
Javascript :: spawn template playcanvas 
Javascript :: jquery on form submit call function 
Javascript :: jquery next sibling with class 
Javascript :: os module in node js 
Javascript :: how to flip a Number in javascript 
Javascript :: javascript check type of object 
Javascript :: execute a function at a certain time of day js 
Javascript :: discord js bot embed user profile picture 
Javascript :: sequelize sqlite example 
Javascript :: string to jspn js 
Javascript :: javascript validate string with regex 
Javascript :: javascript returning a function 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =