I am trying to redirect user to not-found
url when unknown url is given,
and I am using history.push('not-found')
for same,
but it doesn't work, inside ComponentwillReceiveProps()
.
componentWillReceiveProps(nextProps) {
if (nextProps.profile.profile === null &&
this.props.profile.loading) {
this.props.history.push("/not-found");
}
}
In App.js
, using BrowserRouter as Router
, I am rendering component NotFound
, as
But Problem is {NotFound}
is shown to only path /not-found
, not for other incomplete paths,
How can I show NotFound Component for other unspecified routes?
app.js
is
import React, { Component } from "react";
import { BrowserRouter as Router, Route, Switch } from "react-router-dom";
import { Provider } from "react-redux";
import jwt_decode from "jwt-decode";
import store from "./store";
import { setCurrentUser, logoutUser } from "./actions/authActions";
import setAuthToken from "./utils/setAuthToken";
import Navbar from "./components/layout/Navbar";
import Footer from "./components/layout/Footer";
import Landing from "./components/layout/Landing";
import Register from "./components/auth/Register";
import Login from "./components/auth/Login";
import Dashboard from "./components/dashboard/Dashboard";
import CreateProfile from "./components/create-profile/CreateProfile";
import EditProfile from "./components/edit-profile/EditProfile";
import AddExperience from "./components/add-credentials/AddExperience";
import AddEducation from "./components/add-credentials/AddEducation";
import Profiles from "./components/profiles/Profiles";
import Profile from "./components/profile/Profile";
import PrivateRoute from "./components/common/PrivateRoute";
import NotFound from "./components/not-found/NotFound";
import "./App.css";
import { clearCurrentProfile } from "./actions/profileActions";
// check if token is stored in localstorage, if user has logged in.
if (localStorage.jwtToken) {
// if token exitst, then set auth token header auth
setAuthToken(localStorage.jwtToken);
// now decode token and get user-info, expiry-date of token
const decoded = jwt_decode(localStorage.jwtToken);
// set user and user is now authenticated
store.dispatch(setCurrentUser(decoded));
// check for expired date of token
const currentTime = Date.now() / 1000;
if (decoded.exp < currentTime) {
// exp should be greater to keep alive login
store.dispatch(logoutUser());
// Logout user and clear it's profile
store.dispatch(clearCurrentProfile());
}
}
class App extends Component {
render() {
return (
<Provider store={store}>
<Router>
<div className="App">
<Navbar />
<Route exact path="/" component={Landing} />
<div className="container">
<Route exact path="/register" component={Register} />
<Route exact path="/login" component={Login} />
<Route exact path="/profiles" component={Profiles} />
<Route exact path="/profile/:handle" component={Profile} />
<Switch>
<PrivateRoute exact path="/dashboard" component={Dashboard} />
</Switch>
<Switch>
<PrivateRoute
exact
path="/create-profile"
component={CreateProfile}
/>
</Switch>
<Switch>
<PrivateRoute
exact
path="/edit-profile"
component={EditProfile}
/>
</Switch>
<Switch>
<PrivateRoute
exact
path="/add-experience"
component={AddExperience}
/>
</Switch>
<Switch>
<PrivateRoute
exact
path="/add-education"
component={AddEducation}
/>
</Switch>
<Route exact path="/not-found" component={NotFound} />
<Route component={NotFound} />
</div>
<Footer />
</div>
</Router>
</Provider>
);
}
}
export default App;
package.json
is
{
"name": "frontend",
"version": "0.1.0",
"private": true,
"dependencies": {
"axios": "^0.18.0",
"classnames": "^2.2.6",
"moment": "^2.22.2",
"react": "^16.5.2",
"react-dom": "^16.5.2",
"react-moment": "^0.8.2",
"react-redux": "^5.0.7",
"react-router-dom": "^4.3.1",
"react-scripts": "2.0.5",
"redux": "^4.0.1",
"redux-thunk": "^2.3.0"
},
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
"eslintConfig": {
"extends": "react-app"
},
"proxy": "http://localhost:5000",
"browserslist": [
">0.2%",
"not dead",
"not ie <= 11",
"not op_mini all"
]
}
in connect-geeks/frontend/src/components/profile/Profile.js
file,
I am using method history.push(path)
,
also withRouter
didn't worked, on wrapping connect()
in it.
edit 1:
after adding Route with path="*" as,
<Switch>
<Route exact path="/not-found" component={NotFound} />
<Route path="*" component={NotFound} />
</Switch>
it didn't worked as
I tried this solution on StackOverflow , but i couldn't apply it for both private and public routes.
Add a new Route
to your router, with the path="*"
, that will catch all the unmapped routes.
<Route path="*" component={NotFound}/>
I recommend you to read this: React-Router v4 - Handling 404 pages
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.