【问题标题】:react native - onPress get Flatlist detailed informationreact native - onPress 获取 Flatlist 详细信息
【发布时间】:2018-11-01 06:19:44
【问题描述】:

我正在使用来自 react native elements 的 react native Flatlist。 我想要做的是当我点击每一行时,我想导航到一个新屏幕并获取详细信息。

例如,如果我点击AmyFarha,我想在新屏幕中访问她的emailphoneNumber

下面是代码。我添加了电子邮件和电话号码字段。

import { List, ListItem } from 'react-native-elements'

const list = [
  {
    name: 'Amy Farha',
    avatar_url: 'https://s3.amazonaws.com/uifaces/faces/twitter/ladylexy/128.jpg',
    subtitle: 'Vice President',
    email: 'amy@amy.com',
    phoneNumber: '1-808-9999'
  },
  {
    name: 'Chris Jackson',
    avatar_url: 'https://s3.amazonaws.com/uifaces/faces/twitter/adhamdannaway/128.jpg',
    subtitle: 'Vice Chairman',
    email: 'chris@chris.com',
    phoneNumber: '1-808-8888'
  },
  ... // more items
]

<List containerStyle={{marginBottom: 20}}>
  {
    list.map((l) => (
      <ListItem
        roundAvatar
        avatar={{uri:l.avatar_url}}
        key={l.name}
        title={l.name}
      />
    ))
  }
</List>

【问题讨论】:

  • 你为什么不用touchablehighlight
  • 您使用的是什么导航?
  • @Nima createDrawerNavigator 和 createStackNavigator 有关系吗?

标签: javascript reactjs react-native react-native-flatlist


【解决方案1】:

在组件列表项中使用展开来显示用户详细信息,如下所示:

handleExpand = () => {
    this.setState({expand: !this.state.expand})
}

<TouchableOpacity onPress={this.handleExpand}>
   {this.state.expand ? (<ViewExpand/>) : (<ViewNormal/>)}
</TouchableOpacity>

【讨论】:

  • 哼.. 我不明白。如果你能详细说明一下就好了,谢谢:)
  • handleExpand 是一个每次单击行项目时都会调用的函数因此,在函数内部设置了状态,它将恢复它当前具有的任何扩展值。也就是说,如果它具有真值,则将其设置为假,反之亦然。因此,每次展开状态时,都会渲染 ViewExpand 组件。 TouchableOpacity 内部的代码是三元运算符,如果 this.state.expand 为 true 则运行 ViewExpand,如果 this.state.expand 为 false 则运行 null!
【解决方案2】:
handleTouchItem = (item) => {
    this.props.navigation.navigate("forward page do you want", item);
}
 <ListItem
        onPress={() => this.handleTouchItem(item)}
        roundAvatar
        avatar={{uri:l.avatar_url}}
        key={l.name}
        title={l.name}
      />

【讨论】:

  • 你能添加一个简短的描述吗?
  • handleTouchItem 是函数,你知道 es6 规则主要是在 react native 中使用箭头函数,而 ListItem 组件,你只需使用属性 @987654324 调用该函数@ 带有参数item,参数item 来自您的list.map((l) =&gt; (,只需将i 更改为item 对我来说有意义。
【解决方案3】:

你应该使用这样的东西:

 <View>
              <FlatList
                  data={list}
                  keyExtractor={this._keyExtractor}
                  renderItem={({ item }) => (
                      <CustomItemComponent item={item} onPress={this.props.onItemPress}/>
                  )}
                  numColumns={2}
                  />
</View>

您可以创建一个新组件CustomItemComponent,并将 item 作为道具。在这个新组件中,您可以使用nametitle 或任何其他属性。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-07
  • 2018-03-23
  • 2017-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多