I'm building an application that will call out to an API prior to bootstrapping React. This API will tell me which features I need to load based on the user's subscription.
The API will return an array of objects with the pattern outlined below
links: [
{
label: 'Payment Reconciliation',
component: './components/_paymentReconciliation/paymentReconciliationPage',
url: '/paymentreconciliation'
},
{
label: 'Device Integration',
component: './components/_deviceIntegration/deviceIntegrationPage',
url: '/deviceintegration'
}
]
It's easy enough to map the links to produce dynamic Routes
export default function Routes() {
let featureRoutes = config.FEATURES.links.map((menuItem, i) => {
return (
<Route path={menuItem.url} component={require(menuItem.component).default} key={i} />
)
});
return (<div>
<Route exact path="/" component={LandingPage} />
{featureRoutes}
</div>
)
}
This obviously isn't working. I'm struggling figuring out how to not need an import statement for every possible Component that might be loaded. Also should the API be returning the path to the component or just the component name? I've seen examples using something like handler={require(PathToComponent)} That hasn't proven to work either.
Any help here would be appreciated