React Native: Issue when rendering with map and index

Viewed 38

I have a multi-step form and in the top header I want to show some kind if breadcumbs, I'm trying to make it so between each breadcumb there's a line but currently I can only show a line for the last case in my condition (in this case, between 3 and 4, when it should look something like:

1 - 2 - 3- 4

while currently I have:

1 2 3 - 4

Have a look at my image to know what I'm talking about:

enter image description here

My code is as follows:

const [ steps, setSteps ] = useState([ { title:'Detalles' }, { title:'Horario' }, { title:'Fotos' }, { title:'Menu' } ]);

<View style={{ width:'100%', backgroundColor:'white',flexDirection:'row', alignItems:'center', justifyContent:'space-around', position:'relative' }}>
        {steps.map((step,index) => {return (
            <>
            <TouchableOpacity onPress={ ()=>{ onStepPressed(index+1) } } style={{ width:30,height:30,borderRadius:30/2, ...venueStore.currentStep == index+1 ? { backgroundColor:'rgb(3,91,150)' } : { backgroundColor:'rgb(150,150,150)' }, alignItems:'center',justifyContent:'center' }}>
                <Text style={{ fontSize:14, color:'white' }}>{index+1}</Text>
            </TouchableOpacity>
            { index == 0 || index == 1 || index == 2 && (<View style={{ width:45,height:2,backgroundColor:'rgb(68,68,68)' }}></View>)}
            </>
        )})}
        </View>

Any idea how I can achieve my desired result

2 Answers

The condition must be combined

  {(index == 0 || index == 1 || index == 2) && (
     <View
       style={{ width: 45, height: 2, backgroundColor: "rgb(68,68,68)" }}
     />
  )}

Try checking the other way around:

{
  index !== steps.length - 1 
  && <View style={{width:45,height:2,backgroundColor:'rgb(68,68,68)'}} />
}
Related