Using react routes for routing in a react application - javascript

I am trying to setup the routes for a simple react application and react-router doesn't seem to match the given routes in the switch defaults the 404 page
Here is the code for the routes:
import {
BrowserRouter as Router,
Route,
Switch,
HashRouter
} from "react-router-dom";
window.React = React;
render(
<Router>
<Switch>
<HashRouter>
<Route exact path="/" component={App} />
<Route path="list-days" component={App} />
<Route path="add-day" component={AddDayForm} />
<Route component={Whoops404} />
</HashRouter>
</Switch>
</Router>,
document.getElementById("root")
);
And here is the code for the links to those routes:
import { Link } from "react-router-dom";
import { IoIosHome as Home } from "react-icons/io";
import { FaCalendarPlus } from "react-icons/fa";
import { FaTable } from "react-icons/fa";
export const Menu = () => (
<nav className="Menu">
<Link to="/" activeclassname="selected">
<Home />
</Link>
<Link to="/add-day" activeclassname="selected">
<FaCalendarPlus />
</Link>
<Link to="/list-days" activeclassname="selected">
<FaTable />
</Link>
</nav>
When you click anything but the home link the 404 page is displayed.

You use 'links' as HTML structure and have this not inherited in import from "react-router-dom";
Per seeing the documentation of react this all is provided with this import statement.
import React from "react";
import {
BrowserRouter as Router,
Switch,
Route,
Link,
HashRouter
} from "react-router-dom";

Add a / to your Routes' path.
<Route path="/list-days" component={App} />

The problem is, you have defined your Route like
<Route path="list-days" component={App} />
and you are accessing those links as
<Link to="/list-days" activeclassname="selected"></Link>
You can see the difference that your are adding a front slash when accessing through Link. Just put a front slash on Route like this:
<Route path="/list-days" component={App} />
will solve the issue.
Thanks.

Probably because you are using hash router.
try this:
return (
<HashRouter>
<Switch>
<Route exact path="#/" component={App} />
<Route path="#/list-days" component={App} />
<Route path="#/add-day" component={AddDayForm} />
<Route component={Whoops404} />
<Switch>
</HashRouter>
)
or why not just switch to browserRouter so you wouldn't need those hashes on your url?

Try to remove HashRouter and edit your route to be like this
<Router>
<Switch>
<Route exact path="/" component={App} />
<Route exact path="/list-days" component={App} />
<Route exact path="/add-day" component={AddDayForm} />
<Route exact path="*" component={Whoops404} />
</Switch>
</Router>

Related

Adding Base URL in react.js project is not working

I want to add the base URL to my react.js project.
However I had tried couple of methods that did not work.
For example, if my project url is :
http://www.myproject.com/
It should appear in address bar as :
http://www.myproject.com/app
http://www.myproject.com/app/home // If home page
http://www.myproject.com/app/about // If about page
Below is my project code.
index.js
import "./index.css"
import React from 'react';
import ReactDOM from 'react-dom/client';
import { AuthProvider } from './context/AuthProvider';
import {BrowserRouter, Routes, Route} from 'react-router-dom'
import { disableReactDevTools } from '#fvilers/disable-react-devtools';
import App from './App';
if (process.env.NODE_ENV === 'production') {
disableReactDevTools();
}
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<BrowserRouter>
<AuthProvider>
<Routes>
<Route path="/*" element={<App />} />
</Routes>
</AuthProvider>
</BrowserRouter>
);
App.js
import Layout from './components/authController/Layout'
import Login from './components/authController/Login';
import Home from './components/authController/Home'
import AllUser from './components/authController/AllUser'
import RequireAuth from './components/authController/RequireAuth';
import PersistLogin from './components/authController/PersistLogin';
import Logout from './components/authController/Logout';
import Missing from './Missing';
import {Routes, Route, Navigate} from 'react-router-dom'
function App() {
return (
<div>
<Navbar/>
<Routes>
<Route path="/" element={<Layout />}>
{/* public routes */}
<Route path="login" element={<Login />} />
<Route path="loggedOut" element={<Logout />} />
{/* we want to protect these routes */}
<Route element={<PersistLogin />}>
<Route path="/" element={<Navigate to="/home" replace />} />
<Route path="home" element={<Home/>} />
<Route element={<RequireAuth/>}>
<Route path="alluser" element={<AllUser />} />
</Route>
</Route>
{/* catch all */}
<Route path="*" element={<Missing />} />
</Route>
</Routes>
</div>
);
}
export default App;
What I had tried?
As I referred in stackoverflow and also many other websites, integrated below methods.
Added base url inside package.json file
"homepage": "/app",
Tried to pass the tag <base href="http://myapp.com/app"> within <head> tag in public/index.html page
Passed the base url within BrowserRouter in index.js <BrowserRouter basename="app"> </BrowserRouter>
I am sure I am missing something, it would be great learning if someone help on this or give insights on this.
Thank you
I don't know the correct answer to this , but I believe that you can redirect the route "/" to "/app" . I hope this can help you
Solution:-
Hello everyone.
I got the solution for my query.
I have modified the index.js file like below
<Routes>
<Route path="app/*" element={<App />} />
<Route path="/*" element={<Navigate to="app/home" replace />} />
</Routes>

