[英]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.