In React Native (I'm very new to React) I have a javascript function that returns an array in a variable called myResult. Printed in the terminal it shows:
Array [
25,
25,
20,
10,
5,
5,
]
which I add to the state with
this.setState({resultArray: myResult});
Then, in the state I have an array like so:
this.state = {
foodList: [
{id: "25",
src: `"./images/banana25.png"`
},
{id: "20",
src: `"./images/banana20.png"`
},
{id: "15",
src: `"./images/apple15.png"`
},
{id: "10",
src: `"./images/mango10.png"`
},
{id: "5",
src: `"./images/steakNeggs5.png"`
},
}
Using the resultArray I want to show an image in a view for each item, the image corresponding to the id which matches each array item.
<View style={{height: 318, flexDirection: "row" }}>
{this.state. resultArray.map((item, key) => {
let src = this.state.foodList.item.src
return (
<Image key={key} style={{flexDirection: "row" }} source={require({src})} />
);
})}
</View>
Obviously, that doesn't work as I don't have the syntax right for src.
Can someone show me the correct syntax to get the src from the foodList array?
Also, according to the docs, using the index as the key works but is frowned upon, but since the resultArray can (and in this case, does) hold identical numbers/strings, using the array items, I assume, is not possible. Would using Math.random() be a way to create a key, considering the the output would only be making 1-10 Image components?
