I'm essentially having the same problem as this: React app HashRouter not working on localhost as well as Github User page
I used their solution but it only works with the first link I have up. Is there a way to get multiple links to work.
import React, { Component } from 'react';
import {HashRouter as Router, Route, Switch} from 'react-router-dom';
import { Home } from './Home';
import { Music } from './Music';
import { FullCV } from './FullCV';
import { ContactMe } from './ContactMe';
import { NoMatch } from './NoMatch';
import { NavigationBar } from './components/NavigationBar';
import { Jumbotron } from './components/Jumbotron';
import { Container } from 'react-bootstrap';
import './mycssfile.css';
import { Helmet } from 'react-helmet'
class App extends Component {
render() {
return (
<>
<div>
<Helmet>
<title> My Website </title>
</Helmet>
</div>
<NavigationBar/>
<Jumbotron/>
<Container fluid className = "container">
<Router>
<Switch>
<Route exact path="/" component={(Home)}/>
<Route exact path="/fullCV" component={(FullCV)}/>
<Route exact path="/music" component={Music}/>
<Route exact path="/contactme" component ={(ContactMe)}/>
<Route component={(NoMatch)}/>
</Switch>
</Router>
</Container>
</>
);
}
}
export default App;
It works perfectly on my localhost with BrowserRouter but I need it to work with HashRouter.
Here is a sample of how I'm navigating
import React from 'react';
import headshot from './assets/headshot.jpg';
import headshot2 from './assets/headshot2.jpg';
import { Row, Col, Nav } from 'react-bootstrap';
import styled from 'styled-components';
import { HashRouter as Router, Route, Switch, Link} from 'react-router-dom';
export const Home = () => (
<div className = "homepage">
<Row>
<Col>
<Router>
<Switch>
<Route>
<Nav.Link as = {Link} to = "/fullCV"><h4> Full C/V and Resume </h4></Nav.Link>
</Route>
<Route>
<Nav.Link as = {Link} to = "/music"><h4> Music and Music Videos </h4></Nav.Link>
</Route>
<Route>
<Nav.Link as = {Link} to = "/contactme"><h4> Get in Contact with Me </h4></Nav.Link>
</Route>
</Switch>
</Router>
</Col>
<Col>
<img src = {headshot} height = {380} />
</Col>
</Row>
</div>
)
It only shows the first link, Full CV and Resume, but the rest don't show up.
Had the same issues as you!
How I fixed mine;
I was using BrowserRouter from react-router-dom, but switching to HashRouter fixed it for me.
import { HashRouter, Route, Switch } from 'react-router-dom'
return (
<HashRouter>
<React.Suspense fallback={loading}>
<Switch>
<Route exact path="/login" name="Login Page" render={(props) => <Login {...props} />} />
<Route
exact
path="/register"
name="Register Page"
render={(props) => <Register {...props} />}
/>
<Route exact path="/404" name="Page 404" render={(props) => <Page404 {...props} />} />
<Route exact path="/500" name="Page 500" render={(props) => <Page500 {...props} />} />
<Route path="/" name="Home" render={(props) => <DefaultLayout {...props} />} />
</Switch>
</React.Suspense>
</HashRouter>
Related
I'm in the very first step to create an project, but the react-router-dom's 'Route' just don't work. When i try to use it, just erase all other things in the screen.
My app.js:
import { BrowserRouter, Route } from 'react-router-dom';
import './App.css';
import Header from './components/Header';
import CoinPage from './Pages/CoinPage';
import Homepage from './Pages/Homepage';
function App() {
return (
<BrowserRouter>
<div>
<Header />
<Route path="/" component={Homepage} />
<Route path="/coins/:id" component={CoinPage} />
</div>
</BrowserRouter>
);
}
export default App;
My CoinPage.js:
import React from 'react'
const CoinPage = () => {
return (
<div>
Coin Page
</div>
)
}
export default CoinPage
My Homepage.js:
import React from 'react'
export const Homepage = () => {
return (
<div>
Homepage
</div>
)
}
export default Homepage
My Header.js:
import React from 'react'
/*The header it self:*/
export const Header = () => {
return (
<div>
Header
</div>
)
}
export default Header
When I remove <Route path="/" component={Homepage}/> and <Route path="/coins/:id" component={CoinPage} /> the 'Header' component appears again
You need to wrap all your Route inside Routes Component
import {
BrowserRouter,
Routes,
Route,
} from "react-router-dom";
<BrowserRouter>
<div>
<Header />
<Routes>
<Route path="/" component={Homepage} />
<Route path="/coins/:id" component={CoinPage} />
</Routes>
</div>
</BrowserRouter>
If you are using v6, then component has been replaced by element in Route
<Route path="/" element={<Homepage />} />
<Route path="/coins/:id" element={<CoinPage />} />
Solved!
I needed to change component to element and put each Route in a different Routes tag. Like that:
<BrowserRouter>
<div>
<Header />
<Routes>
<Route path="/" element={<Homepage />} />
</Routes>
<Routes>
<Route path="/coins/:id" element={<Coinpage />} />
</Routes>
</div>
</BrowserRouter>
I have a problem with react Router.
So i am building the nav of an website and when i'm doing the routes, nothing appears.
Would you help me please to see what i'm doing wrong?
The page stays white. Nothing appears, even the root doesnt see the homepage.
........................................................................................
Homepage:
import React from 'react';
import Navbar from '../allpages/navbar/Navbar';
import {
BrowserRouter as Router, Routes, Route
} from "react-router-dom";
import Contact from "../contact/Contact";
import About from "../about/About";
const Home = () => {
return (
<>
<Router>
<Navbar />
<Routes>
<Route path="/about" element={About} />
<Route path="/contact" element={Contact} />
<Route exact path="/" element={Home} />
</Routes>
</Router>
<h3>Home</h3>
</>
)
}
export default Home
Navbar:
import React from 'react';
import {
BrowserRouter as Link
} from "react-router-dom";
const Navbar = () => {
return (
<div>
<nav>
<ul>
<li>
<Link to="/home">Home</Link>
</li>
<li>
<Link to="/about">About</Link>
</li>
<li>
<Link to="/contact">Contact</Link>
</li>
</ul>
</nav>
</div>
)
}
export default Navbar
main app:
import './App.css';
import Home from './components/home/Home'
function App() {
return (
<>
<Home />
</>
);
}
export default App;
contact:
import React from 'react';
const Contact = () => {
return (
<div>
<h3>Contact</h3>
</div>
)
}
export default Contact
About:
import React from 'react';
const About = () => {
return (
<div>
<h1>About</h1>
</div>
)
}
export default About
So besides this typo:
{ BrowserRouter as Link } // use just Link
which is inside Navbar.js
I would also use recommended approach by React-Router team. Which uses <Switch> component to wrap pages. You are using Routes instead, idk if this is even valid.
You can check this example: https://v5.reactrouter.com/web/example/basic
which is really similar to your code.
What versión is you using? You might be out of dated
<React.Suspense fallback={<p>Loading...</p>}>
<Routes>
<Route path="/*" element={<Outlet />}>
<Route path="informacion-financiera" element={<Financiera />} />
<Route path="derechos-de-autor" element={<Author />} />
<Route
</Route>
</Routes>
</React.Suspense>
```
What versión is you using? You might be out of dated
<React.Suspense fallback={<p>Loading...</p>}>
<Routes>
<Route path="/*" element={<Outlet />}>
<Route path="informacion-financiera" element={<Financiera />} />
<Route path="derechos-de-autor" element={<Author />} />
<Route
</Route>
</Routes>
</React.Suspense>
```
You have browserRouter as Link so yow links are not links
import React, { useContext } from 'react';
import Sidebar from "./components/sidebar/Sidebar";
import Topbar from "./components/topbar/Topbar";
import "./app.css";
import Home from "./pages/home/Home";
import { BrowserRouter as Router, Routes, Route, Navigate} from "react-router-dom";
import UserList from "./pages/userList/UserList";
import User from "./pages/user/User";
import NewUser from "./pages/newUser/NewUser";
import ProductList from "./pages/productList/ProductList";
import Product from "./pages/product/Product";
import NewProduct from "./pages/newProduct/NewProduct";
import Login from './pages/login/Login';
import { AuthContext } from './context/authContext/AuthContext';
function App() {
const { user } = useContext(AuthContext);
return (
<Router>
<Routes>
<Route path="/login" element={user ? <Navigate to='/' />: <Login />} />
{user && (
<>
<Topbar/>
<div className="container">
<Sidebar />
<Route exact path="/" element={<Home />} />
<Route path="/users" element={<UserList />} />
<Route path="/user/:userId" element={<User />} />
<Route path="/newUser" element={<NewUser />} />
<Route path="/movies" element={<ProductList />} />
<Route path="/product/:productId" element={<Product />} />
<Route path="/newproduct" element={<NewProduct />} />
</div>
</>
)}
</Routes>
</Router>
);
}
export default App;
After I run this code show this error..
index.tsx:19
Uncaught Error: [Topbar] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>
at invariant (index.tsx:19)
at index.tsx:776
at react.development.js:1104
at react.development.js:1067
at mapIntoArray (react.development.js:964)
at mapIntoArray (react.development.js:1004)
at mapChildren (react.development.js:1066)
at Object.forEachChildren [as forEach] (react.development.js:1103)
at createRoutesFromChildren (index.tsx:760)
at index.tsx:769
I think React version 5 haven't this problem in code. with
Switch.....
I need help for how to implement div in react version 6....with
Routes ?
And I need to help fix Topbar and sidebar problem
I have one React application where I have setup routes via the Navbar. Ideally it should switch but the routes are not switching. Am I missing something?
App.js:
import React from 'react';
import { BrowserRouter } from 'react-router-dom';
import NavigationBar from './navigation/NavigationBar';
import { Routes } from './navigation/Routes';
export class App extends React.Component {
render() {
return (
<BrowserRouter>
<NavigationBar />
<Routes />
</BrowserRouter>
);
}
}
export default App;
naviagtion/NavigationBar.js:
import React, { Component } from 'react';
import { Nav, Navbar, Form, FormControl, NavDropdown, Button } from 'react-bootstrap';
export default class NavigationBar extends Component {
render() {
return (
<Navbar bg="light" variant="light">
<Navbar.Brand href="/">KitchenSmart</Navbar.Brand>
<Navbar.Toggle aria-controls="basic-navbar-nav" />
<Navbar.Collapse id="basic-navbar-nav">
<Nav className="mr-auto">
<Nav.Link href="/">Home</Nav.Link>
<Nav.Link href="/shop">Shop</Nav.Link>
<NavDropdown title="Sale" id="basic-nav-dropdown">
<NavDropdown.Item href="/sale">Winter Sale</NavDropdown.Item>
<NavDropdown.Item href="/sale">Summer Sale</NavDropdown.Item>
<NavDropdown.Item href="/sale">Spring Sale</NavDropdown.Item>
</NavDropdown>
</Nav>
</Navbar.Collapse>
</Navbar>
)
}
}
navigation/Routes.js:
import React from 'react';
import {Route, Switch} from 'react-router';
import Products from '../Products';
import Shop from '../Shop';
import Sale from '../Sale';
import Checkout from '../Checkout';
import Default from '../Default';
const Routes = () => (
<Switch>
<Route exact path="/" component={Products} />
<Route path="/shop" component={Shop}></Route>
<Route path="/sale" component={Sale}></Route>
<Route path="/checkout" component={Checkout}></Route>
<Route component={Default}></Route>
</Switch>
);
export { Routes };
On the URL, the route is added, but the components are not changing. Am I missing something?
You have to use Link tag from react-router-dom in NavigationBar component i.e
<Link to="/">Home</Link>
<Link to="/shop">Shop</Link>
Add prop exact={true} to all your Route components. i.e
<Switch>
<Route exact path="/" component={Products} />
<Route exact path="/shop" component={Shop}></Route>
<Route exact path="/sale" component={Sale}></Route>
<Route exact path="/checkout" component={Checkout}></Route>
<Route exact component={Default}></Route>
</Switch>
I am working on a web application and my application works for the most part except I cannot see the /tasks section. I do not understand because it is the same as the rest of my elements.
import React, {
Component,
Fragment
} from 'react';
import {
BrowserRouter as Router,
Route,
Link
} from 'react-router-dom';
import AppProvider, {
Consumer
} from './AppProvider';
import Login from './Login';
import Signup from './Signup';
import Navbar from './Navbar';
import FlashMessage from './FlashMessage';
class App extends Component {
render() {
return (
<AppProvider>
<Router>
<Fragment>
<Navbar />
<FlashMessage />
<Route exact path="/" component={() =>
<h1 className="content">Welcome, Home!</h1>} />
<Route exact path="/login" component={() => <Login />} />
<Route exact path="/signup" component={() => <Signup />} />
<Router exact path="/tasks" component={() =>
<h1 className="content">Content Should Go Here</h1>} />
<Route exact path="/signedOut" component={() =>
<h1 className="content">You're now signed out.</h1>} />
<Route exact path="/accountCreated" component={() =>
<h1 className="content">Account created. <Link
to="/login">
Proceed to Dashboard</Link></h1>} />
</Fragment>
</Router>
</AppProvider>
);
}
}
export default App;
The content div routed at /tasks won't display anything. There are no errors, I have checked in the console. All I want is it to display the content on /tasks.
I think the problem is you are using the BrowserRouter in your "./tasks" instead of use route.
The issue is because you have a trailing Slash on your link.