I've built a toggle component that creates the code chunks for a list of feature components based on the env variable set for the build.
e.g.:
import React from "react";
import OtherComponent from "./OtherComponent";
import { ToggleFeature, features } from "./ToggleFeature";
export default function App() {
return (
<>
<OtherComponent />
<ToggleFeature feature={features.CustomRoute} suspense={false} />
</>
);
}
This approach works for almost every component, except for Route, it seems to break any routes bellow.
I've thought that the Suspense in the middle of the Switch was breaking the code, so I've created a boolen prop called suspense to deactivate it inside the ToggleFeature, leaving this responsability to the code calling it.
This works:
import React, { Suspense } from "react";
import { BrowserRouter, Switch, Route, Link } from "react-router-dom";
import Home from "./Home";
import RouteCommon from "./RouteCommon";
import { features } from "./ToggleFeature";
export default function App() {
return (
<>
<BrowserRouter>
<Link to="/">Home</Link> •
<Link to="/common">Common Route</Link> •
<Link to="/lazy">Lazy Route</Link>
<Suspense fallback={null}>
<Switch>
<Route path="/common" component={RouteCommon} />
<Route path="/lazy" component={features.CustomRoute} />
<Route path="/" component={Home} />
</Switch>
</Suspense>
</BrowserRouter>
</>
);
}
This doesn't work:
import React, { Suspense } from "react";
import { BrowserRouter, Switch, Route, Link } from "react-router-dom";
import Home from "./Home";
import RouteCommon from "./RouteCommon";
import { ToggleFeature, features } from "./ToggleFeature";
export default function App() {
return (
<>
<BrowserRouter>
<Link to="/">Home</Link> •
<Link to="/common">Common Route</Link> •
<Link to="/lazy">Lazy Route</Link>
<Suspense fallback={null}>
<Switch>
<Route path="/common" component={RouteCommon} />
<ToggleFeature feature={features.CustomRoute} suspense={false} />
<Route path="/" component={Home} />
</Switch>
</Suspense>
</BrowserRouter>
</>
);
}
Is this approach possible for this situation?
Here's a fiddle with the above code running: https://codesandbox.io/s/toggle-route-kvqp5
Related
Before importing react-router it was working fine. Now it build successfully but shows a blank page. Here is my code:
App.js
//import ReactDOM from "react-dom/client";
import { BrowserRouter, Routes, Route } from "react-router-dom";
import 'bootstrap/dist/css/bootstrap.min.css';
import Pending from './Pages/Home';
import Home from './Pages/Pending';
export default function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={Home} />
<Route path="/Pending" element={Pending} />
</Routes>
</BrowserRouter>
);
}
Home.js
import React, { Component } from "react";
import 'bootstrap/dist/css/bootstrap.min.css';
import NavBar from '../components/NavBar';
function Home(){
return(
<div>
<div>
<NavBar/>
</div>
<h1>HI</h1>
</div>
);
}
export default Home;
Issue
The Route component API changed in react-router-dom#6. All routed content is now rendered on a single element prop as a ReactNode, a.k.a. JSX, not a reference to a React component.
Solution
Render the routed components as JSX, i.e. <Home /> instead of Home.
import { BrowserRouter, Routes, Route } from "react-router-dom";
import 'bootstrap/dist/css/bootstrap.min.css';
import Pending from './Pages/Home';
import Home from './Pages/Pending';
export default function App() {
return (
<BrowserRouter>
<Routes>
<Route path="/" element={<Home />} />
<Route path="/Pending" element={<Pending />} />
</Routes>
</BrowserRouter>
);
}
You should specify which version of react-router-dom you are using. So I'm just gonna assume, you are using the latest v6.
In your App.js file, you have to make the following changes in order to work:
import { Routes, Route } from "react-router-dom";
import 'bootstrap/dist/css/bootstrap.min.css';
import Pending from './Pages/Home';
import Home from './Pages/Pending';
export default function App() {
return (
<Routes>
<Route index path="/" element={<Home />}/>
<Route path="pending" element={<Pending />} />
</Routes>
);
}
For more info, please visit the official documentation here!
import './App.css';
import { BrowserRouter as Router , Route, Routes } from "react-router-dom";
import Header from './Components/Header';
import Footer from './Components/Footer';
import Home from './Components/Home';
import About from './Components/About';
function App() {
return (
<>
<Router>
<Header/>
<Routes>
<Route path="/" element={ <Home/> } />
<Route path="/About" element={ <About/> } />
</Routes>
<Footer/>
</Router>
</>
);
}
export default App;
Here is my App.js code
import './App.css';
import Home from './Pages/Home';
import About from './Pages/About';
import Contact from './Pages/Contact';
import {BrowserRouter,Routes,Route ,Link} from 'react-router-dom';
function App() {
return (
<div>
<BrowserRouter>
<Routes>
<Route path="/" element={<Home/>}/>
<Route Path="/About" element={<About/>}/>
<Route Path="/Contact" element={<Contact/>}/>
</Routes>
</BrowserRouter>
</div>
);
}
export default App;
and Here is my About page
import React from 'react';
import {Link} from 'react-router-dom';
function About() {
return (
<div>
<h1>This is About</h1>
<Link to="About">Go to About</Link>
</div>
)
}
export default About
React Route is not working.I installed react router Dom already.please help me to fix
Not navigation working
Can you change this
<Link to="About">Go to About</Link>
to
<Link to="/About">Go to About</Link>
I have added / before the "About" in to.
I'm just starting my first react web app but my components are not rendering. even a simple h1 element directly in the app.js file is not rendering. it did, however, after deleting the Routes, so my best guess is that the problem lies somewhere there.
the code:
app.js
import React, { lazy, Suspense } from 'react'
import './App.css';
import { Routes, Route } from 'react-router-dom'
import { ToastContainer } from 'react-toastify'
import 'react-toastify/dist/ReactToastify.css'
const Home = React.lazy(() => import('./pages/Home'))
const App = () => {
return (
<Suspense fallback={<div>Loading...</div>}>
<h1>App</h1>
<ToastContainer />
<Routes>
<Route exact path="/" element={Home} />
</Routes>
</Suspense>
)
}
export default App;
Home.js
import React from 'react'
const Home = () => {
return (
<h1>Home Page</h1>
)
}
export default Home
sorry for this very beginner question. please keep in mind that this is my first react web app.
React route accepts ReactElement not ReactNode, no need of exact also
<Routes>
<Route path="/" element={<Home />} />
</Routes>
I think that the tutorial you are using uses the previous react-router API. Please, try this:
import React from "react";
import {
BrowserRouter as Router,
Switch,
Route,
Link,
useRouteMatch,
useParams
} from "react-router-dom";
export default function App() {
return (
<Router>
<div>
<ul>
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/topics">Topics</Link>
</li>
</ul>
<Switch>
<Route path="/about">
<About />
</Route>
<Route path="/">
<Home />
</Route>
</Switch>
</div>
</Router>
);
}
function Home() {
return <h2>Home</h2>;
}
function About() {
return <h2>About</h2>;
}
**When i Write my My Nav Bar Component Content Directly in My Router Component. It works Fine But When I write that content in a NavBar Component it Generates the following error
Error: useHref() may be used only in the context of a component.
.**
Im Using
"react-dom": "^17.0.2", "react-router-dom": "^6.0.0-beta.6",
Following are my Components..
My NavBar Component:-
import { Link } from "react-router-dom";
// import styles from './NavBar.module.css';
export const NavBar = () => {
return (
<nav>
<Link to="/"> Home </Link>
<Link to="/about"> About </Link>
<Link to="/product"> Product </Link>
</nav>
);
}
My Route Component:-
import { BrowserRouter as Router, Switch, Route, Routes} from "react-router-dom";
// importing Component's
import { Home } from "./Components/Home/Home";
import { About } from "./Components/About/About";
import { Products } from "./Components/Products/Products";
import { ProductItems } from "./Components/ProductItems/ProductItems";
import {NavBar} from "./Components/NavBar/NavBar";
import { Fotter } from "./Components/Fotter/Fotter";
export const RouterConfig = () => {
return (
<Router>
<NavBar />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route exact path="/product" component={Products} />
<Route path="/product/:id" component={ProductItems} />
<Route path="*" component={() => <h2>404 Not Found </h2>} />
</Switch>
{/* <Fotter />' */}
</Router>
);
};
import {BrowserRouter as Router} from "react-router-dom";
Use in components App.js or above in index.js and wrap your component in Router.
Then the problem will disappear ;-)
In Your case I see two option:
index.js
import React from "react";
import ReactDOM from "react-dom";
import App from "./App";
import { BrowserRouter as Router } from "react-router-dom";
ReactDOM.render(
<React.StrictMode>
<Router>
<App />
</Router>
</React.StrictMode>,
document.getElementById("root")
);
OR use App Component instead of RouterConfig Component.
App.js
import { BrowserRouter as Router, Switch, Route, Routes} from "react-router-dom";
// importing Component's
import { Home } from "./Components/Home/Home";
import { About } from "./Components/About/About";
import { Products } from "./Components/Products/Products";
import { ProductItems } from "./Components/ProductItems/ProductItems";
import {NavBar} from "./Components/NavBar/NavBar";
import { Fotter } from "./Components/Fotter/Fotter";
export const App = () => {
return (
<Router>
<NavBar />
<Switch>
<Route exact path="/" component={Home} />
<Route path="/about" component={About} />
<Route exact path="/product" component={Products} />
<Route path="/product/:id" component={ProductItems} />
<Route path="*" component={() => <h2>404 Not Found </h2>} />
</Switch>
{/* <Fotter />' */}
</Router>
);
};
I tried to explain it as best I could. Hope this helps you ;-) Best Greetings and Good Luck!
If you're still confused, check out here =>
React Router V6 - Error: useRoutes() may be used only in the context of a <Router> component
export is not running properly and causing an error. Can you retype it like below and try?:
import { Link } from "react-router-dom";
const NavBar = () => {
return (
<div>
<Link to="/"> Home </Link>
<Link to="/about"> About </Link>
<Link to="/product"> Product </Link>
</div>
);
}
export default Navbar;
Also, you don't need curly brace for import statement.
import NavBar from "./Components/NavBar/NavBar";
If problem still persists, you can check your file paths because you are repeating folder names twice, it looks suspicious to me. Maybe you mean this:
import NavBar from "./Components/NavBar";
I want deep link with dynamic route
My code is this
import React from 'react';
import { BrowserRouter as Router, Switch, Route, Link } from "react-router-dom";
import Home from '../Routes/Home';
import Add from '../Routes/Add';
import Region from '../Routes/Region';
import Stores from '../Routes/Region/Stores';
function App() {
return (
<div className="App">
<Router>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/addBook" component={Add} />
<Route path="/:region" component={Region} />
<Route path="/:region/:store" component={Stores} />
</Switch>
<Link to={`/${"ulsan"}`}>
<button>go</button>
</Link>
</Router>
</div>
);
}
export default App;
and my region page is this
import React from 'react';
import { Link } from 'react-router-dom';
function Region() {
const store = "hello"
return(
<>
<Link to={`/${"ulsan"}/${store}`}>
<button>store</button>
</Link>
<div>Region</div>
</>
)
}
export default Region;
when I click store button, page is staying Region
I don't know how to move Stoer page with dynamic route
what is the problem???
Sorry I'm not good at English
The Problem is how you declare your routes, you need to add exact attributs:
<Route exact path="/:region" component={Region} />