I am trying to return my routes from an array using loop. I have an app.js file where my code is like this:
return (
<Switch>
<Route path="/" component={() => <HomePage />} exact />
<Route path="/chooseStyles" component={FormContainer} />
<Route path="/heightAndWeight" component={FormContainer} />
<Route path="/bodyShape" component={FormContainer} />
</Switch>
);
I am trying to make my routes dynamic and loop through an array. But I am unable to make it work. For that, I am trying this code:
return (
<Switch>
{a.map((b,i) => <Route path={b[i].url} component={() => <Global_Container name={a[0].path} />} />)}
<Route path="/" component={() => <HomePage />} exact />
<Route path="/chooseStyles" component={FormContainer} />
<Route path="/heightAndWeight" component={FormContainer} />
<Route path="/bodyShape" component={FormContainer} />
</Switch>
);
Using code above I am getting errors. I have created a variable called a like this:
var a = [{"url":"/global","path":"maternityFit"},
{"url":"/global1","path":"clothFit"}
]
My code works fine when I am using code like this:
{a.map((b,i) => <Route path={a[0].url} component={() => <Global_Container name={a[0].path} />} />)}
I don't know how to make it work in my case. I am declaring my var a in code block:
export default function App() {
var a = [{"url":"/global","path":"maternityFit"},
{"url":"/global1","path":"clothFit"}
]
}
0index for thenameprop.