How to access BrowserRouter history outside of BrowserRouter?

Below is my code snippet simplified. (using react-router-v5)
My question is how to get access to BrowserRouter's history in the logout_Handler(), given that I am "outside" BrowserRouter?
I've seen this answer How to access history object outside <Route /> in React Router v4
, but there is also a comment which applies to my case:
"When I try to use "withRouter" as shown here I get the error
You should not use <Route> or withRouter() outside a <Router>
which is exactly what I get, if I try to use withRouter at the App.js level.
// App.js
function App(props) {
const logout_Handler = (e) => {
localStorage.removeItem("app-tokenObj");
// how to get the history from BrowserRouter to redirect?!
//history.push("/login") ?? NO IDEA ??
}
return (
<BrowserRouter>
<nav>
<h3>My Supa Dupa App</h3>
<Link to="/">Home Page</Link>
<Link to="/admin">Admin Page</Link>
<button type="button" onClick={logout_Handler}>Logout</button>
</nav>
<hr/>
<Switch>
<Route exact path="/" component={Home} />
<Route path="/link1" component={Comp1} />
<Route path="/link2" component={Comp1} />
<Route path="/login" component={Login} />
<Route path="/signup" component={Signup} />
</Switch>
</BrowserRouter>
)
}
If you are trying to access parts of a routing context in the App component then the router providing the context needs to be higher in the ReactTree. The solution is simple. Move BrowserRouter higher than App in your apps code. Wrapping App in the BrowserRouter would be sufficient enough for this.
Example index.js
ReactDOM.render(
...
<BrowserRouter>
<App />
</BrowserRouter>
...,
root,
);
App
The App component now has a routing context available. While you could use the withRouter Higher Order Component, using the useHistory hook would be simpler.
import { Link, Route, useHistory } from 'react-router-dom';
function App(props) {
const history = useHistory();
const logout_Handler = (e) => {
localStorage.removeItem("app-tokenObj");
history.push("/login");
}
return (
<>
<nav>
<h3>My Supa Dupa App</h3>
<Link to="/">Home Page</Link>
<Link to="/admin">Admin Page</Link>
<button type="button" onClick={logout_Handler}>Logout</button>
</nav>
<hr/>
<Switch>
<Route path="/link1" component={Comp1} />
<Route path="/link2" component={Comp1} />
<Route path="/login" component={Login} />
<Route path="/signup" component={Signup} />
<Route path="/" component={Home} />
</Switch>
</>
);
}
#for move one folder previous:
history.push("/../login")

Router, Rout and Switch not working in my React website?

I am trying to create a website with React, but my code is not working and i dont understand why..
I have this code in index.js, because I want my header to always be there.
const element = <h1>Header</h1>; ReactDOM.render(element, document.getElementById('root'));
I have created a few components, and I have this code in my App.js
import React, {Component} from 'react';
import {BrowserRouter as Router, Route, Switch, Link, BrowserRouter} from 'react-router-dom';
import {Home} from './Components/Home';
import {About} from './Components/About';
import {Contact} from './Components/Contact';
import {Resume} from './Components/Resume';
import {Photos} from './Components/Photos';
import {NoMatch} from './Components/NoMatch';
class App extends Component {
render(){
return(
<React.Fragment> <Router> <switch>
<Route path="/" exact component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
<Route path="/resume" component={Resume} />
<Route path="/photos" component={Photos} />
<Route component={NoMatch} />
</switch>
</Router>
</React.Fragment>
);
}
}
And all my components looks like this now, just temporarly:
import React from 'react'
export const About = () => (
<div>
<h2>About</h2>
</div>
)
The problem is that i only see "Header" on all pages, and when i change the path to localhost../About, it does not show anything else but "Header".
Why is not my code working and the code in my components showing in the different paths?
I've used the npm create-react-app and uses visualstudiocode. Thank you for the help, much appreciated :)
Btw, im following a tutorial and it is working for him but not me for some reason...
The Switch needs to be with a capital letter 'S'
<Router>
<Switch>
<Route path="/" exact component={Home} />
<Route path="/about" component={About} />
<Route path="/contact" component={Contact} />
<Route path="/resume" component={Resume} />
<Route path="/photos" component={Photos} />
<Route component={NoMatch} />
</Switch>
</Router>
Shouldn’t ‘switch’ be with a capital letter ( Switch )?
Is <switch> with lower case on purpose? It should be <Switch>
Anyway, I would like to see your code in a code sandbox if it's not the problem

