Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass props in the component to FlatList renderItem

I have a Function as a prop in my component and I have to pass this Function Prop to another Component in the renderItem in FlastList. How to do that? Here is my code.

import React, { Component } from 'react';
import { View } from 'native-base';
import PropTypes from 'prop-types';
import { FlatList } from 'react-native';
import AddPlayers from '../AddPlayers/AddPlayers';
import League from '../League/League';
export default class InviteLeagues extends Component {
  static propTypes = {
    invitedLeagues: PropTypes.Array,
    label: PropTypes.string.isRequired,
    InvitedLeaguesList: PropTypes.Array,
    onPress: PropTypes.func.isRequired
  };

  static defaultProps = {
    InvitedLeaguesList: [
      { name: 'Howdy', createdBy: '[email protected]', status: 'Join' },
      { name: 'Lorem', createdBy: '[email protected]', status: 'Join' }
    ]
  };

  renderLeague(item) {
    return <League invitedLeague={item} />;
  }

  render() {
    return (
      <View {...this.props}>
        <AddPlayers
          label={this.props.label}
          labelStyle={{ fontStyle: 'italic' }}
        />
        <FlatList
          numColumns={1}
          data={this.props.InvitedLeaguesList}
          renderItem={this.renderLeague}
        />
      </View>
    );
  }
}

Now I have to pass onPress (Function Prop) to League Component

I tried like this

 <FlatList
          numColumns={1}
          data={this.props.InvitedLeaguesList}
          renderItem={this.renderLeague}
          extraData={this.props}
        />

renderLeague(item) {
    return <League invitedLeague={item} onPress={this.props.onPress} />;
  }
like image 857
m9m9m Avatar asked Apr 24 '19 06:04

m9m9m


People also ask

How do you get the index of an element in FlatList React Native?

To get the index of the currently visible item in a React Native flat list, we can set the onViewableItemsChange prop to a function that gets the current visible items. to define the onViewableItemsChanged function that gets the viewableItems property. viewableItems has the items that are current visible on the screen.

How can FlatList be used in class components?

The FlatList component takes two required props: data and renderItem. The data is the source of elements for the list, and renderItem takes one item from the source and returns a formatted component to render. To implement the FlatList component, we need to import FlatList from 'react-native' library.


1 Answers

you can pass props as a parameter to the function that render items of flat list as below:

<FlatList
     numColumns={1}
     data={this.props.InvitedLeaguesList}
     renderItem={(item) => this.renderLeague(item, this.props)}
/>

and you can use this parameter in the renderLeague function:

renderLeague({item}, props) {
   ...
}

that props variable include all parameter of props as you can use this.props in another place.

like image 116
behnam shateri Avatar answered Sep 19 '22 04:09

behnam shateri