使用状态值更新操作表标题

时间:2019-08-16 07:08:25

标签: react-native react-native-android react-native-flatlist react-native-navigation

1)我有一个Flatlist,在renderItem方法中有Actionsheet,代码如下:

renderItem = ({ item }) => (
<TouchableOpacity onLongPress={() => {this.longPressed(item)}} activeOpacity={0.5} onPress={() => this.actionOnRow(item)} underlayColor="white">
        <ListItemViewCell data={item} />
      </TouchableOpacity>
<ActionSheet
        ref={o => this.ActionSheet = o}
        title={this.getTitle()}
        options={['Generate', 'Delete', 'Cancel']}
        cancelButtonIndex={2}
        destructiveButtonIndex={2}
        onPress={(index) => {
            if (index == 0) {
              this.generate(this.state.selectedListing)
            }
            if (index == 1) {
              this.delete(this.state.selectedListing)
            }
          }}
      />
)
getTitle() {
    return this.state.selectedProfileName
  }

“ getTitle”方法从Flatlist中选定的单元格返回配置文件名称。 但是对我来说,在动作表中标题没有显示。 我还已经在构造函数中绑定了“ getTitle”方法。

将调用“ getTitle()”方法,直到数量众多的列表项为止。 有人可以建议我如何进一步在ActionSheet中显示标题吗? 更新

3)我可以访问“

已更新: 在渲染方法中

  <FlatList
  data={this.state.data}
  renderItem={this.renderItem}
  keyExtractor={item => item.id}
  ItemSeparatorComponent={this.flatListItemSeparator}
  ListFooterComponent={this.renderFooter}
  // onRefresh={this.handleRefresh}
  refreshing={this.state.refreshing}
  onEndReached={this.handleLoadMore}
  onEndReachedThreshold={0}
  onRefresh={this.refreshProfiles}
/>

从平面列表生成列表之后,当我长按平面列表项目时,我想在Actionsheet上显示两个我使用过“ [https://github.com/beefe/react-native-actionsheet][1]”的选项。

在Cell longpress方法中,我正在更新状态值,并且试图触发操作表,如下所示

longPressed(item) {
    console.log("Long pressed " + JSON.stringify(item)
    this.setState({
      selectedProfileName: item.name
    },() => {
      console.log("selected Listing "+ this.state.selectedProfileName)
      this.ActionSheet.show()
    })
  }

在setstate完成中,我正在调用操作表的show()方法。还要更新状态值后,配置文件名称不会显示在“动作表”标题中。

有人可以帮助我解决此问题吗?[1]:https://github.com/beefe/react-native-actionsheet

item的JSON响应是

{ "id": 985646, "name": "Business Name", "website": "businesswebsite.com", "formatted_website": "businesswebsite.com", "is_favorite": false }

2 个答案:

答案 0 :(得分:0)

您的代码没有约束力。如果具有绑定,则代码应如下所示。

title={this.getTitle.bind(this)}

OR

title={() => this.getTitle()}

如果使用状态值,则不必调用函数。

title={this.state.selectedProfileName}

答案 1 :(得分:0)

请检查以下代码 第1步:使用提醒检查商品数据

renderItem = (item) => (
alert(JSON.stringify(item))
<TouchableOpacity onLongPress={() => {this.longPressed(item)}} activeOpacity={0.5} onPress={() => this.actionOnRow(item)} underlayColor="white">
        <ListItemViewCell data={item} />
      </TouchableOpacity>
<ActionSheet
        ref={o => this.ActionSheet = o}
        title={this.getTitle()}
        options={['Generate', 'Delete', 'Cancel']}
        cancelButtonIndex={2}
        destructiveButtonIndex={2}
        onPress={(index) => {
            if (index == 0) {
              this.generate(this.state.selectedListing)
            }
            if (index == 1) {
              this.delete(this.state.selectedListing)
            }
          }}
      />
)
getTitle() {
    return this.state.selectedProfileName
  }

**