404 page in REACT

I created the component NotFound and it works fine when I go to a page that doesn't exist. But the same page it's appearing in all my pages, not only the one that doesn't exist. This is the component:
import React from 'react'
const NotFound = () =>
<div>
<h3>404 page not found</h3>
<p>We are sorry but the page you are looking for does not exist.</p>
</div>
export default NotFound
And this is how I used it in the main page:
class MainSite extends Component {
render () {
return (
<div>
{/* Render nav */}
<Route path='/dashboard' component={Nav} />
<Route path='/retrospectives' component={Nav} />
<Route path='/users' component={Nav} />
<Route path='/projects' component={Nav} />
{/* Dashboard page */}
<ProtectedRoute exact path='/dashboard' component={DashboardPage} />
{/* Retrospectives page */}
<ProtectedRoute exact path='/retrospectives' component={RetrospectivesPage} />
{/* Users page */}
<ProtectedRoute exact path='/users' component={UsersPage} />
{/* Projects page */}
<ProtectedRoute exact path='/projects' component={ProjectsPage} />
{/* Retrospective related pages */}
<Route exact path='/retrospectives/:retrospectiveId' component={Retrospective} />
<Route exact path='/join-retrospective' component={JoinRetrospective} />
<ProtectedRoute exact path='/create-retrospective/:retrospectiveId' component={Retrospective} />
{/* OnBoarding pages */}
<ProtectedRoute exact path='/beta-code' component={BetaCodeAccess} />
<Route exact path='/auth-handler' component={AuthHandler} />
<Route exact path='/join-organization' component={JoinOrganization} />
</div>
)
}
}
export default MainSite
As you can see I use <Route path="*" component={NotFound} /> to create the 404 pages, but that component is appearing in every existing page as well. How can I fix this?
Try this one:
import { Switch, Route } from 'react-router-dom';
<Switch>
<Route path='/dashboard' component={Nav} />
<Route path='/retrospectives' component={Nav} />
<Route path='/users' component={Nav} />
<Route path='/projects' component={Nav} />
<Route path="" component={NotFound} />
</Switch>
All below example works fine:
<Route path="" component={NotFound} /> // empty ""
<Route path="*" component={NotFound} /> // star *
<Route component={NotFound} /> // without path
Or if you want to return a simple 404 message without any component:
<Route component={() => (<div>404 Not found </div>)} />
For those who are looking for an answer using react-router-dom v6, many things had changed. Switch for example doesn't exists anymore, you have to use element instead of component, ... Check this little example to get you an idea:
import React from 'react'
import ReactDOM from 'react-dom'
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom'
import './index.css'
import App from './App'
const Test = () => (
<h1>404</h1>
)
ReactDOM.render(
<React.StrictMode>
<Router>
<Routes>
<Route path='/' element={<App />} />
<Route path='*' element={<Test />}/>
</Routes>
</Router>
</React.StrictMode>,
document.getElementById('root')
)
With this you are defining your home route and all the other routes will show 404. Check the official guide for more info.
Try This:
import React from "react";
import { Redirect, Route, Switch, BrowserRouter } from 'react-router-dom';
import HomePage from './pages/HomePage.jsx';
import NotFoundPage from './NotFoundPage.jsx';
class App extends React.Component {
render(){
return(
<BrowserRouter>
<Switch>
<Route exact path='/' component={HomePage} />
<Route path="*" component={NotFoundPage} />
</Switch>
</BrowserRouter>
)
}
}
export default App;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
Simply import Switch from react-router-dom and wrap all your Routes in the Switch Component. Also Important here is to note to keep your 404Page component at the very bottom(just before your switch ending tag) This way it will match each component with its route first. If it matches, it will render the component or else check the next one. Ultimately if none matching routes will be founded, it will render 404Page
react router is a headache for new coders. Use this code format. This is class component but you can make a functional component and use it.
import React from "react";
import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import HomePage from './pages/HomePage.jsx';
import NotFoundPage from './NotFoundPage.jsx';
import Footer from './Footer';
class App extends React.Component {
render(){
return(
<Router>
<Routes>
<Route exact path='/' element={HomePage} />
<Route path="*" element={NotFoundPage} />
</Routes>
<Routes>
<Route path="/" element={Footer}/>
</Routes>
</Router>
)
}
}
export default App;

React-Router only one child

