FlatList 使用静态组件滚动

FlatList scroll with the static component

我在一个屏幕中有一个静态组件和许多 for 循环渲染组件。

所有代码都在下面。

import React from "react";
import { View, Text, FlatList } from "react-native";
...

class FlatListScrollWithStatic extends React.Component {
  render() {
    return (
      <View style={{ flex: 1 }}>
        <FriendsTop />  // the static component
        <FlatList
          style={{ flex: 1 }}
          data={this.props.friendsCard}
          renderItem={({ item }) => <FriendsCard {...item} />}  // the for loop rendered components
          keyExtractor={(item, index) => index.toString()}
        />
      </View>
    );
  }
}

现在我想将FriendsTop迁移到Flatlist,让它随着渲染的组件滚动,我应该如何更改我的代码?

这可能对你有用:

import React, { Fragment } from "react";

...

<FlatList
  style={{ flex: 1 }}
  data={this.props.friendsCard}
  renderItem={({ item }, index) => (
    <Fragment>
      { !index && <FriendsTop /> } // Render when !index is truthy (i.e. index === 0)
      <FriendsCard {...item} />
    </Fragment>
  )}
  keyExtractor={(item, index) => index.toString()}
/>

Flatlist 有一个接受 JSX 元素的属性 ListHeaderComponent。 所以:

import React from "react";
import { View, Text, FlatList } from "react-native";
...

class FlatListScrollWithStatic extends React.Component {
  render() {
    return (
      <View style={{ flex: 1 }}>

        <FlatList
          style={{ flex: 1 }}
          data={this.props.friendsCard}
          renderItem={({ item }) => <FriendsCard {...item} />}  // the for loop rendered components
          keyExtractor={(item, index) => index.toString()}
          ListHeaderComponent={<FriendsTop />}
        />
      </View>
    );
  }
}

我有两种方法可以首先使用 flatlist

的 header 组件

但我想如果你想要静态数据和你从 fetch 中获得的数据,你应该将它们结合起来

 <FlatList
   inverted
      data={this.state.data.concat({
        "id":1,
        "user_id": 1,
        "title": "all",
        "similar_title": null,
        "description": null,
        "font_icon": null,
        "default_distance": 8,
        "visible": "yes",
        "priority": "no",
        "image": null,
        "created_at": null,
        "updated_at": "2018-06-14 22:13:58"
    })}

      numColumns={4}


      renderItem={({item}) =>

在该代码中,我添加了带有 concat 的静态所有按钮