简体   繁体   English

如何将组件中的道具传递给FlatList renderItem

[英]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. 我的组件中有一个函数作为道具,并且必须将此函数道具传递给FlastList中renderItem中的另一个组件。 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: 'email1@gamil.com', status: 'Join' },
      { name: 'Lorem', createdBy: 'email@gmail.com', 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 现在我必须将onPress (功能道具)传递给League组件

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} />;
  }

This way is working for me 这样对我有用

<FlatList
     numColumns={1}
     data={this.props.InvitedLeaguesList}
     renderItem={({ item }) => <League invitedLeague={item} onPress={this.props.onPress} />}
     extraData={this.props}
 />

在此处输入图片说明

I think you try to make a callBack function, if so, please do the following. 我认为您尝试制作callBack函数,如果是这样,请执行以下操作。

renderLeague(item) {
    return <League invitedLeague={item} onPress={this._callBack.bind(this)} />;
}


//callback function
_callBack(data) {
   // your code here...
}

From your component League 来自您的League

call the function like the following, 像下面这样调用函数,

this.props.onPress(datas);

you can pass props as a parameter to the function that render items of flat list as below: 您可以将props作为参数传递给呈现平面列表项目的函数,如下所示:

<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函数中使用此参数:

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

that props variable include all parameter of props as you can use this.props in another place. 该props变量包含props的所有参数,因为您可以在其他地方使用this.props

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ReactNative-FlatList renderItem道具 - ReactNative - FlatList renderItem props 将额外的道具数据传递给 react-native-draggable-flatlist 中的 renderItem - pass extra props data to renderItem in react-native-draggable-flatlist React Native:如何正确地将 renderItem 项传递给 FlatList,以便它们可以在另一个组件中呈现? - React Native: how to properly pass renderItem items to FlatList, so they can be rendered in another component? 如何通过FlatList将键传递到其renderItem? - How do I pass a key through a FlatList into its renderItem? react-native:Flatlist如何从onViewableItemsChanged传递到renderItem - react-native: Flatlist how to pass from onViewableItemsChanged to renderItem React Native FlatList renderItem 所需的道具和道具的名称 - React Native FlatList renderItem required props and names of the props FlatList不会通过0或null传递给renderItem属性 - FlatList does not pass through 0 or null into renderItem prop 如何更改 FlatList 中 TextInput 组件的道具? - How to change props of TextInput component in a FlatList? 如何将道具保留在Flatlist的子组件中? - How to keep props in a child component inside a Flatlist? 如何将FlatList项目中的道具传递给Modal? - How to pass props from FlatList item to Modal?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM