Home > database >  Displaying a List in React Native Map Method
Displaying a List in React Native Map Method

Time:10-29

  1. How can Fix the error: Each child in a list should have a unique "key" prop.
  2. How can make every render View selectable?

EXPO SNACK

CodePudding user response:

I think this answers your question: https://snack.expo.dev/xtbdjO-bB

For the highest level element in your .map (in this case the TouchableOpacity) you need to add a key prop which is unique. Handily you have an ID which is perfect.

To make things selectable, on press I added the ID of the pressed item into an array saved to state selectedId. Then in your styles if the elements id is in the array of selectedId then show it blue. Else red.

CodePudding user response:

Ugly way but: you could use array.map() and put your view as an element

  • Related