Fixes(SectionList) component preventing element unmounting, when sepe…#42435
Closed
Biki-das wants to merge 15 commits into
Closed
Fixes(SectionList) component preventing element unmounting, when sepe…#42435Biki-das wants to merge 15 commits into
Biki-das wants to merge 15 commits into
Conversation
Base commit: fe7b9e5 |
javache
reviewed
Jan 22, 2024
Contributor
Author
|
@javache 😅 you were correct , it actually does not need a |
…rator is Added or removed
Contributor
Author
|
@huntie could you possibly check this out, has been on radio silence for a while now |
Member
|
Don't think I'm the best owner for this — cc @NickGerleman perhaps? |
Contributor
Author
|
@javache friendly ping 😃 |
Contributor
Author
|
closing as reopen on #43223 |
facebook-github-bot
pushed a commit
that referenced
this pull request
Feb 29, 2024
) Summary: Reopen in reference to #42435 , as it had a lot of commits and fixes making it messed up. Weirdly when using a [Section List](https://reactnative.dev/docs/sectionlist) specifically when using the [SectionSeperatorComponent](https://reactnative.dev/docs/sectionlist#sectionseparatorcomponent), and adding additional Data on the renderItem List, there is a unmount occurring as the new Data is added, this only happens while using the `SectionSeparatorComponent` and limited to the same **Here is the Current Behaviour** https://github.com/facebook/react-native/assets/72331432/0622e789-ddbc-4038-8ccc-a421050eecbf **Here is the Expected Behaviour After Fixes** https://github.com/facebook/react-native/assets/72331432/2f0cc3a9-da97-43f5-8a4b-3faca74ae834 The issues is happening because the Parent View gets removed, as we add Data, the separator moves causing the unmount, the solution that i proposed here is to conditionally render to make sure that our separator renders correctly which required to add an empty view as the `itemSeparatorComponent`. ## Changelog: [General] [Added] - Fixes SectionList Unmounting issue with separatorComponent on data addition and removal. Pull Request resolved: #43223 Test Plan: i used the `rn-tester` App to test out the changes below is the code to test the same. Update the snapshots to align with the changes. ``` import React, {useEffect, useState} from 'react'; import { StyleSheet, Text, View, SafeAreaView, StatusBar, SectionList, Image, } from 'react-native'; interface Item { id: string; title: string; imgSrc: string; } interface Section { title: string; data: Item[]; } const keyExtractor = (item: Item, index: number) => `${item.title}-${index}`; const renderItem = ({item, index}: {item: Item; index: number}) => { return <ItemComponent item={item} index={index} />; }; const additionalFruits = [ { id: '7', title: 'Sandwich', imgSrc: 'https://img.freepik.com/premium-vector/sanwich-vector-isolated-fast-food_484148-2.jpg', }, { id: '8', title: 'Burger', imgSrc: 'https://static.vecteezy.com/system/resources/thumbnails/033/494/666/original/animated-illustration-of-burger-cartoon-for-foods-menu-animation-free-video.jpg', }, { id: '9', title: 'Juice', imgSrc: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcQHg9yLXE7Jlq4OM9ey64TW4D9qUFaeGU76rg&usqp=CAU', }, { id: '10', title: 'Milkshake', imgSrc: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcROpBez3eZKvEVdxBce9XyG6j6caDFMjV_xUQ&usqp=CAU', }, ]; const DATA: Section[] = [ { title: 'Items', data: [ { id: '1', title: 'Cake', imgSrc: 'https://static.vecteezy.com/system/resources/previews/012/132/227/original/cute-cake-cartoon-icon-illustration-food-recreation-icon-concept-isolated-premium-flat-cartoon-style-vector.jpg', }, { id: '2', title: 'Pizza', imgSrc: 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcR2kYjlsayzKZx4hFo6fbH7S8pde2JRo2a0hfaeoz3O7bjqdJjhI0tFiSROM8G4UbdbwXU&usqp=CAU', }, { id: '3', title: 'Coke', imgSrc: 'https://png.pngtree.com/png-clipart/20220116/original/pngtree-cartoon-hand-painted-coke-bottle-png-image_7106844.png', }, { id: '4', title: 'Noodles', imgSrc: 'https://thumbs.dreamstime.com/z/cute-cartoon-vector-bowl-noodle-isolated-white-background-115120959.jpg', }, { id: '5', title: 'Pasta', imgSrc: 'https://t4.ftcdn.net/jpg/02/65/00/69/360_F_265006936_2dlz2VtcqZZUbco1VnDpU2diyd8OagFS.jpg', }, { id: '6', title: 'Fries', imgSrc: 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBxISEBUQEBAVFRMTERkVFRISDw8PEBIVFRcWFhcSFRYYHSggGBolGxgVITEhJSkrLi4uFx8zOD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o0687vs0HcfiywO+t69P8h5fFVgPSrf8A1h+Q2jg6nI1lp7CPZrdPyQfwLZH/AJuMkv7UPZKb+4r9p1cjsiZJpYWhGhRTzIX+c86Tcm5Nt7XdkiWNKGpBI8tjcR9RXlU3PZ3LYAAdCKAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAf/Z', }, ], }, ]; const ItemComponent = ({item, index}: {item: Item; index: number}) => { useEffect(() => { console.log('Mount of', index, item.title); return () => { console.log('Unmount of', index, item.title); }; }, []); return ( <View style={styles.card}> <Text style={styles.cardText}>{item.title}</Text> <Image source={{uri: item.imgSrc}} style={styles.imageStyles} /> </View> ); }; const App = () => { const [data, setData] = useState(DATA); const fetchNextPage = () => { const newInnerData = [...data[0].data, ...additionalFruits]; setData([{...data[0], data: newInnerData}]); }; return ( <SafeAreaView style={styles.container}> <View style={styles.scrollView}> <SectionList keyExtractor={keyExtractor} onEndReachedThreshold={0.5} onEndReached={fetchNextPage} sections={data} renderItem={renderItem} ItemSeparatorComponent={() => <View style={{height: 16}} />} renderSectionHeader={({section}) => ( <Text style={styles.sectionHeaderText}>{section.title}</Text> )} /> </View> </SafeAreaView> ); }; const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#F5F5F5', paddingTop: StatusBar.currentHeight, }, scrollView: { paddingHorizontal: 16, }, card: { backgroundColor: '#FFFFFF', flexDirection: 'row', alignItems: 'center', justifyContent: 'center', gap: 8, height: 200, padding: 16, borderRadius: 8, marginBottom: 16, borderWidth: 1, }, cardText: { fontSize: 30, }, headerText: { fontSize: 24, textAlign: 'center', marginBottom: 12, }, footerText: { fontSize: 24, textAlign: 'center', marginTop: 12, }, sectionHeaderText: { backgroundColor: '#FFFFFF', fontSize: 24, fontWeight: 'bold', }, imageStyles: { height: '100%', width: 100, }, }); export default App; ``` Reviewed By: yungsters Differential Revision: D54301991 Pulled By: javache fbshipit-source-id: d2cfafb1b4eb868761d849111b5abb7e9d584a46
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
Weirdly when using a Section List specifically when using the SectionSeperatorComponent, and adding additional Data on the renderItem List, there is a unmount occurring as the new Data is added, this only happens while using the
SectionSeparatorComponentand limited to the sameHere is the Current Behaviour
Screen.Recording.2024-01-20.at.11.35.55.PM.mov
Here is the Expected Behaviour After Fixes
Screen.Recording.2024-01-20.at.11.32.43.PM.mov
The issues is happening because the Parent View gets removed, as we add Data, the separator moves causing the unmount, the solution that i proposed here is to conditionally render to make sure that our separator renders correctly which required to add an empty view as the
itemSeparatorComponent.Changelog:
[General] [Added] - Fixes SectionList Unmounting issue with separatorComponent on data addition and removal.
Test Plan:
i used the
rn-testerApp to test out the changes below is the code to test the same.Update the snapshots to align with the changes.