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 Invariant Violation, Changing numColumns on the fly not supported - Recommended key Fix Does Not Work #50439

Open
ibrust opened this issue Apr 1, 2025 · 5 comments
Labels
Component: FlatList Needs: Attention Issues where the author has responded to feedback. Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. Newer Patch Available

Comments

@ibrust
Copy link

ibrust commented Apr 1, 2025

Description

I'm using react-native 0.76.7. I'm using a FlatList and supporting orientation changes. I'm recalculating the number of columns for the list and changing it on the fly.
I have some local state numCols for this purpose. I'm passing numCols as the key to the flatlist, as recommended by the error message and online.
The error does not go away.
The flatlist is fairly large which appears to cause a delay in rerendering, possibly effecting the timing of the flatlist redrawing. But that's the only noteworthy thing here, it's just a typical FlatList otherwise.

Steps to reproduce

  1. setup a simple react-native app with react-native version 0.76.7
  2. create a screen which uses something similar to the example code below as a component containing a FlatList:

Reproducible example code:

`
function useSimpleOrientation() {
const { width: viewportWidth, height: viewportHeight } = useViewportDimensions();
const [orientation, setOrientation] = useState(
viewportWidth > viewportHeight ? SimpleOrientation.LANDSCAPE : SimpleOrientation.PORTRAIT
);

useEffect(() => {
const sub = Dimensions.addEventListener('change', ({ window: { width, height } }) => {
if (width < height) {
setOrientation(SimpleOrientation.PORTRAIT);
} else {
setOrientation(SimpleOrientation.LANDSCAPE);
}
});
return () => {
sub.remove();
};
}, []);

return orientation;
}

export default function MyListComponent({ pageNumber }: MyListComponentProps) {
const flatListRef = React.useRef(null);
const pageSize = 60;

const [numCols, setNumCols] = useState(getProductCardNumCols(viewportWidth, sizeCategory));
const orientation = useSimpleOrientation();

React.useEffect(() => {
const newColumnCount = getNewColumnCount(sizeCategory);
if (newColumnCount !== numCols) {
setNumCols(newColumnCount);
}
}, [orientation, setNumCols, viewportWidth, sizeCategory, numCols]);

const renderEmptyComponent = useCallback(() => {
return (

No items found

);
}, []);

useEffect(() => {
flatListRef.current?.scrollToOffset({ offset: 0, animated: false });
}, [pageNumber]);

const getItemLayout = useCallback(
(data: ArrayLike | null | undefined, index: number) => ({
length: STATIC_ITEM_HEIGHT,
offset: STATIC_ITEM_HEIGHT * index,
index,
}),
[]
);

const renderItem = useCallback(({ item }: { item: ViewableItem }) => {
return ;
}, []);

const keyExtractor = useCallback((item: ViewableItem, index: number): string => {
return item.getStringId() + index.toString();
}, []);

return (
<View style={[styles.container, style, styles.flatList]}>
<FlatList
key={orientation + numCols}
ref={flatListRef}
style={styles.flatList}
data={inventory}
columnWrapperStyle={numCols > 1 ? { justifyContent: 'space-evenly' } : undefined}
ListEmptyComponent={renderEmptyComponent}
getItemLayout={getItemLayout}
numColumns={numCols}
keyExtractor={keyExtractor}
renderItem={renderProductCardInList}
initialNumToRender={pageSize}
ItemSeparatorComponent={FlatlistSeparator}
/>

);
}
`

  1. rotate the device and wait for orientation change to update the FlatList.

expected result: since the key is set to orientation + numCols, as the error message recommends, there should be no runtime error.
result: runtime error

React Native Version

0.76.7

Affected Platforms

Runtime - Android

Output of npx @react-native-community/cli info

System:
  OS: Windows 11 10.0.22621
  CPU: "(16) x64 AMD EPYC 7763 64-Core Processor                "
  Memory: 31.25 GB / 63.95 GB
Binaries:
  Node:
    version: 22.12.0
    path: C:\Program Files\nodejs\node.EXE
  Yarn: Not Found
  npm:
    version: 10.9.0
    path: C:\Program Files\nodejs\npm.CMD
  Watchman: Not Found
