React Native FlatList items not rendering - reactjs

I'm testing a FlatList nested in a Navigator and trying to learn how it works. The code below works fine:
<FlatList
data={[{key: 'a'}, {key: 'b'}]}
renderItem={({item}) => <Text>{item.key}</Text>}
horizontal={true}
/>
But this does not:
<FlatList
data={[{key: 'a'}, {key: 'b'}]}
renderItem={({item}) => <TextComp data={item}/>}
horizontal={true}
/>
TextComp is just a component that displays item.key and it works as intended when tested separately. The code is
<View>
<Text>{this.props.data.key}</Text>
</View>
I've also tried drawing borders around both components and it seems that the FlatList is definitely rendering, but the items are not.
I am testing on my Android device.
Update: I added console.log(this.props) statement to the TextComp component and it displays the props correctly, so the the correct data is being passed from the FlatList to TextComp, but TextComp is just not getting rendered.

Marcel Kalveram got the solution here after I compared his code with mine. Turns out, a height and width are required for the item to render correctly in the FlatList. The explanation I figured for this is that the item's dimensions are required to properly size the item within the FlatList. Similarly, an item's dimensions cannot be percentages since its parent is a FlatList, which has varying dimensions itself. Therefore, the solution to my problem was to add width and height style attributes to the item component. To make it automatically resize, I used built-in React Native Dimensions.

Related

How can user change the order of flatlist components by drag and drop

I am using flatlist to show the results of different games, I want my users to change the order as they wish for that I used draggable flatlist function, the problem is i can drag the game but it couldn't place where i want it always comes back to its original position as in API.
Here is the code for flatlist function
<DraggableFlatList
data={this.state.dataSource}
onDragEnd={this.dataSource}
renderItem={this.renderItem}
keyExtractor={item => item.GameId.toString()}
ItemSeparatorComponent={this.renderSeprator}
refreshing = {this.state.refreshing}
onRefresh = {this.handleRefresh}
refreshControl={
<RefreshControl refreshing={this.refreshing} onRefresh={this.getS}
/>
}
/>

How To Get Springy/Bouncy Effect When Scrolling In Flatlist

How can I make a vertical Flatlist item in React Native to snap into position on scroll and fill up the current window? I want each item to fill up the screen and not for the previous or coming items to show. An example of the effect I want is the achieved in ScrollView by having pagingEnabled={true}. But my problem with this, when applied to Flatlist, is that parts of the previous item shows at the top of subsequent views. This increases in size as you scroll down. The effect I want is like if you are scrolling through TikTok. You see how each video fills up the screen and when you scroll up or down, there is a springy effect that if the scroll is not complete, the current video bounces back into position.
This is my current flatlist:
<FlatList
data={DATA}
renderItem={renderItems}
keyExtractor={item => item.id}
Animated={true}
removeClippedSubviews={true}
refreshControl={
< RefreshControl
refreshing={refreshing}
onRefresh={onRefresh}
/>}
Please note that I am using Functional components not classes.
Thanks in advance.
Wow! After hours on this, GeekForGeeks has delivered me!
This is the article and all thanks to Brijen Makwana.
The answer is in applying three props already provided by Flatlist to your/my flatlist. They are:
snapToAlignment='start'
decelerationRate={'fast'}
snapToInterval={windowHeight}
So my Flatlist will now look like this:
<FlatList
data={DATA}
renderItem={renderItems}
keyExtractor={item => item.id}
snapToAlignment='start'
decelerationRate={'fast'}
snapToInterval={windowHeight}
/>
Remember to first Import Dimensions from React native and make the Item View have the full height and full width of the window (that is the screen):
import { StyleSheet, View, FlatList, Dimensions } from "react-native";
const windowHeight = Dimensions.get('window').height;
const windowWidth = Dimensions.get('window').width;
In your stylesheet:
StyleOfItemsContainerForFlatList: {
height: windowHeight,
width: windowWidth,
justifyContent: "center",
alignItems: "center"
},
I removed the Refreshcontrol here since it is not relevant to this answer.
And just like that my React Native Vertical Flatlist now has a bouncy, springy transition animation like Tiktok and Instagram!
And also, the article shows how you can apply snap animation on Horizontal Flatlists also.
Please check the original article for details.

