An open API service indexing awesome lists of open source software.

https://github.com/ihoverlord/react-router-sample

Sample React project to define routes with JSON config and redirecting users on 404
https://github.com/ihoverlord/react-router-sample

react react-router react-router-dom reactjs

Last synced: 12 months ago
JSON representation

Sample React project to define routes with JSON config and redirecting users on 404

Awesome Lists containing this project

README

          

### React Routing sample

Sample project to test defining routes with JSON and handle redirecting on 404

1. Define routes in a JSON

```
const routes = {
'/default': {
component: Home
},
'/dashboard': {
component: Dashboard
},
'/profile': {
component: Profile
}
}
```

2. Create a function to define routes dynamically

```
const DefineRoutes = ({ routes }) => {
const paths = Object.keys(routes)
let location = useLocation();
// console.log({ location, has: routes[location.pathname] })
if (!routes.hasOwnProperty(location.pathname)) return
if (paths && paths.length > 0) {
return (
<>
{paths.map(path => )}
>
)
}

}
```
This function handles 404 and redirects the user to `/`

3. Define router and import the routes

```




```