SDKs:
  Android SDK:
    API Levels:
      - "35"
      - "36"
    Build Tools:
      - 34.0.0
      - 35.0.0
      - 36.0.0
    System Images:
      - android-30 | Google APIs Intel x86 Atom
      - android-36 | Pre-Release 16 KB Page Size Google Play ARM Intel x86_64
        Atom
    Android NDK: Not Found
  Windows SDK: Not Found
IDEs:
  Android Studio: AI-241.17011.79.2412.11938461
  Visual Studio:
    - 17.11.35312.102 (Visual Studio Community 2022)
Languages:
  Java: 23.0.2
  Ruby: Not Found
npmPackages:
  "@react-native-community/cli": Not Found
  react:
    installed: 18.3.1
    wanted: 18.3.1
  react-native:
    installed: 0.76.7
    wanted: 0.76.7
  react-native-windows: Not Found
npmGlobalPackages:
  "*react-native*": Not Found
Android:
  hermesEnabled: Not found
  newArchEnabled: Not found
iOS:
  hermesEnabled: Not found
  newArchEnabled: Not found

info React Native v0.78.2 is now available (your project is running on v0.76.7).
info Changelog: https://github.com/facebook/react-native/releases/tag/v0.78.2
info Diff: https://react-native-community.github.io/upgrade-helper/?from=0.76.7&to=0.78.2
info For more info, check out "https://reactnative.dev/docs/upgrading?os=windows".

Stacktrace or Logs

Warning: Invariant Violation: Changing numColumns on the fly is not supported. Change the key prop on FlatList when changing the number of columns to force a fresh render of the component.