Add custom component after searchbar in #react-navigation/native-stack

I'm having the following design
Looking for a smart way to accomplish my mission using #react-navigation/native-stack
Requirements are the following:
Filters should be present after native search bar.
Screen title should automatically shrink/expand upon scroll. As it's done by default.
The problem is I don't see any proper place to put filters component to.
If I put it to content title animation doesn't work. It's always small because content's outer component should be a ScrollView or its ancestor like FlatList or SectionList. Nested ScrollView of the same direction are not supported too.
<View>
<FiltersComponent />
<SectionList ... />
</View>
If I put it like that title animation works, but filters go out of bounds on scroll.
<SectionList ListHeaderComponent={FiltersComponent} ... />
I tried even following, but title looks buggy, its both presentations (small and large) visible in the same time.
// set headerLargeTitle: true initially
<SectionList
onScroll={(e) => {
navigation.setOptions({ headerLargeTitle: e.nativeEvent.contentOffset.y === 0 });
}}
/>
Can anyone suggest something?

How can I set the Overlay container property when using OverlayTrigger?

I'm using the OverlayTrigger component to get hover behavior for tooltips, and therefore not using the Overlay component directly. The Overlay component has a container property that I'd like to use to remedy the tooltip getting cut off by its natural container element.
I've attempted to pass a popperConfig object, but that's not working. I've also tried adding a container attribute to the OverlayTrigger component.
<Container fluid className='flex-fill' ref={rowContainer}>
...
<OverlayTrigger delay={{show: 500}} overlay={renderUserTooltip}
popperConfig={{container: rowContainer}}>
<FaUser/>
</OverlayTrigger>
How can I set the container for the Overlay when the Overlay component isn't directly used?
React bootstrap doesn't have a container prop or something similar (I mean it has a target prop but as this part of the docs suggests, for the OverlayTrigger the type is null, so it doesn't accept values and I don't think you can trick it to accept (and I don't think it would be wise to try).
But a pretty nice example, that shows some sort of a workaround in my opinion, can also be find in the docs, under customizing-trigger-behavior.
Starting from that example, if you need your trigger to be totally separated from the container an option is to just wrap everything in a big container that receives ({ ref, ...triggerHandler }) and all is left is to give your container the ref, and the trigger to your FaUser component. So something like:
<OverlayTrigger
placement="bottom"
overlay={<Tooltip id="button-tooltip-2">Check out this avatar</Tooltip>}
>
{({ containerRef, ...triggerHandler }) => (
<Container fluid className='flex-fill' ref={containerRef}>
...
<FaUser/>
</Container>
)}
</OverlayTrigger>
I also created a sandbox with a minimal reproducible example.

One of two FlatLists not rendering items in same component

I have two FlatLists in my react native component. They are filled with similar data and about 80 items for the second FlatList. When the first FlatList reaches 11 items or more (I'm adding dynamically items to the first FlatList), the second one stops rendering items, and it is empty even if the choice for the filter is correct.
This is the first FlatList:
<FlatList
style={styles.flatList}
data={todaySuggestions}
renderItem={this.renderSuggestionItem}
keyExtractor={this.keyExtractor}
extraData={this.props}
/>
And the second one is:
<FlatList
style={styles.flatList}
data={data.filter(item => !completedItems.find(item1 => item1.id === item.id).completed)}
renderItem={this.renderFurtherSuggestionItem}
keyExtractor={this.keyExtractor}
extraData={this.props}
/>
when state change, Flatlist don't rerender again. if you want to rerender it again should use :
<FlatList
data:{yourData}
extraData:{this.state.yourState}
renderItem={this.yourRenderItem}
/>
instead of yourState you should use the state that when it changed, you need to rerender your flatlist.
Solved this one by adding another condition for filter, removing it from renderFurtherSuggestionItem function. In the other case, FlatList won't notice change of the data.

Resources