I try to make some customized routes for components with protected function but route is not working:
import React from "react";
import "./styles.css";
import { BrowserRouter, Switch, Route, useHistory } from "react-router-dom";
const RoutesRender = ({ Routes }) => {
const History = useHistory();
if (Routes.AuthRequired) {
History.push("/login");
return null;
} else {
return (
<Route exact path={Routes.Path} render={(props) => <Routes.Component {...props} />} />
);
}
};
const RoutesProvider = () => {
return (
<React.Fragment>
<BrowserRouter>
<Switch>
{[
{
Path: "/login",
Component: () => <div>"login"</div>,
Title: "Login",
AuthRequired: false
},{
Path: "/register",
Component: () => <div>"register"</div>,
Title: "Register",
AuthRequired: false
},{
Path: "/",
Component: () => <div>"root"</div>,
Title: "Janus Chat",
AuthRequired: true
}
].map((Routes, Index) => (
<RoutesRender Routes={Routes} key={Index} />
))}
</Switch>
</BrowserRouter>
</React.Fragment>
);
};
export default function App() {
return <div className="App">{RoutesProvider()}</div>;
}
It's not show any error put it's show first object from array and not looping on others. if i put Route inside map it's work but if i put in function not work.
{RoutesMaster.map((Routes, Index) => (
<Route exact key={Index} path={Routes.Path} render={(props) => <Routes.Component {...props} />}/>
))}