简体   繁体   English

如何使用平面列表在 React Native 中渲染组件

[英]How can you use a flatlist to render components in react native

I am currently creating an onboarding screen which is just a few components navigating linear to the next component on a button click.我目前正在创建一个入职屏幕,它只是几个组件,通过单击按钮线性导航到下一个组件。 However I've seen I could use a flatlist to style this better.但是我已经看到我可以使用平面列表来更好地设置样式。 Is it possible to pass components to a flatlist as the data prop?是否可以将组件作为数据道具传递给平面列表?

     import React from 'react';
    import { SafeAreaView, View, FlatList, StyleSheet, Text, StatusBar } from 'react-native';
   import DateOfBirth, Name, ProfilePicture from ‘./components’
    
    const DATA = [
      DateOfBirth, Name, ProfilePicture
    ];
   

 return (
        <SafeAreaView style={styles.container}>
          <FlatList
            data={DATA}
          />
        </SafeAreaView>
      );
    }

Would this be possible for example?例如,这可能吗? I know there is a renderedItems prop which seems to do something similar but seems to match the data to the item which isn't what I really want.我知道有一个 renderItems 道具似乎做了类似的事情,但似乎将数据与我真正想要的项目相匹配。 Is there anyway better?还有更好的吗? Or if there is any better libraries for onboarding options in react native that would be great.或者,如果有任何更好的库可用于 React Native 中的入职选项,那就太好了。 Most of what I see is similar to a flatlist and adds just an array with text rather than inputs etc我看到的大部分内容都类似于平面列表,并且只添加了一个带有文本而不是输入等的数组

You also need to set the renderItem prop.您还需要设置renderItem道具。 Style the <Item /> component to meet your requirement.设置<Item />组件的样式以满足您的要求。

const App = () => {
  const renderItem = ({ item }) => (
    <Item title={item.title} />
  );

  return (
    <SafeAreaView style={styles.container}>
      <FlatList
        data={DATA}
        renderItem={renderItem}
        keyExtractor={item => item.id}
      />
    </SafeAreaView>
  );
};

Don't understand why you're trying to do with your components.不明白您为什么要尝试使用您的组件。

To use Flatlist, you need to pass data and THEN use your components.要使用 Flatlist,您需要传递数据,然后使用您的组件。

Learn more here https://reactnative.dev/docs/flatlist在此处了解更多信息https://reactnative.dev/docs/flatlist

Example例子

import React from 'react';
import { SafeAreaView, View, FlatList, StyleSheet, Text, StatusBar } from 'react-native';
import DateOfBirth, Name, ProfilePicture from ‘./components’
    
const DATA = [
    {
        name: "Jhon Doe",
        dateOfBirth: "01/01/1980",
        profilePicture: "https://url.com/profile.jpg"
    },
    {
        name: "Jane Doe",
        dateOfBirth: "02/01/1980",
        profilePicture: "https://url.com/profile2.jpg"
    }
];

const App = () => {
    function renderClient ({client}){
        return(
            <View key={client.index}>
                <Name name={client.item.name} />
            </View>
        )
    }

    return (
        <SafeAreaView style={styles.container}>
            <FlatList
                data={DATA}
                keyExtractor={(item, index) => index.toString()}
                renderItem={renderClient}
            />
        </SafeAreaView>
        );
    }
}

 import React from 'react'; import {View,Text,StyleSheet,FlatList} from 'react-native'; const Demo = () => { let ary = [ { id:1, name:'jahnavi', }, { id:2, name:'yash', }, { id:3, name:'aniket', }], return ( <SafeAreaView style={styles.container}> <FlatList keyExtractor={item => item.id} data={ary} renderItem={({item}) => (<Item title={item.name}/> ); } /> </SafeAreaView> ); }; export default Demo;

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

相关问题 如何在 React Native 的 Flatlist 中传递数组以渲染 - How to pass array to render in Flatlist in React Native 如何在 react-Native 中的平面列表中使用 extraData - How to use extraData in a flatlist in react-Native 如何在本机反应中呈现 FlatList 中的数据,我可以控制台记录它但在屏幕上显示它时出现问题 - How to render data in FlatList in react native, I can Console log it but have a problem to display it on Screen React Native - 使用带有 FlatList 的 keyExtractor - React Native - Use a keyExtractor with FlatList 在本机反应中呈现带有 object 数组的项目平面列表 - render item flatlist with array of object in react native 我们如何根据索引或通过 react-native flatlist 中的键检查/取消选中 flatlist 中的单个复选框 - How can we Check/Uncheck individual checkbox in flatlist based on index or by key in react-native flatlist React-native flatlist 在 isLoading 上的组件之间共享道具 - React-native flatlist is sharing props between components on isLoading react native flatlist hooks - 使用 object 数组渲染项目 - react native flatlist hooks - render item with array of object 如何在反应原生中划分FlatList和Text中的数组 - How to divide array in FlatList and Text in react native 如何将对象迭代到清单中-反应本机? - How to Iteration object into flatlist - react native?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM