i hev a list of name and id and i want to display in screen with this componenent…
- User
i hev a list of name and id and i want to display in screen with this componenent <Text style={{color: colors.secondary, marginRight: 10}}>{item} <View style={{marginBottom: 1}}>{last ? null : }
- Assistant
Assuming you have an array of objects where each object has a "name" and an "id" property, you can map over that array and render each item using the TouchableOpacity component you provided.
Here's an example of how you can do it:
import React from 'react'; import { Text, View, TouchableOpacity } from 'react-native'; import Separator from './Separator'; // assuming you have a Separator component defined const ItemList = ({ data, onPress }) => { return ( <> {data.map((item, index) => ( <TouchableOpacity key={item.id} onPress={() => onPress(item)} style={[styles.item]} activeOpacity={0.5} > <View style={styles.section}> <Text style={{ color: colors.secondary, marginRight: 10 }}> {item.name} </Text> <Arrow /> </View> </TouchableOpacity> <View style={{ marginBottom: 1 }}> {index === data.length - 1 ? null : <Separator />} </View> ))} </> ); }; - User
import {View, Text, TouchableOpacity, ScrollView} from 'react-native'; import React from 'react'; import {AuthHeader} from '@components/headers/AuthHeader'; import {styles} from './style'; //LANGUAGES import {useTranslation} from 'react-i18next'; import {DATA} from '@helpers/fakedata/Data'; import {SousSection} from './index'; import {colors} from '@constants/colors'; import {Arrow} from '@assets/images/profile/arrowSection.svg'; const NameList = ({name}) => { return ; }; const Separator = () => { return ; }; export const reclamationScreen = ({navigation}) => { const {t, i18n} = useTranslation(); console.log(DATA); return ( <AuthHeader style={{paddingHorizontal: 20}} onPress={() => navigation.goBack()} showText={true} label={t('profile.profile')} /> <View style={{top: 20}}> Reclamations <ScrollView style={{marginTop: 30}}> /} <> {DATA.map((item, index) => ( <TouchableOpacity key={item.id} onPress={() => onPress(item)} style={[styles.item]} activeOpacity={0.5}> <Text style={{color: colors.secondary, marginRight: 10}}> {item.name} <View style={{marginBottom: 1}}> ))} </> {/ ); }; correct this code and make it better
- Assistant
- User
try with flatList
- Assistant
Sure, here's the code using
FlatListinstead ofScrollViewandTouchableOpacitycomponents:import React from 'react'; import { View, Text, FlatList } from 'react-native'; import { AuthHeader } from '@components/headers/AuthHeader'; import { styles } from './style'; import { DATA } from '@helpers/fakedata/Data'; import { colors } from '@constants/colors'; import Arrow from '@assets/images/profile/arrowSection.svg'; const Separator = () => { return <View style={styles.sectionSeparator} />; }; export default function ReclamationScreen({ navigation }) { const handlePressItem = (item) => { console.log(item); }; const renderItem = ({ item }) => ( <View key={item.id} style={styles.item}> <View style={styles.section}> <Text style={{ color: colors.secondary, marginRight: 10 }}> {item.name} </Text> <Arrow width={20} height={20} /> </View> <Separator /> </View> ); return ( <View style={styles.reclamationContainer}> <View style={styles.reasonDeleteAuthContainer}> <AuthHeader style={{ paddingHorizontal: 20 }} onPress={() => navigation.goBack()} showText={true} label="Profile" /> </View> <View style={{ top: 20 }}> <Text style={[styles.header]}>Reclamations</Text> </View> <FlatList data={DATA} renderItem={renderItem} keyExtractor={(item) => item.id.toString()} style={{ marginTop: 30 }} /> </View> ); }