I keep on getting the error:
A 'Router' may have only one child element
when using react-router.
I can't seem to figure out why this is not working, since it's exactly like the code they show in their example: Quick Start
Here is my code:
import React from 'react';
import Editorstore from './Editorstore';
import App from './components/editor/App';
import BaseLayer from './components/baselayer';
import {BrowserRouter as Router, Route} from 'react-router-dom';
import {render} from 'react-dom';
const root = document.createElement('div');
root.id = 'app';
document.body.appendChild(root);
const store = new Editorstore();
const stylelist = ['https://maxcdn.bootstrapcdn.com/font-awesome/4.6.3/css/font-awesome.min.css', 'https://cdnjs.cloudflare.com/ajax/libs/semantic-ui/2.2.2/semantic.min.css', 'https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.5.2/animate.min.css', 'https://api.tiles.mapbox.com/mapbox-gl-js/v0.33.1/mapbox-gl.css'];
stylelist.map((link) => {
const a = document.createElement('link');
a.rel = 'stylesheet';
a.href = link;
document.body.appendChild(a);
return null;
});
render((
<Router>
<Route exact path="/" component={BaseLayer} />
<Route path="/editor" component={App} store={store} />
</Router>
), document.querySelector('#app'));
You have to wrap your Route's in a <div>(or a <Switch>).
render((
<Router>
<Route exact path="/" component={BaseLayer} />
<Route path="/editor" component={App} store={store} />
</Router>
), document.querySelector('#app'));
should be
render((
<Router>
<div>
<Route exact path="/" component={BaseLayer} />
<Route path="/editor" component={App} store={store} />
</div>
</Router>
), document.querySelector('#app'));
jsfiddle / webpackbin
This is an API change in react-router 4.x. Recommended approach is to wrap Routes in a Switch: https://github.com/ReactTraining/react-router/issues/4131#issuecomment-274171357
Quoting:
Convert
<Router>
<Route ...>
<Route ...>
</Router>
to
<Router>
<Switch>
<Route ...>
<Route ...>
</Switch>
</Router>
You will, of course, need to add Switch to your imports:
import { Switch, Router, Route } from 'react-router'
I Always use Fragment in react web and native ( >= react 16 )
import React, { Component, Fragment } from 'react'
import { NativeRouter as Routes, Route, Link } from 'react-router-native'
import Navigation from './components/navigation'
import HomeScreen from './screens/home'
import { RecipesScreen } from './screens/recipe'
class Main extends Component {
render() {
return (
<Fragment>
<Navigation />
<Routes>
<Fragment>
<Route exact path="/" component={HomeScreen} />
<Route path="/recipes" component={RecipesScreen} />
</Fragment>
</Routes>
</Fragment>
)
}
}
export default Main
I put all my <Route /> tags inside the <Switch> </Switch> tag like this.
<BrowserRouter>
<Switch>
<Route path='/' component={App} exact={true} />
<Route path='/form-example' component={FormExample} />
</Switch>
</BrowserRouter>
This solves the problem.
If you are nesting other components inside the Router you should do like.
<Router>
<div>
<otherComponent/>
<div>
<Route/>
<Route/>
<Route/>
<Route/>
</div>
</div>
</Router>
If you are using Reach Routers make sure the Code looks like this:
<Router>
<Login path="/" />
<Login path="/login" />
</Router>
Including these Components in a Div in the case of React Routers will make this work but In Reach Routers, Remove that Div Element.
you can also wrap all your route in a parent route which defaults to index page
<Router history={history}>
<Route path="/" component={IndexPage}>
<Route path="to/page" component={MyPage}/>
<Route path="to/page/:pathParam" component={MyPage}/>
</Route>
</Router>
I am using react-router-dom package with version 5.0.1 and it works perfectly fine with your code.
import { BrowserRouter as Router , Router, Link } from 'react-router-dom';
import Home from './pages/Home';
import About from './pages/About';
...
class App extends React.Component {
render() {
return (
<Router>
<ul>
<li><Link path='/'>Home</Link></li>
<li><Link path='/about'>About</Link></li>
</ul>
<Route path='/' exact component={Home} />
<Route path='/about' component={About} />
</Router>
);
}
}
export default App;
Not sure if my router might be too simple, or there was a change to this rule but was following along a tutorial that mentioned this limitation (A 'Router' may have only one child element) and it allowed me to add 3 routes without giving any errors. This is the working code:
function render() {
ReactDOM.render(
<BrowserRouter>
<Route exact path="/" component={App} />
<Route path="/add" component={AddAuthorForm} />
<Route path="/test" component={test} />
</BrowserRouter>
,
document.getElementById('root')
);
}
And this are my dependencies:
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-router-dom": "^5.1.2",
"react-scripts": "3.4.1",
This problem occurs when you don't have parent tag before <Route>inside <Router> so to resolve this problem keep the <Route>enclosed in a parent tag such as <div> , <p> etc.
Example -
<Router>
<p>
<Route path="/login" component={Login} />
<Route path="/register" component={Register} />
</p>
</Router>

Categories