简体   繁体   English

React Native Flatlist renderItem

[英]React Native Flatlist renderItem

Working with React Native, having some issues with the FlatList component.使用 React Native 时,FlatList 组件存在一些问题。 This is my FlatList这是我的 FlatList

    <FlatList
     data={this.state._data}
     renderItem={() => this.renderItem()}
     refreshControl={
       <RefreshControl
        onRefresh={() => this.handleRefresh}
        refreshing={this.state.refreshing}
       />
      }
    />

This is my renderItem function:这是我的 renderItem 函数:

    renderItem({item, index}) {
     return (
      <View style={{marginTop: 10, marginHorizontal: 10, paddingLeft: 
         10}}>
        <ListItem
            roundAvatar
            title={`${item.itemName}`}
            subtitle={`${item.amount}`}
            avatar={require('../../../images/logo.png')}
        />
        <View
            style={{
                paddingBottom: 10,
                paddingTop: 10,
                display: 'flex',
                flexDirection: "row",
                justifyContent: "space-around",
                alignContent: "center"
            }}
         >
            <View style={{ flexDirection: "row", alignContent: 
                 "center", width:"45%"}}>
                <Button
                    block
                    small
                    // disabled={this.state.acceptButtonGray}
                    style=
                      {this.state.acceptButtonGray ? ({
                      backgroundColor: 'gray',
                      width: "100%"
                      }) : ({backgroundColor: "#369ecd",
                         width: "100%"
                      })}
                    onPress={() =>
                      this.setState({
                         modalVisible: true,
                         // acceptOrDeclineModalText: `Accept offer for ${item.amount} ${'\b'} Are you Sure?`,
                         acceptOffer: true,
                          })
                      }
                      >
                    <Text>
                        Accept
                    </Text>
                </Button>
            </View>
        </View>
    </View>
   );
  }

this.setState in the onPress in the button should make a Modal visible, and set acceptOffer to true.按钮中 onPress 中的 this.setState 应该使 Modal 可见,并将 acceptOffer 设置为 true。 Modal opens, user confirms their offer.模态打开,用户确认他们的报价。 The offer button which opened that modal now should be gray, and even better, disabled.现在打开该模式的报价按钮应该是灰色的,甚至更好的是禁用。

Passing my RenderItem function as shown above, I receive如上所示传递我的 RenderItem 函数,我收到

    TypeError: Cannot read property 'item' of undefined.

Passing my RenderItem function like this:像这样传递我的 RenderItem 函数:

    renderItem={this.renderItem}

I Get This Error:我收到此错误:

    _this2.setState is not a function

The FlatList Component is certainly responsible for part of my issue, as well as how and where I am calling this.setState. FlatList 组件当然要对我的部分问题负责,以及我调用 this.setState 的方式和位置。 Only one button is shown in my post, but there are two, one for accept, one for decline.我的帖子里只显示了一个按钮,但是有两个,一个是接受,一个是拒绝。 Would having two modals change anything?有两个模态会改变什么吗?

The FlatList displays my ListItem components with ease until I attempt to call this.setState in the buttons inside the View which contains those ListItems. FlatList 可以轻松地显示我的 ListItem 组件,直到我尝试在包含这些 ListItem 的 View 内的按钮中调用 this.setState。

The Modal close button takes this.state.acceptOffer and if true, sets this.state.acceptButtonGray to true, should this logic be somewhere else?模态关闭按钮接受 this.state.acceptOffer,如果为真,则将 this.state.acceptButtonGray 设置为真,这个逻辑应该在其他地方吗?

Is there another way to open a modal and change the button color without using component state?是否有另一种方法可以在不使用组件状态的情况下打开模态并更改按钮颜色? Does react want these buttons inside of a TouchableOpacity? react 是否需要 TouchableOpacity 中的这些按钮?

I greatly appreciate any help given.我非常感谢您提供的任何帮助。

you should write a renderItem function like this你应该写一个这样的renderItem函数

renderItem = ({item, index}) => {
 // return here
}

将您的 renderItem 方法更改为renderItem={this.renderItem.bind(this)}

1) You can write function as - 1)您可以将函数编写为-

renderItem = ({item, index}) => { // return here }

2) or else if you want to execute your function then - 2) 否则,如果您想执行您的功能,则 -

<FlatList
 data={this.state._data}
 renderItem={(item) => this.renderItem.bind(this, item)}
 refreshControl={
   <RefreshControl
    onRefresh={() => this.handleRefresh}
    refreshing={this.state.refreshing}
   />
  }
/>

您必须使用bind(this,item)或更改函数,如(item)=>

I experienced the same issue and wasted many hours to figure out why it was not re-rendering:我遇到了同样的问题并浪费了很多时间来弄清楚为什么它没有重新渲染:

We need to set extraData prop of FlatList if there is any change in the state like so:如果状态有任何变化,我们需要设置extraDataFlatList ,如下所示:

<FlatList data={this.state.data} extraData={this.state} .../>

Please see the official documentation here:请在此处查看官方文档:

https://facebook.github.io/react-native/docs/flatlist.html https://facebook.github.io/react-native/docs/flatlist.html

As per my Knowledge item and index are passed as object in flatlist's renderItem根据我的知识项和索引作为对象在 flatlist 的 renderItem 中传递

so we can pass by two ways所以我们可以通过两种方式

1 way 1路

Flatlist Component平面列表组件

<FlatList
     data={this.state.data}
     keyExtractor={(item, index) => index.toString()}
     renderItem={({ item, index }) => this._renderItem(item, index)} //Passing as object from here.
/>

Render Item渲染项目

_renderItem = (item, index) => {
      console.log(item)
      console.log(index)
}

2 way 2种方法

Flatlist Component平面列表组件

<FlatList
     data={this.state.data}
     keyExtractor={(item, index) => index.toString()}
     renderItem={( item, index ) => this._renderItem(item, index)}
/>

Render Item渲染项目

_renderItem = ({item, index}) => { // Passing as object from here
      console.log(item)
      console.log(index)
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM