ReactJS Route 404 '*' redirecting all the time - javascript

Im having a little problem here, Im trying to put a page 404 when the url is invalid, the problem is, it's loading all the time, for example if a try to access my home page will redirect to the page 404, I've tried some examples but not success :
#redirect in all pages
<Redirect exact={true} from='*' to='/404' /> #test 1
<Redirect from='*' to='/404' /> #test2
#appear in the bottom of the pages
<Route exact={true} path='*' component={asyncComponent(() => import('./containers/Page/404'))} />
<Route path='*' component={asyncComponent(() => import('./containers/Page/404'))} />

try having in this way
<Switch>
<Route path="/" exact component={Home}/>
<Route path="/will-match" component={WillMatch}/>
<Route component={NoMatch} />
</Switch>
React router docs for no match

This is how I have configured the routes in a recent react project.
<Route exact path={`${process.env.PUBLIC_URL}/`} component={Main} />
<Route path='/new' component={ReportCreator}/>
<Route path='/reports/:id' component={Report}/>
<Route component={Page404} />
Using BrowserRouter and Switch router.
https://reacttraining.com/react-router/web/example/no-match

Related

Redirect to 404 not found page while any of the paths get unmatched?

<Routes>
{!userlog ? (
<Route path="/" element={<AuthLayout />}>
<Route index element={<Login />} />
<Route path="/login" element={<Login />} />
<Route path="/signin" element={<Login />} />
<Route path="/forgotpassword" element={<ForgotPassword />} />
<Route path="/changepassword" element={<ChangePassword />} />
</Route>
) : userlog?.user_type===1?(
<Route path="/" element={<HomeLayout />}>
<Route index element={<HomePage />} />
<Route path="viewproduct/:id" element={<ViewPage />} />
<Route path="cartlist" element={<CartList />} />
</Route>
):
<Route path="/" element={<AdminLayout />}>
<Route index element={<Dashboard />} />
<Route path="listproducts" element={<ProductsList />} />
</Route>
}
<Route path="*" element={<Navigate to={"/"} />} />
</Routes>
What i want,
Scenario 1(user not logged in): if user try to navigate "/cartlist" it's redirect to the login page. If user try navigate something not an part of route path's want to navigate 404 not found page.
Scenario 2(user logged in): if user try to navigate "/login" it's redirect to the dashboard page. If user try navigate something not an part of route path's want to navigate 404 not found page.
Now what i get is,
User try to navigate any other routes it's navigate to "/" page.
NOTE: I don't want to put private route for every Route's and Also i want routing path like this only..
Thanks in advance....
The reason it always resolves to "/" is because it will choose the first route which matches the given pattern, even if it is a partial match. Since all of your routes begin with "/" eg. "/login" etc it always matches "/".
You can prevent this with the "exact" parameter:
<Route exact path="/" element={<AuthLayout />}>
Ofcourse due to this line, it will go to "/":
<Route path="*" element={<Navigate to={"/"} />} />
With the new react-router v6 you can use this by changing:
// 404 route
<Route path="*" element={<NotFoundPage />} />
NotFoundPage will be your component to tell users that page is not found.
Also, for these, i think you need to put a forward slash / before pathname
<Route path="/viewproduct/:id" element={<ViewPage />} />
<Route path="/cartlist" element={<CartList />} />
<Route path="/listproducts" element={<ProductsList />} />

How to create a dynamic route in react with react router

Please am finding it difficult to create a route in react router
I want to create a route like this <Route path="/:storeId" component={StorePage} />
But I also have a route like this /stores
Any time I go to the /:storeId page it loads the /users page
Am just confuse
Here is the code
<Switch>
<ScrollToTop>
<MainLayout>
<Route exact path="/">
<Home />
</Route>
<Route exact path="/stores">
<Stores />
</Route>
<Route exact path="/:storeId">
<StorePage />
</Route>
</MainLayout>
</ScrollToTop>
</Switch>
Any help please.
You should do something like this
<Switch>
<Route exact path="/users">
<Users/>
</Route>
<Route path="/users/:id">
<UserById/>
</Route>
</Switch>

React Router - How to IndexRedirect to dynamic route

I have a situation in my React app to look like something as follows using react-router. I want the index of the users/:userId endpoint to redirect to another endpoint that includes a :userId params. What is a good way to do this? Right now the user from currentUser will return null because the code is only executed once in the beginning when the App is loaded the user is not yet authenticated. I'm guessing I will need to force react-router to reload the history and certain variables, or change the order of when I do authentication?
routes.js
{ /* Routes requiring login */ }
<Route onEnter={requireLogin}>
<Route path="users" component={Users}>
<Route path=":userId" component={User}>
<IndexRedirect to={`/users/${currentUser().id}/profile`} />
<Route path="profile" component={UserProfile} />
<Route path="settings" component={UserSettings} />
<Route path="activity" component={UserActivity} />
</Route>
</Route>
</Route>
If I understand it correctly and you want to redirect from /users/1 to /users/1/profile and /users/2 to /users/2/profile, you can simply replace ${currentUser().id} with :userId.
So your code would looks like this:
<Route onEnter={requireLogin}>
<Route path="users" component={Users}>
<Route path=":userId" component={User}>
<IndexRedirect to="profile" />
<Route path="profile" component={UserProfile} />
<Route path="settings" component={UserSettings} />
<Route path="activity" component={UserActivity} />
</Route>
</Route>
</Route>
Because the redirect is in the context of the route :userId: you can simply define your target route relative.

React-Router: Adding in a landing page