This error is located at:
    in FlatList (created by SeasonalTab)
    in RCTView (created by View)
    in View (created by SeasonalTab)
    in RCTView (created by View)
    in View (created by ScrollView)
    in RCTScrollView
    in VScrollViewNativeComponent (created by ScrollView)
    in ScrollView (created by ScrollView)
    in Wrapper (created by NativeViewGestureHandler)
    in NativeViewGestureHandler (created by ScrollView)
    in ScrollView
    in Unknown (created by SeasonalTab)
    in SeasonalTab (created by SceneView)
    in StaticContainer
    in EnsureSingleNavigator (created by SceneView)
    in SceneView (created by MaterialTopTabNavigator)
    in RCTView (created by View)
    in View (created by SceneView)
    in SceneView (created by PagerViewAdapter)
    in RCTView (created by View)
    in View (created by PagerViewInternal)
    in RNCViewPager (created by PagerViewInternal)
    in PagerViewInternal
    in Unknown (created by Animated(Anonymous))
    in Animated(Anonymous) (created by PagerViewAdapter)
    in PagerViewAdapter (created by TabView)
    in RCTView (created by View)
    in View (created by TabView)
    in TabView (created by MaterialTopTabView)
    in MaterialTopTabView (created by MaterialTopTabNavigator)
    in PreventRemoveProvider (created by NavigationContent)
    in NavigationContent
    in Unknown (created by MaterialTopTabNavigator)
    in MaterialTopTabNavigator (created by ProductsTabNavigator)
    in ProductsTabNavigator (created by SceneView)
    in StaticContainer
    in EnsureSingleNavigator (created by SceneView)
    in SceneView (created by StackNavigator)
    in RCTView (created by View)
    in View (created by CardContainerInner)
    in RCTView (created by View)
    in View (created by CardContainerInner)
    in RCTView (created by View)
    in View
    in CardSheet (created by Card)
    in RCTView (created by View)
    in View (created by Animated(View))
    in Animated(View) (created by PanGestureHandler)
    in PanGestureHandler (created by PanGestureHandler)
    in PanGestureHandler (created by Card)
    in RCTView (created by View)
    in View (created by Animated(View))
    in Animated(View) (created by Card)
    in RCTView (created by View)
    in View (created by Card)
    in Card (created by CardContainerInner)
    in CardContainerInner (created by CardStack)
    in RNSScreen (created by Animated(Anonymous))
    in Animated(Anonymous)
    in Suspender (created by Freeze)
    in Suspense (created by Freeze)
    in Freeze (created by DelayedFreeze)
    in DelayedFreeze
    in InnerScreen (created by Screen)
    in Screen (created by MaybeScreen)
    in MaybeScreen (created by CardStack)
    in RNSScreenContainer (created by ScreenContainer)
    in ScreenContainer (created by MaybeScreenContainer)
    in MaybeScreenContainer (created by CardStack)
    in RCTView (created by View)
    in View (created by CardStack)
    in CardStack (created by HeaderShownContext)
    in RCTView (created by View)
    in View (created by SafeAreaProviderCompat)
    in SafeAreaProviderCompat (created by StackView)
    in RNGestureHandlerRootView (created by GestureHandlerRootView)
    in GestureHandlerRootView (created by StackView)
    in StackView (created by StackNavigator)
    in PreventRemoveProvider (created by NavigationContent)
    in NavigationContent
    in Unknown (created by StackNavigator)
    in StackNavigator (created by ProductsNavigator)
    in ProductsNavigator (created by SceneView)
    in StaticContainer
    in EnsureSingleNavigator (created by SceneView)
    in SceneView (created by BottomTabNavigator)
    in RCTView (created by View)
    in View (created by Screen)
    in RCTView (created by View)
    in View (created by Animated(View))
    in Animated(View) (created by Background)
    in Background (created by Screen)
    in Screen (created by BottomTabView)
    in RNSScreen (created by Animated(Anonymous))
    in Animated(Anonymous)
    in Suspender (created by Freeze)
    in Suspense (created by Freeze)
    in Freeze (created by DelayedFreeze)
    in DelayedFreeze
    in InnerScreen (created by Screen)
    in Screen (created by MaybeScreen)
    in MaybeScreen (created by BottomTabView)
    in RNSScreenContainer (created by ScreenContainer)
    in ScreenContainer (created by MaybeScreenContainer)
    in MaybeScreenContainer (created by BottomTabView)
    in RCTView (created by View)
    in View (created by SafeAreaProviderCompat)
    in SafeAreaProviderCompat (created by BottomTabView)
    in BottomTabView (created by BottomTabNavigator)
    in PreventRemoveProvider (created by NavigationContent)
    in NavigationContent
    in Unknown (created by BottomTabNavigator)
    in BottomTabNavigator (created by BottomTabNavigator)
    in RCTView (created by View)
    in View (created by BottomTabNavigator)
    in BottomTabNavigator (created by SceneView)
    in StaticContainer
    in EnsureSingleNavigator (created by SceneView)
    in SceneView (created by DrawerNavigator)
    in RCTView (created by View)
    in View (created by Screen)
    in RCTView (created by View)
    in View (created by Animated(View))
    in Animated(View) (created by Background)
    in Background (created by Screen)
    in Screen (created by DrawerViewBase)
    in RNSScreen (created by Animated(Anonymous))
    in Animated(Anonymous)
    in Suspender (created by Freeze)
    in Suspense (created by Freeze)
    in Freeze (created by DelayedFreeze)
    in DelayedFreeze
    in InnerScreen (created by Screen)
    in Screen (created by MaybeScreen)
    in MaybeScreen (created by DrawerViewBase)
    in RNSScreenContainer (created by ScreenContainer)
    in ScreenContainer (created by MaybeScreenContainer)
    in MaybeScreenContainer (created by DrawerViewBase)
    in RCTView (created by View)
    in View (created by Drawer)
    in RCTView (created by View)
    in View (created by AnimatedComponent(View))
    in AnimatedComponent(View)
    in Unknown (created by Drawer)
    in RCTView (created by View)
    in View (created by AnimatedComponent(View))
    in AnimatedComponent(View)
    in Unknown (created by Drawer)
    in Wrap (created by AnimatedComponent(Wrap))
    in AnimatedComponent(Wrap)
    in Unknown (created by GestureDetector)
    in GestureDetector (created by Drawer)
    in RNGestureHandlerRootView (created by GestureHandlerRootView)
    in GestureHandlerRootView (created by Drawer)
    in Drawer (created by DrawerViewBase)
    in DrawerViewBase (created by DrawerView)
    in RCTView (created by View)
    in View (created by SafeAreaProviderCompat)
    in SafeAreaProviderCompat (created by DrawerView)
    in DrawerView (created by DrawerNavigator)
    in PreventRemoveProvider (created by NavigationContent)
    in NavigationContent
    in Unknown (created by DrawerNavigator)
    in DrawerNavigator (created by DrawerNavigator)
    in DrawerNavigator (created by SceneView)
    in StaticContainer
    in EnsureSingleNavigator (created by SceneView)
    in SceneView (created by StackNavigator)
    in RCTView (created by View)
    in View (created by CardContainerInner)
    in RCTView (created by View)
    in View (created by CardContainerInner)
    in RCTView (created by View)
    in View
    in CardSheet (created by Card)
    in RCTView (created by View)
    in View (created by Animated(View))
    in Animated(View) (created by PanGestureHandler)
    in PanGestureHandler (created by PanGestureHandler)
    in PanGestureHandler (created by Card)
    in RCTView (created by View)
    in View (created by Animated(View))
    in Animated(View) (created by Card)
    in RCTView (created by View)
    in View (created by Card)
    in Card (created by CardContainerInner)
    in CardContainerInner (created by CardStack)
    in RNSScreen (created by Animated(Anonymous))
    in Animated(Anonymous)
    in Suspender (created by Freeze)
    in Suspense (created by Freeze)
    in Freeze (created by DelayedFreeze)
    in DelayedFreeze
    in InnerScreen (created by Screen)
    in Screen (created by MaybeScreen)
    in MaybeScreen (created by CardStack)
    in RNSScreenContainer (created by ScreenContainer)
    in ScreenContainer (created by MaybeScreenContainer)
    in MaybeScreenContainer (created by CardStack)
    in RCTView (created by View)
    in View (created by CardStack)
    in CardStack (created by HeaderShownContext)
    in RCTView (created by View)
    in View (created by SafeAreaProviderCompat)
    in SafeAreaProviderCompat (created by StackView)
    in RNGestureHandlerRootView (created by GestureHandlerRootView)
    in GestureHandlerRootView (created by StackView)
    in StackView (created by StackNavigator)
    in PreventRemoveProvider (created by NavigationContent)
    in NavigationContent
    in Unknown (created by StackNavigator)
    in StackNavigator (created by AppStackNavigator)
    in AppStackNavigator (created by SceneView)
    in StaticContainer
    in EnsureSingleNavigator (created by SceneView)
    in SceneView (created by StackNavigator)
    in RCTView (created by View)
    in View (created by CardContainerInner)
    in RCTView (created by View)
    in View (created by CardContainerInner)
    in RCTView (created by View)
    in View
    in CardSheet (created by Card)
    in RCTView (created by View)
    in View (created by Animated(View))
    in Animated(View) (created by PanGestureHandler)
    in PanGestureHandler (created by PanGestureHandler)
    in PanGestureHandler (created by Card)
    in RCTView (created by View)
    in View (created by Animated(View))
    in Animated(View) (created by Card)
    in RCTView (created by View)
    in View (created by Card)
    in Card (created by CardContainerInner)
    in CardContainerInner (created by CardStack)
    in RNSScreen (created by Animated(Anonymous))
    in Animated(Anonymous)
    in Suspender (created by Freeze)
    in Suspense (created by Freeze)
    in Freeze (created by DelayedFreeze)
    in DelayedFreeze
    in InnerScreen (created by Screen)
    in Screen (created by MaybeScreen)
    in MaybeScreen (created by CardStack)
    in RNSScreenContainer (created by ScreenContainer)
    in ScreenContainer (created by MaybeScreenContainer)
    in MaybeScreenContainer (created by CardStack)
    in RCTView (created by View)
    in View (created by CardStack)
    in CardStack (created by HeaderShownContext)
    in RCTView (created by View)
    in View (created by SafeAreaProviderCompat)
    in SafeAreaProviderCompat (created by StackView)
    in RNGestureHandlerRootView (created by GestureHandlerRootView)
    in GestureHandlerRootView (created by StackView)
    in StackView (created by StackNavigator)
    in PreventRemoveProvider (created by NavigationContent)
    in NavigationContent
    in Unknown (created by StackNavigator)
    in StackNavigator (created by RootNavigator)
    in RootNavigator (created by App)
    in ThemeProvider
    in EnsureSingleNavigator
    in BaseNavigationContainer
    in NavigationContainerInner (created by CustomNavigationContainer)
    in CustomNavigationContainer (created by App)
    in RNCSafeAreaProvider (created by SafeAreaProvider)
    in SafeAreaProvider (created by App)
    in RCTView (created by View)
    in View (created by CustomActionSheet)
    in RCTView (created by View)
    in View (created by CustomActionSheet)
    in CustomActionSheet
    in ActionSheetProvider (created by App)
    in ThemeProvider (created by PaperProvider)
    in RCTView (created by View)
    in View (created by Portal.Host)
    in Portal.Host (created by PaperProvider)
    in RNCSafeAreaProvider (created by SafeAreaProvider)
    in SafeAreaProvider (created by SafeAreaInsetsContext)
    in SafeAreaProviderCompat (created by PaperProvider)
    in PaperProvider (created by CustomPaperProvider)
    in CustomPaperProvider (created by App)
    in ServerSideConfigProvider (created by App)
    in RNGestureHandlerRootView (created by GestureHandlerRootView)
    in GestureHandlerRootView (created by App)
    in QueryClientProvider (created by App)
    in ErrorBoundary (created by App)
    in Provider (created by App)
    in App (created by withDevTools(App))
    in withDevTools(App)
    in RCTView (created by View)
    in View (created by AppContainer)
    in RCTView (created by View)
    in View (created by AppContainer)
    in AppContainer
    in main(RootComponent)

