How to create a FlatList - javascript

I want to create a FlatList, Grid List or a View off numColum={3} for my
{folderToDisplay.map((item) => {
return <Text key={item.id}>{item.value}</Text>;
})}
right now my text is a list row. How to make it a colum off 3?

Use FlatList with numColumns props.
<FlatList
data={folderToDisplay}
renderItem={({ item }) => <Text key={item.id}>{item.value}</Text>}
keyExtractor={item => item.id}
numColumns={3}
/>
https://reactnative.dev/docs/flatlist

numColumns
Multiple columns can only be rendered with horizontal={false}.
FlatList in React-Native
<FlatList
contentContainerStyle={{margin:4}}
horizontal={false}
numColumns={4}
data={this.state.categoryDataSource}
renderItem={(item) =>
return <Text key={item.id}>{item.value}</Text>;
}
keyExtractor={category => category.id}
/>

Related

How To Know Last Item of FlatList

I have a FlatList.
I want to add comma between items.
Currently working like this;
If there is only one item;
Mon,
If there are multiple items;
Mon, Tue, Wed,
My code;
<FlatList
data={job.schedule}
renderItem={({ item, index }) => (
<View style={[[Layout.center], {}]}>
<Text style={[[Fonts.textInterRegular12]]}>
{item.dayHuman.slice(0, 3)}{', '}
</Text>
</View>
)}
horizontal
keyExtractor={item => item.id}
extraData={this.state}
showsVerticalScrollIndicator={false}
/>
Above code has two issues.
If there is only one item, I don't want to add a comma.
E.g. Mon
If there are multiple items, I don't want to add a comma next to the last item.
E.g. Mon, Tue, Wed
How can I achieve this?
When working with ReactNative FlatList, you can use FlatList's ItemSeparatorComponent prop to render a component between each item, but not at the start or end of the list.
You can use FlatList's ListFooterComponent & ListHeaderComponent props to render some JSX or a component at the end or start of the list.
See diagram below.
If the above doesn't help and you just need to know that you are at the end of the list within a renderItem, you can check the index in the metadata provided to renderItem against the length of data.
const data = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday'];
export default function App() {
return (
<SafeAreaView>
<FlatList
data={data}
renderItem={({ item, index }) => {
const isEnd = index === data.length - 1;
return (
<View>
<Text>
{item}{isEnd && <Text>. </Text>}
</Text>
</View>
);
}}
horizontal
keyExtractor={(item) => item.id}
extraData={this.state}
showsVerticalScrollIndicator={false}
ItemSeparatorComponent={() => <Text>, </Text>}
ListFooterComponent={() => <Text>The End!</Text>}
/>
</SafeAreaView>
);
}
Snack
<FlatList
data={job.schedule}
renderItem={({ item, index }) => (
<View style={[[Layout.center], {}]}>
<Text style={[[Fonts.textInterRegular12]]}>
{item.dayHuman.slice(0, 3)}
{index !== job.schedule.length -1 && ', '}
</Text>
</View>
)}
horizontal
keyExtractor={item => item.id}
extraData={this.state}
showsVerticalScrollIndicator={false}
/>
I think you are looking for this
<FlatList
data={job.schedule}
renderItem={({ item, index }) => (
<View style={[[Layout.center], {}]}>
<Text style={[[Fonts.textInterRegular12]]}>
{item.dayHuman.slice(0, 3)}{index < job.schedule.length -1 ?', ':""}
</Text>
</View>
)}
horizontal
keyExtractor={item => item.id}
extraData={this.state}
showsVerticalScrollIndicator={false}
/>

Displaying a Flatlist Data to Flatlist Props

im having an issue with passing my Flatlist data to my Flatlist props which is 'ListHeaderComponent'. here's my code:
<FlatList
ListHeaderComponent={
<View>
<Text>{}</Text>
</View>
}
data={displayedPhoto}
keyExtractor={(item) => item.id}
renderItem={renderPhotoItem}
numColumns={2}
/>
is there any possible to pass my Flatlist Data inside the Text Component? Help me please
ListHeaderComponent is a prop which use to render the component as Header of the FlatList which scrolls with the FlatList. ListHeaderComponent can't have the item of the FlatList. You can use like
renderHeader = () => {
return (
<View>
<Text>Employees</Text>
</View>
);
};
<FlatList
ListHeaderComponent={this.renderHeader}
data={displayedPhoto}
keyExtractor={(item) => item.id}
renderItem={renderPhotoItem}
numColumns={2}
/>

