score:1
You could check to see if this.props.match.username
exists. If it does then render as normal if it doesnt then use React-Router Redirect Component to redirect to a 404
component. If you have multiple paths that require this functionality then you may consider making a HOC for this purpose.
score:2
Use Switch then Redirect
https://reacttraining.com/react-router/web/example/no-match
https://reacttraining.com/react-router/web/api/Redirect
Valid URL no redirect: /user/valid_username
404 URL redirects: /user/invalid_username
import React, { Component } from 'react'
import { BrowserRouter as Router, Route, Switch, Redirect } from 'react-router-dom'
class App extends Component {
render() {
return (
<Router>
<div>
<Switch>
<Route path="/user/:username" component={Child} />
<Route path="/404" component={NoMatch} />
<Route component={NoMatch} />
</Switch>
</div>
</Router>
)
}
}
function Child({ match }) {
// perform some username check
if (match.params.username !== 'valid_username') {
return (
<div>
<Redirect to="/404" />
</div>
)
}
return (
<div className="App">
<h3>ID: {match.params.username}</h3>
</div>
)
}
function NoMatch({ location }) {
return (
<div>
<h3>
404 - No match for <code>{location.pathname}</code>
</h3>
</div>
)
}
export default App
Source: stackoverflow.com
Related Query
- How to properly render a 404 page in React with React-Router?
- How to properly set property(if user is logged) from cookie after page opens or F5 in React with router and redux
- How can I use Multiple layouts with 404 page in React router
- How to let react router respond with 404 status code?
- How to navigate to another page with a smooth scroll on a specific id with react router and react scroll
- React Router V4: How to render a modal in the same screen changing only the url and then with that url, be able to rebuild the whole screen
- issue with how to render with react router
- React Router how to return 404 page in case of dynamic routes expecting a parameter?
- How to 1). put a div , 2). render a component without Route inside <Routes> with React Router in v6 React?
- How can I add multiple path for home page with react router dom 6?
- How to mock properly with Jest a React component using render props
- React, how to pass product id to details page with React Router Dom?
- React Redux one-page app: handle an API 404 with full page render
- My 404 not found page with React router is not working
- How to add a 404 page in react with nested routing?
- React Router v6 : How to render multiple component inside and outside a div with the same path
- how do I make a 404 redirect with react router 6?
- How to let React Router v6 respond with 404 status code
- 404 custom page not working with useContext and React Router
- React Router - How to hide components on 404 page
- React Router v6 - how to handle nested routes but with only current page showing instead of previous pages
- How to give props from one page to another with react Router
- How to render different component with react router after user logged in
- How to create a new page and route programatically in React.js with React Router
- How to link an image to a page dynamically with React Router Dom?
- Im using React Router and I want one of my route links with path="/" to be the home page on reload.. How can you do that?
- With React Router : How to render a component multiple times with differents props each time in a Route?
- React Router with redirection to login page but also to have 404 NotFound route
- How to render an iframe of local page within React app using React Router Dom
- React router - How to route to a page with specific post id
More Query from same tag
- Formik and yup validation not working for required
- Pass props from function? React
- Is there a way to apply physics in framer motion?
- TypeError: __WEBPACK_IMPORTED_MODULE_3__ is not a function
- xlsx is undefined when trying to export excel sheet
- Dynamically add properties to this.state in React
- ReactJS - Data Table did not re-render/update when state changed
- How to send updated state in axios in React?
- Highcharts - Format X axis with 2 digit hours in 24 hour format
- How to import JS library and script to React project?
- Type '{}' is missing the following properties from type - TS error
- how to debug react router not loading component on url change
- Blur validation gets triggered before input selection fills the data field - ReactJS
- delete nested object base on key in react
- How to run Redux DevTools Extension INSIDE Chrome Extension running React?
- displaying data based on filter - react todo list
- Is passing a function to UseEffect valid? Can this be done to replace useCallback?
- How to conditionally route to a component in react based on a variable in the route?
- Passing external values to the react component
- React Router Dom v6 Detect user leave page and do somthing
- How to reset the value of a nested object?
- Not getting data from api into the web page, can anyone help me through this where I am getting an error and what can i do to get data
- React Bootstrap Alert Default true
- I have a simple react contact me form, the email it is supposed to send from nodemailer is coming in empty
- React/MobX component not re-rendering after state change (found a weird solution, would like to know why)
- How to add facebook authentication to my react app?
- onKeyDown Event in React with TypeScript
- Passing values into the `next` function in Redux Saga
- React.js The select box option does not go into jsx and $$typeof: Symbol(react.element)
- Bootstrap 4 card usage with responsive next/image