I have been having trouble with this problem for a bit now. I use forEach to loop through an array, and I want the correct page to render with the corresponding index when I click on the component. Right now my issue is that I loop through my array, but I am not able to return the correct index, only the first one in the array. I want the startPage prop on the Pages component to render to correct index from my newNum variable.
const itemId = this.props.navigation.getParam('pk');
let newArray = this.props.moment.filter(item => {
return item.trip == itemId
});
console.log('getting moment fromt trip')
let num = Object.keys(this.props.trip[0].moments)
let newNum = num.forEach((number, index) => {
console.log(number)
return number
})
return (
// <View style={{flex:1, backgroundColor: '#F0F5F7'}} {...this.panResponder.panHandlers}>
<View style={{flex:1, backgroundColor: '#F0F5F7'}}>
<HeaderMomentComponent navigation={this.props.navigation} />
<Pages indicatorColor="salmon" startPage={newNum}>
{newArray.map((item, index) => {
console.log('this is the index')
console.log(index)
return(
<MomentContent
name={item.name}
place={item.place}
description={item.description}
tags={item.tags}
key={index}
/>
)
})}
</Pages>
</View>
);
forEachdoesn't return anything. Are you looking formap?newArraycoming from?mapbecause you're just returning the element. You would be getting the exact same array. What's the goal withnewNum?