Relatively new to react and javascript, but was trying to organize a project with some routes being pulled from a different constant so I could use them in another file so that someone could develop and test on a separate module without needing the full application (pseudo micro frontends type stuff). this is likely over-engineering, but basically, when I put my routes in a const and reference them from somewhere else, any route below my const references just loads a blank page instead of the components or html.
Any guidance would be appreciated.
(react 17.0.2 and react-router-dom 5.1.2)
routes.js):
import { BrowserRouter, Route, Switch, Redirect } from "react-router-dom";
import * as Yup from 'yup';
import Location from 'app-location';
import * as profile from '@profile-views';
import * as view from "modules/app/views";
...
const PROFILE_ROOT = `/profile`;
const userId = Yup.string();
/* Profile Routes */
export const Profile = new Location(
`${PROFILE_ROOT}/:userId`,
{
userId: userId.required()
}
);
export const ProfileContactInfo = new Location(
`${Profile.path}/contact`,
{ userId: userId.required()},
);
export const ProfileCalendar = new Location(
`${Profile.path}/calendar`,
{ userId: userId.required()},
);
...
export const renderRoutes = (
<>
{/* everything works if Route components are in Routes() */}
<Route exact path={Profile.path} component={profile.Timeline}/>
<Route path={ProfileCalendar.path} component={profile.Calendar}/>
<Route path={ProfileContactInfo.path} component={profile.ContactInfo}/>
</>
)
export default function Routes() {
return (
<BrowserRouter>
<Switch>
<Route path="/login" component={view.Login} />
{ renderRoutes }
{/* EVERYTHING ABOVE WORKS */}
{/* EVERYTHING BELOW renderRoutes DOES NOT WORK */}
{/* everything below works if i remove renderRoutes */}
{/* everything works if i copy Routes from renderRoutes here */}
<Route path="/create-user" component={view.CreateUser} />
<Route path="/404" component={() => <h1>Not Found!</h1>} />
<Redirect to="/404" />
</Switch>
</BrowserRouter>
);
}
App.js:
import Routes from "./routes";
...
function App() {
return (
<UserProvider>
<Routes />
</UserProvider>
);
}