I am trying to render array of data in form of card. However only one item will be render as single card at a time. Once user update card number, next item will be rendered. I am not able to call array item wise in render function.
map function for list doesn't work for me as it will populate whole array but I want user to input before next item.
This data is saved from API call
data = [
{ id: 1, text: 'Card #1', uri: 'https://images.unsplash.com/photo-1535591273668-578e31182c4f?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=f28261f0564880c9086a57ee87a68887&auto=format&fit=crop&w=500&q=60' },
{ id: 2, text: 'Card #2', uri: 'https://images.unsplash.com/photo-1535576434247-e0f50b766399?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=232f6dbab45b3f3a6f97e638c27fded2&auto=format&fit=crop&w=500&q=60' },
{ id: 3, text: 'Card #3', uri: 'https://images.unsplash.com/photo-1535565454739-863432ea3c0e?ixlib=rb-0.3.5&ixid=eyJhcHBfaWQiOjEyMDd9&s=7edfb9bc7d214dbf2c920723cb0ffce2&auto=format&fit=crop&w=500&q=60' },
];
This is array of Data. Now from render function:
renderCard() {
const buttons = ['Call', 'Fold', 'Raise']
const selectedIndex = this.state.selectedIndex
return (
<Card
title="Profiling Question "
image={{ uri: this.state.data[selectedIndex].uri }}
>
<Text style={{ marginBottom: 10 }}>
{this.state.data[selectedIndex].text}
</Text>
<Button
onPress={this.updateIndex}
/>
</Card>
);
}
UpdateIndex function updates index value. This is working fine.
I expect single card to be rendered with button to update to next card.
However I am getting this error:
TypeError: undefined is not an object(evaluting'this.state.data[selectedIndex].uri')
EDIT:
I have tried removing index too to check as below:
return (
<Card
title={"Question No: "+ this.state.selectedIndex +1 }
image={{ uri: this.state.data[0].question_image }}
>
<Text style={{ marginBottom: 10 }}>
{this.state.data[0].question_text}
</Text>
<Button
onPress={this.updateLn}>
<Text> Update Question </Text>
</Button>
</Card>
);
Still same error.
My state is as:
export default class QuizScreen extends Component {
constructor(props) {
super(props)
this.state = {
data: [],
selectedIndex: 0,
dataLength: 0,
cardIndex: 0,
};
this.updateIndex = this.updateIndex.bind(this)
this.updateLn = this.updateLn.bind(this)
}```
const { selectedIndex } = this.state??