Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FlatList ItemSeparatorComponent doesn't re-render when item change #24592

Closed
brunolemos opened this issue Apr 25, 2019 · 0 comments
Closed

FlatList ItemSeparatorComponent doesn't re-render when item change #24592

brunolemos opened this issue Apr 25, 2019 · 0 comments
Labels
Bug Component: FlatList Resolution: Locked This issue was locked by the bot.

Comments

@brunolemos
Copy link
Contributor

brunolemos commented Apr 25, 2019

🐛 Bug Report

FlatList's ItemSeparatorComponent receives a leadingItem prop, but when this item change the ItemSeparatorComponent doesn't get updated with the new leadingItem value.

To Reproduce

  • Create a FlatList with ItemSeparatorComponent
  • Make the ItemSeparatorComponent render different colors based on the received leadingItem
  • Change the item at runtime
  • ItemSeparatorComponent doesn't update

Expected Behavior

ItemSeparatorComponent should re-render when the leadingItem change as the default behavior.

Code Example

https://snack.expo.io/@brunolemos/itemseparatorcomponent-bug

See code
import * as React from 'react';
import { Button, View, StyleSheet, FlatList } from 'react-native';

export default class App extends React.Component {
  state = {
    items: [
      { id: 1, isRead: false },
      { id: 2, isRead: false },
      { id: 3, isRead: true },
      { id: 4, isRead: true },
      { id: 5, isRead: true },
      { id: 6, isRead: false },
      { id: 7, isRead: false },
      { id: 8, isRead: false },
      { id: 9, isRead: false },
      { id: 10, isRead: false },
    ],
  };

  renderItem = ({ item }) => (
    <View
      style={{
        flex: 1,
        height: 50,
        backgroundColor: item.isRead ? '#999999' : '#CCCCCC',
      }}
    />
  );

  renderSeparator = ({ leadingItem }) => (
    <View
      style={{
        width: '100%',
        height: 2,
        backgroundColor: leadingItem && leadingItem.isRead ? 'black' : 'blue',
      }}
    />
  );

  render() {
    return (
      <View style={styles.container}>
        <FlatList
          ItemSeparatorComponent={this.renderSeparator}
          data={this.state.items}
          renderItem={this.renderItem}
          style={{ flex: 1 }}
        />

        <Button
          title="Mark all as read"
          onPress={() =>
            this.setState(state => ({
              items: state.items.map(item => ({ ...item, isRead: true })),
            }))
          }
        />
        <Button
          title="Mark all as unread"
          onPress={() =>
            this.setState(state => ({
              items: state.items.map(item => ({ ...item, isRead: false })),
            }))
          }
        />
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    paddingTop: 21,
    backgroundColor: '#ecf0f1',
  },
});

GIF: Item separator is black when item is marked as read, and blue when not. When marking all items as read, all separators should turn black, but they are not being re-rendered.

Environment

See Environment

React Native Environment Info:
System:
OS: macOS 10.14.4
CPU: (4) x64 Intel(R) Core(TM) i5-6267U CPU @ 2.90GHz
Memory: 61.35 MB / 16.00 GB
Shell: 3.2.57 - /bin/bash
Binaries:
Node: 11.5.0 - /usr/local/bin/node
Yarn: 1.13.0 - ~/.yarn/bin/yarn
npm: 6.7.0 - /usr/local/bin/npm
Watchman: 4.9.0 - /usr/local/bin/watchman
SDKs:
iOS SDK:
Platforms: iOS 12.2, macOS 10.14, tvOS 12.2, watchOS 5.2
Android SDK:
API Levels: 19, 22, 23, 24, 25, 27, 28
Build Tools: 23.0.1, 23.0.3, 24.0.3, 25.0.0, 25.0.1, 25.0.2, 25.0.3, 26.0.0, 26.0.1, 26.0.2, 26.0.3, 27.0.0, 27.0.1, 27.0.2, 27.0.3, 28.0.0, 28.0.2, 28.0.3
System Images: android-19 | Google APIs Intel x86 Atom, android-28 | Google APIs Intel x86 Atom, android-28 | Google Play Intel x86 Atom
IDEs:
Android Studio: 3.3 AI-182.5107.16.33.5314842
Xcode: 10.2.1/10E1001 - /usr/bin/xcodebuild
npmGlobalPackages:
react-native-git-upgrade: 0.2.7

@brunolemos brunolemos changed the title FlatList ItemSeparatorComponent doesn't re-render when item changes FlatList ItemSeparatorComponent doesn't re-render when item change Apr 25, 2019
brunolemos added a commit to brunolemos/react-native that referenced this issue Jun 1, 2019
brunolemos added a commit to brunolemos/react-native that referenced this issue Jun 1, 2019
M-i-k-e-l pushed a commit to M-i-k-e-l/react-native that referenced this issue Mar 10, 2020
…book#25114)

Summary:
Fix facebook#24592

Just added a `getDerivedStateFromProps`, similar to what `VirtualizedSectionList` already does: https://github.com/facebook/react-native/blob/18fededae085b53b01e54a7ed27e32c2318e7cae/Libraries/Lists/VirtualizedSectionList.js#L470-L492

## Changelog

[General] [Fixed] - Fix ItemSeparatorComponent's leadingItem prop not being updated
Pull Request resolved: facebook#25114

Differential Revision: D15602460

Pulled By: cpojer

fbshipit-source-id: b16a82912fd746a956f6aa360d18ade53357f634
@facebook facebook locked as resolved and limited conversation to collaborators Jun 3, 2020
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Jun 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Component: FlatList Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants