I am trying to map objects in my array that is working great but becomes a problem when I try to map just one of the objects. My array code is as follows
const slides = [
{
index: 0,
title: 'Slide',
path: '/slide',
component: slide,
},
{
index: 1,
title: 'Slide1',
path: '/slide1',
component: slide1,
},
{
index: 2,
title: 'Slide12',
path: '/slide2',
component: slide2,
},
];
Here is my jsx code
{slides.map((item) => {
if (auth.verify(Roles.role1)) {
return (
<Tab
label={item.title}
key={item.index}
component={Link}
to={item.path}
/>
);
} else {
return (
<Tab
label={slides[0].title}
key={slides[0].index}
component={Link}
to={slides[0].path}
/>
)
}
})}
}
This code is working great for the first part of the if but for the else it is putting the first object of the array [0] three times because of the .map(item) so I have tried putting the if statement outside of it like this
if (auth.verify(Roles.role1)) {
{slides.map((item) => {
but this does not let me run the code and I'm unsure why. I am new to react and would appreciate anyway for the if statement to run correctly without iterating through all three array objects.