Search code examples
javascriptlistreact-nativereact-native-flatlist

Add two button for each item in flatlist


Actually i'm working for a school project in react native and i want to know if it's possible to add two buttons into flatlist using react-native.


export class ItineraryScreen extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
        data:[
                {key: 'PKRX'},
                {key: 'UXUA'},
                {key: 'PGRF'},
            ]
    };
}

render() {
        return (
            <ScrollView>
                    <FlatList
                        data={this.state.data}
                        renderItem={({item}) => <Text style={styles.item}>{item.key}</Text>}
                    />
            </ScrollView>
        );


}


could you give some advices to implement this feature please?

Best regards,

Imad.


Solution

  • Here is an example of how yu could do it (Repro on Snack Expo) :

    import * as React from 'react';
    import { Text, View, StyleSheet , FlatList, Button} from 'react-native';
    
    export default function App() {
      const data = [
                    {key: 'PKRX'},
                    {key: 'UXUA'},
                    {key: 'PGRF'},
                ];
    
      return (
        <View>
          <FlatList data={data} renderItem={({item}) => <Item item={item} /> } />
        </View>
      );
    }
    
    const Item = ({item}) => {
      return (
        <View style={{flex: 1, flexDirection: 'row', alignItems: 'center'}}>
          <Text>{item.key}</Text>
          <View style={{flex:1, flexDirection: 'row-reverse'}}>
            <Butto title='Button 1' onPress={() => {}} />
            <Button title='Button 2' onPress={() => {}} />
          </View>
        </View>
      )
    }