score:10
Accepted answer
you can use
import { redirect } from 'react-router-dom';
and add this route inside your switch:
<route render={() => <redirect to={{pathname: "/"}} />} />
it'll catch anything that has no route.
score:4
if you want to redirect home page when type invalid path string so that follow this code....
<switch>
<route exact path="/" component={home} />
<route path="/login" component={login} />
<route path="/signup" component={signup} />
<route path="/user" component={user} onenter={this.requireauth}/>
<route path="*" component={home} />
</switch>
here i used * in path that means if invalid path string type then it auto redirect to home page.
Source: stackoverflow.com
Related Query
- How to redirect to home page if URL is invalid?
- How to redirect to home page from custom 404 page with locale in the url nextjs
- How to server side redirect to other page without reloading the page and still keeping the url in Nextjs app?
- how to redirect to home page after submitting redux-form?
- How to redirect to another page if user gets to a specific URL in Next.js?
- how to redirect to home page in react using useHistory Hook
- How could i use the routing in reactjs to redirect from one page after loading to another url
- Can't figure out how to redirect to home page
- How to redirect to home page right after successful Authorization?
- How can redirect to Home page once a user is logged in?
- How to redirect to 404 page if data is invalid in getStaticProps?
- How to redirect the application to home page when refreshing the browser?
- How to redirect to Home page after rendering the Header component in React?
- In react, how do I redirect back the to current page I'm on and include a url parameter
- How can I redirect to error page when wrong url is entered?
- how do i redirect to root url when i reload a page in react js
- How to redirect to home page after logout in React
- how to redirect to home page after login: ReactJS?
- How to redirect from login page to home page if the authentication success in react
- How to hard code simple authentication (no backend) and redirect to home page - React
- how to redirect home page if only login is success .react js node js with jwt
- How to redirect to a new page from a function in React?
- How to get page URL or hostname in NextJs Project?
- How to redirect to a gatsby-react page with an onclick event of a component?
- How to redirect page with Javascript in React-Router?
- JS: How to pass url through redirect function to login function
- How to make material ui icon clickable and redirect to url
- Redirect changing page URL but not rendering new page
- How redirect to login page when got 401 error from ajax call in React-Router?
- How to make a button redirect to another page in React
More Query from same tag
- React - Redux-Form Remote Submit
- Sinon.spy() & jest.spyOn - TypeError: Attempted to wrap undefined property getTableData as function
- Use async/await in React component
- Material-UI: How to remove the transformation of InputLabel?
- Remove DOM in react.js,
- iOS 11 JavaScript can't find variable `globalThis`
- react-redux: dispatch action from reducer
- React: State not rendering in real time
- Custom navigation with Swiper React and typescript
- withRouter with react-redux connect causing error with typescript
- How to make fullscreen picture in a meteor/react setup?
- Pass function inside string literal to other component
- Material-UI component in React Router Link triggering the link
- Nextjs: Unable to load images from static folder
- Why does this comment get passed as an argument in Javascript/React code?
- classnames not appearing React
- Not able to fetch data from graphQL in gatbsy
- ReactJS/Typescript : Typing and properties of children
- Uncaught (in promise) TypeError: spawn is not a function
- Create dynamic svg icons component in ReactJs
- Can someone please explain me this code from ReactJS?
- React router, how to save transition and redirect upon successful login
- How can I utilise ApplicationInsights-JS in a service worker?
- Why the variable in styled-jsx tag didn't work?
- why destroyOnClose={true} not working in React
- React Conditional Rendering Child
- Express auth on a create-react-app project
- How do I convert my react js class component to a functional component?
- React useEffect(), fetch data with React Hooks and set the formik form
- Is there a way to apply a fade in/out transition with React suspense for the fallback component?