Reproducer

It's not my job to maintain this repo, it's yours.

Screenshots and Videos

No response

@react-native-bot
Copy link
Collaborator

Tip

Newer version available: You are on a supported minor version, but it looks like there's a newer patch available - 0.76.8. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.

@react-native-bot react-native-bot added Component: FlatList Needs: Author Feedback Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. labels Apr 1, 2025
@react-native-bot
Copy link
Collaborator

Tip

Newer version available: You are on a supported minor version, but it looks like there's a newer patch available - undefined. Please upgrade to the highest patch for your minor or latest and verify if the issue persists (alternatively, create a new project and repro the issue in it). If it does not repro, please let us know so we can close out this issue. This helps us ensure we are looking at issues that still exist in the most recent releases.

@react-native-bot
Copy link
Collaborator

Warning

Missing reproducer: We could not detect a reproducible example in your issue report. Please provide either:

@sarthak-d11
Copy link
Collaborator

Hey @ibrust , It would be great if you could share a reproducer for this issue. You can use the following template to help with that: Reproducer Template.

Additionally, could you confirm if this issue is reproducible on Android, iOS, or both?

While reviewing the output of npx @react-native-community/cli info, we couldn’t determine if the new architecture is enabled, as the newArchEnabled field is not found. Any insights on this would be appreciated.

@ibrust
Copy link
Author

ibrust commented Apr 3, 2025

@sarthak-d11 new arch is enabled. I do not have time to reproduce and test this for you, I have my own work to do. My recommendation would be you test it in a baseline project with similar settings as output from @react-native-community/cli info, as close as you can approximate the settings, and if you see no issue then close the ticket.

@github-actions github-actions bot added Needs: Attention Issues where the author has responded to feedback. and removed Needs: Author Feedback labels Apr 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: FlatList Needs: Attention Issues where the author has responded to feedback. Needs: Repro This issue could be improved with a clear list of steps to reproduce the issue. Newer Patch Available
Projects
None yet
Development

No branches or pull requests

3 participants