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} />;
}
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With