📌  相关文章
📜  flatlist react native Horizontal - Javascript (1)

📅  最后修改于: 2023-12-03 15:30:47.269000             🧑  作者: Mango

FlatList React Native Horizontal

Introduction

FlatList is a component in React Native that helps in rendering a list of data. It's optimized for performance and has a variety of configuration options. One of its key features is the ability to render items horizontally, which is great for things like image carousels or horizontally-scrolling lists. In this article, we'll explore how to set up and configure a horizontal FlatList in React Native.

Prerequisites

To follow along with this tutorial, you'll need a working knowledge of React Native and JavaScript. Additionally, you'll need to have a development environment set up that includes React Native and its dependencies.

Getting Started

To get started, we'll create a new React Native project using the Expo CLI. If you don't already have Expo installed, you can install it with the following command:

npm install -g expo-cli

Once you have Expo installed, use the following command to create a new project:

expo init my-project

This will create a new project with a basic file structure. Next, navigate to the project directory and install the dependencies:

cd my-project
npm install
Creating the FlatList

Now that we have a basic project set up, we can create our FlatList component. First, we'll import the FlatList component from the 'react-native' library and create a new component called 'HorizontalList':

import { FlatList } from 'react-native';
import React from 'react';

const HorizontalList = () => {
  return (
    <FlatList
      horizontal={true}
      data={[ 
        { key: 'item1' },
        { key: 'item2' },
        { key: 'item3' },
        { key: 'item4' },
        { key: 'item5' }
      ]}
      renderItem={({ item }) => <Text>{item.key}</Text>}
    />
  );
};

export default HorizontalList;

In this code snippet, we're creating a new component called 'HorizontalList' that returns a FlatList component. We're setting the 'horizontal' prop to true so that the list items will be rendered horizontally. We're also passing in some sample data as an array of objects, along with a 'renderItem' prop that renders each item as a text component.

Adding Styles

Now that we've created the list, let's add some styles to make it look better. We'll create a new stylesheet and add some styles for the list container and the list items:

import { FlatList, StyleSheet, Text, View } from 'react-native';
import React from 'react';

const styles = StyleSheet.create({
  container: {
    flex: 1,
    paddingTop: 22
  },
  item: {
    padding: 10,
    fontSize: 18,
    height: 44
  }
});

const HorizontalList = () => {
  return (
    <View style={styles.container}>
      <FlatList
        horizontal={true}
        data={[ 
          { key: 'item1' },
          { key: 'item2' },
          { key: 'item3' },
          { key: 'item4' },
          { key: 'item5' }
        ]}
        renderItem={({ item }) => <Text style={styles.item}>{item.key}</Text>}
      />
    </View>
  );
};

export default HorizontalList;

In this code snippet, we've imported the 'StyleSheet' component from 'react-native' and created a new stylesheet called 'styles'. We've added styles for the container and each item in the list. We're also passing the 'styles.container' style to the 'View' component that surrounds the FlatList.

Conclusion

In this tutorial, we've explored how to create and style a horizontal FlatList in React Native. We've covered the basics of using the 'FlatList' component, passing in data, and rendering items. We've also added some styles to make the list look better. With these skills, you can create all sorts of horizontally-scrolling lists and carousels in your React Native apps!