React Native: renderItem component to Custom FlatList as a prop? (like Vue slots)

I want to have a reusable FlatList component, basically a wrapper I can pass a custom component as an item to render, and I want to pass this component as props, this is easily done in vue..js with scoped/slots.
Is there a way to do this in react native?
<View style={{ flex:1 }}>
<FlatList style={{ width:'100%'}} data={props.data} keyExtractor={item => item.id.toString()}
renderItem={({ item, index }) =>
<PropComponent item={item}></PropComponent>}
/>
</View>
In example above PropComponent would be passed to CustomFlatList like this:
<CustomFlatList>
<PropComponent></PropComponent>
</CustomFlatList>
```
Use this way
// CustomFlatList.js
const { ...rest } = props;
<View style={{ flex:1 }}>
<FlatList
style={{ width:'100%'}}
data={props.data}
keyExtractor={item => item.id.toString()}
{...rest}
/>
</View>
Usage like
itemView = ({ item, index }) => {
return <PropComponent item={item} />
}
<CustomFlatList renderItem={itemView} />

Beginners Problem with ReactNative FlatList

I'm pretty new to react native and have an issue that my data is not showing up in my view.
I can retrieve it from my server and save it in the data array.
When I want to go through it and create a flatlist it doesn't show up and I don't know why.
The console.log output shows that I have data in my array and also displayList is true at that point in time. Any help why this happens ?
return (
<View style={styles.container}>
<Button title="Retrieve Data" onPress={fetchData} />
<Text>before</Text>
{console.log(
`Size of Data: ${data.length} and displayList: ${displayList}`
) &&
displayList &&
data &&
data.map((item, i) => (
<FlatList
data={data}
renderItem={({ item }) => <Text>{item["name"].Value[0]}</Text>}
keyExtractor={(item) => item}
/>
))}
<Text>after</Text>
<StatusBar style="auto" />
</View>
);
}
By mapping over the flat list you are creating a new list for each item of data which I doubt it what you want to do.
The internals of render item have the map function in for you.
I think what you're looking for is...
return (
<View style={styles.container}>
<Button title="Retrieve Data" onPress={fetchData} />
<Text>before</Text>
<FlatList
data={data}
renderItem={({ item }) => <Text>{item["name"].Value[0]}</Text>}
keyExtractor={(item) => item.id.toString()}
/>
))}
<Text>after</Text>
<StatusBar style="auto" />
</View>
);
If you want to render something else if theres no data then you can use the ListEmptyComponent prop on the flat list.
Im also not sure what your data array looks like. But this may also be an issue. If this doesn't work please update your question with the full component and data structure.
try this, Don't have to iterate through map, the item is already containing the object for respective index.
return (
<View style={styles.container}>
<Button title="Retrieve Data" onPress={fetchData} />
<Text>before</Text>
{data.lenght()>0? <FlatList
data={data}
renderItem={({ item }) => <Text>{item["name"].Value[0]}</Text>}
keyExtractor={(item) => item}
/>:<Text>No DATA FOUND<Text>}
<Text>after</Text>
<StatusBar style="auto" />
</View>
);

How do I alternate colors in Flat List (React Native)

Trying to alternate colors in React Natives Flatlist. I believe I need rowID or something similar to do it. This is what I've got so far:
let colors = ['#123456', '#654321', '#fdecba', '#abcdef'];
<View >
<FlatList style={{backgroundColor: colors[this.index % colors.length]}}
data={this.state.dataSource}
renderItem={({item}) => <Text style={styles.listStyle}>{item.title}, {item.releaseYear}</Text>}
keyExtractor={(item, index) => index}
/>
</View>
Any ideas?
The renderItem callback argument has a property index that allows you to access the row index for the current row:
<View >
<FlatList
data={this.state.dataSource}
keyExtractor={(item, index) => index}
renderItem={({item, index}) => (
<View style={{ backgroundColor: colors[index % colors.length] }}>
<Text style={styles.listStyle}>{item.title}, {item.releaseYear}</Text>
</View>
)}
/>
</View>

Categories

Resources