With React-Router there is a nesting concept that makes it hard for me to imagine where I should inject my landing page.
For example:
<Route path="/" component={App}>
<Route path="login" component={Login} />
<Route path="logout" component={Logout} />
<Route path="about" component={About} />
<Route path="dashboard" component={Dashboard} onEnter={requireAuth} />
</Route>
In that example part of the top-level component is shared with the rest of the components.
But with a landing page, there might not be the parent-child relationship for you to fit it into the routing.
Ergo, how do you manage putting in a landing page when using react-router?
Ok, let me answer here then.
The way you could do it is pretty straight forward, I think, if I understood you correctly.
I would do something like:
<Route path='/' component={LandingPage}>
<Route path='app' component={App}>
<Route path="login" component={Login} />
<Route path="logout" component={Logout} />
<Route path="about" component={About} />
<Route path="dashboard" component={Dashboard} onEnter={requireAuth} />
</Route>
</Route>
and then only include this.props.children in your App component.
I couldn't get any of the above answers to work so I decided to just do it my way. Here is what I did:
<Route path="/">
<IndexRoute component={Landing}/>
<Route path="/" component={App}>
<Route path="login" component={LoginOrRegister} onEnter={redirectAuth}/>
<Route path="dashboard" component={Dashboard} onEnter={requireAuth}/>
<Route path="vote" component={Vote} onEnter={requireAuth}/>
<Route path="about" component={About}/>
</Route>
</Route>
The Main component contains the minimal required for both the landing page and the App itself. In my case, they share nothing so the component is simply a wrapping div.
const Main = ({children}) => {
return (
<div>
{children}
</div>
);
};
Ran into this with the latest version (v6) of react-router-dom which no longer supports IndexRoute. After trying a few different approaches, it turned out to be relatively simple to add to the final catch-all where we normally specify our 404 page...
<Switch>
<Route
path="/learn?"
component={Learn}
/>
<Private
path="/search?"
component={Search}
/>
<Route
component={match.isExact ? Landing : Lost}
/>
</Switch>
This is within a top-level Application component that handles the root (/) path so match.isExact means we're on the home page. Above and below the <Switch> you can render any elements present on every page (e.g. a header and footer).
Hope this helps anyone coming across this question in 2022 and beyond.
React-router V6 supports index as an attribute of Route component
Index Route - A child route with no path that renders in the parent's outlet at the parent's URL.
See docs https://reactrouter.com/docs/en/v6/getting-started/concepts under "Defining Route" heading.
<Route path="/" element={<App />}>
<Route index element={<Home />} />
<Route path="teams" element={<Teams />}>
<Route path=":teamId" element={<Team />} />
...
</Route>
</Route>
</Routes>```
The IndexRoute in React-Router lets you define the default component when reaching a portion of your application. So with the following code, if you go to "/", the page will render the App component and inject the LandingPage component into its props.children.
<Route path="/" component={App}>
<IndexRoute component={LandingPage} />
<Route path="login" component={Login} />
<Route path="logout" component={Logout} />
<Route path="about" component={About} />
<Route path="dashboard" component={Dashboard} onEnter={requireAuth} />
</Route>

How to set the DefaultRoute to another Route in React Router

I have the following:
<Route name="app" path="/" handler={App}>
<Route name="dashboards" path="dashboards" handler={Dashboard}>
<Route name="exploreDashboard" path="exploreDashboard" handler={ExploreDashboard} />
<Route name="searchDashboard" path="searchDashboard" handler={SearchDashboard} />
<DefaultRoute handler={DashboardExplain} />
</Route>
<DefaultRoute handler={SearchDashboard} />
</Route>
When using the DefaultRoute, SearchDashboard renders incorrectly since any *Dashboard needs to rendered within Dashboard.
I would like for my DefaultRoute within the "app" Route to point to the Route "searchDashboard". Is this something that I can do with React Router, or should I use normal Javascript (for a page redirect) for this?
Basically, if the user goes to the home page I want to send them instead to the search dashboard. So I guess I'm looking for a React Router feature equivalent to window.location.replace("mygreathostname.com/#/dashboards/searchDashboard");
You can use Redirect instead of DefaultRoute
<Redirect from="/" to="searchDashboard" />
Update 2019-08-09 to avoid problem with refresh use this instead, thanks to Ogglas
<Redirect exact from="/" to="searchDashboard" />
Source:
https://stackoverflow.com/a/43958016/3850405
Update for version 6.4.5 to 6.8.1 <:
Use replace={true} for Navigate component.
<Routes>
<Route path="/" element={<Navigate to="/searchDashboard" replace={true} />}>
<Route path="searchDashboard" element={<SearchDashboard/>} />
<Route
path="*"
element={<Navigate to="/" replace={true} />}
/>
</Route>
</Routes>
https://reactrouter.com/en/6.4.5/components/navigate
https://reactrouter.com/en/6.8.1/components/navigate
Thanks to #vicky for pointing this out in comments.
Update:
For v6 you can do it like this with Navigate. You can use a "No Match" Route to handle "no match" cases.
<Routes>
<Route path="/" element={<Navigate to="/searchDashboard" />}>
<Route path="searchDashboard" element={<SearchDashboard/>} />
<Route
path="*"
element={<Navigate to="/" />}
/>
</Route>
</Routes>
https://reactrouter.com/docs/en/v6/getting-started/tutorial#adding-a-no-match-route
https://stackoverflow.com/a/69872699/3850405
Original:
The problem with using <Redirect from="/" to="searchDashboard" /> is if you have a different URL, say /indexDashboard and the user hits refresh or gets a URL sent to them, the user will be redirected to /searchDashboard anyway.
If you wan't users to be able to refresh the site or send URLs use this:
<Route exact path="/" render={() => (
<Redirect to="/searchDashboard"/>
)}/>
Use this if searchDashboard is behind login:
<Route exact path="/" render={() => (
loggedIn ? (
<Redirect to="/searchDashboard"/>
) : (
<Redirect to="/login"/>
)
)}/>
I was incorrectly trying to create a default path with:
<IndexRoute component={DefaultComponent} />
<Route path="/default-path" component={DefaultComponent} />
But this creates two different paths that render the same component. Not only is this pointless, but it can cause glitches in your UI, i.e., when you are styling <Link/> elements based on this.history.isActive().
The right way to create a default route (that is not the index route) is to use <IndexRedirect/>:
<IndexRedirect to="/default-path" />
<Route path="/default-path" component={DefaultComponent} />
This is based on react-router 1.0.0. See https://github.com/rackt/react-router/blob/master/modules/IndexRedirect.js.
UPDATE : 2020
Instead of using Redirect, Simply add multiple route in the path
Example:
<Route exact path={["/","/defaultPath"]} component={searchDashboard} />
Jonathan's answer didn't seem to work for me. I'm using React v0.14.0 and React Router v1.0.0-rc3. This did:
<IndexRoute component={Home}/>.
So in Matthew's Case, I believe he'd want:
<IndexRoute component={SearchDashboard}/>.
Source: https://github.com/rackt/react-router/blob/master/docs/guides/advanced/ComponentLifecycle.md
Since V6 was released recently, the accepted answer won't work since Redirect no more exists in V6. Consider using Navigate.
<Route path="/" element={<Navigate to="/searchDashboard" />} />
Ref:- V6 docs
import { Route, Redirect } from "react-router-dom";
class App extends Component {
render() {
return (
<div>
<Route path='/'>
<Redirect to="/something" />
</Route>
//rest of code here
this will make it so that when you load up the server on local host it will re direct you to /something
May 2022
Import Navigate
import { Routes, Route, Navigate } from 'react-router-dom';
Add
<Route path="/" element={<Navigate replace to="/home" />} />
For example:
import React from 'react';
import { Routes, Route, Navigate } from 'react-router-dom';
import Home from './pages/Home';
import Login from './pages/Login';
const Main = () => {
return (
<Routes>
<Route path="/" element={<Navigate replace to="/home" />} />
<Route path='home' element={<Home />}></Route>
<Route path='login' element={<Login />}></Route>
</Routes>
);
}
export default Main;
Done!
I ran into a similar issue; I wanted a default route handler if none of the route handler matched.
My solutions is to use a wildcard as the path value. ie
Also make sure it is the last entry in your routes definition.
<Route path="/" component={App} >
<IndexRoute component={HomePage} />
<Route path="about" component={AboutPage} />
<Route path="home" component={HomePage} />
<Route path="*" component={HomePage} />
</Route>
For those coming into 2017, this is the new solution with IndexRedirect:
<Route path="/" component={App}>
<IndexRedirect to="/welcome" />
<Route path="welcome" component={Welcome} />
<Route path="about" component={About} />
</Route>
<Route name="app" path="/" handler={App}>
<Route name="dashboards" path="dashboards" handler={Dashboard}>
<Route name="exploreDashboard" path="exploreDashboard" handler={ExploreDashboard} />
<Route name="searchDashboard" path="searchDashboard" handler={SearchDashboard} />
<DefaultRoute handler={DashboardExplain} />
</Route>
<Redirect from="/*" to="/" />
</Route>
The preferred method is to use the react router IndexRoutes component
You use it like this (taken from the react router docs linked above):
<Route path="/" component={App}>
<IndexRedirect to="/welcome" />
<Route path="welcome" component={Welcome} />
<Route path="about" component={About} />
</Route>
Firstly u need to install:
npm install react-router-dom;
Then u need to use your App.js (in your case it can be different) and do the modification below.
In this case I selected the Redirect to get proper rendering process.
import { BrowserRouter as Router, Route, Switch, Redirect } from "react-router-dom";
<Router>
<Suspense fallback={<Loading />}>
<Switch>
<Route exact path="/">
<Redirect to="/Home" component={Routes.HomePage}/>
</Route>
<Route exact path="/Biz" component={Routes.Biz} />
</Switch>
</Suspense>
</Router>
U successfully do the modification above u can see the redirect URL is on your browser path and rendering process also working properly according to their component.
Some time ago, we had an opportunity to use the component named "DefaultRoute" in the react routing.
Now, its depreciated method, and it’s not so popular to use it, you can create the custom route named default or whatever, but still, it’s not how we do it in modern React.js development.
It’s just because using the "DefaultRoute" route, we can cause some rendering problems, and its the thing that we definitely would like to avoid.
Here is how I do it-
<Router>
<div className="App">
<Navbar />
<TabBar />
<div className="content">
<Route exact path={["/default", "/"]}> //Imp
<DefStuff />
</Route>
<Route exact path="/otherpage">
<Otherstuff />
</Route>
<Redirect to="/defult" /> //Imp
</div>
</div>
</Router>
Use:
<Route path="/" element={<Navigate replace to="/expenses" />} />
In context:
import { BrowserRouter, Routes, Route, Navigate } from "react-router-dom";
<BrowserRouter>
<Routes>
<Route element={<App />}>
<Route path="/expenses" element={<Expenses />} />
<Route path="/invoices" element={<Invoices />} />
</Route>
<Route path="/" element={<Navigate replace to="/expenses" />} />
</Routes>
</BrowserRouter>
You use it like this to redirect on a particular URL and render component after redirecting from old-router to new-router.
<Route path="/old-router">
<Redirect exact to="/new-router"/>
<Route path="/new-router" component={NewRouterType}/>
</Route>

Categories