react-native: Flatlist render error: Cannot read property 'getItem' of undefined
Description
I have been working on project which has several flatlists, which are causing problem suddenly. All flatlists that I have shows same issue dispite they were working previously. To add, auto refreshing also stopped working I dont know they are related to each other or not.
React Native Version
0.71.3
Output of npx react-native info
System: OS: Windows 10 10.0.19044 CPU: (8) x64 Intel® Core™ i3-10105 CPU @ 3.70GHz Memory: 5.21 GB / 15.87 GB Binaries: Node: 18.15.0 - C:\Program Files\nodejs\node.EXE Yarn: Not Found npm: 9.5.0 - C:\Program Files\nodejs\npm.CMD Watchman: Not Found SDKs: Android SDK: API Levels: 28, 31, 33 Build Tools: 28.0.3, 30.0.3, 31.0.0, 33.0.0 System Images: android-28 | Google APIs Intel x86 Atom, android-31 | Intel x86 Atom_64, android-31 | Google Play Intel x86 Atom_64, android-33 | Google APIs Intel x86_64 Atom Android NDK: Not Found Windows SDK: Not Found IDEs: Android Studio: AI-213.7172.25.2113.9123335 Visual Studio: Not Found Languages: Java: 11.0.18 npmPackages: @react-native-community/cli: Not Found react: 18.2.0 => 18.2.0 react-native: 0.71.3 => 0.71.3 react-native-windows: Not Found npmGlobalPackages: react-native: Not Found
Steps to reproduce
Steps to reproduce:
Open the project and navigate to the screen where the flatlists are being used. Observe that all the flatlists are not rendering and showing an error. Check the console for any error messages related to the flatlists. Look for the specific error message “Cannot read property ‘getItem’ of undefined”. Verify that this error is being displayed for all the flatlists on the screen. Check if the code for the flatlists has been modified recently or if any libraries have been updated. Verify that the data source for the flatlists is correctly defined and passed as props. Make sure that the flatlist component is imported and rendered properly in the code. Check if any other component or library is conflicting with the flatlists. Provide any additional information that might be helpful for developers to identify and fix the issue. Expected outcome: The flatlists should render without any errors.
Actual outcome: The flatlists are not rendering and showing the error message “Cannot read property ‘getItem’ of undefined”.
Snack, code example, screenshot, or link to a repository
// CODE IS GIVEN BELOW IS EXAMPLE WHICH WORKS ON BRAND NEW PROJECT BUT NOT IN THE CURRENTLY USED ONE
import React, {useState} from ‘react’; import { SafeAreaView, ScrollView, StatusBar, StyleSheet, Text, Platform, View, FlatList, } from ‘react-native’; import {COLORS} from ‘…/constants/theme’; const DATA = [ {id: ‘1’, title: ‘Item 1’}, {id: ‘2’, title: ‘Item 2’}, {id: ‘3’, title: ‘Item 3’}, {id: ‘4’, title: ‘Item 4’}, {id: ‘5’, title: ‘Item 5’}, {id: ‘6’, title: ‘Item 6’}, {id: ‘7’, title: ‘Item 7’}, ];
const Item = ({title}) => ( <View style={styles.item}> <Text style={styles.title}>{title}</Text> </View> ); const Settings = () => { const [selectedId, setSelectedId] = useState(null);
const renderItem = ({item}) => <Item title={item.title} />;
return ( <View style={styles.container}> <FlatList horizontal showsHorizontalScrollIndicator={false} data={DATA} renderItem={renderItem} keyExtractor={item => item.id} extraData={selectedId} /> </View> ); };
const styles = StyleSheet.create({ container: { flex: 1, marginTop: 50, }, item: { backgroundColor: COLORS.primary, padding: 5, borderRadius: 20, marginHorizontal: 5, height: 50, justifyContent: ‘center’, alignItems: ‘center’, }, title: { fontSize: 22, color: ‘white’, }, });
export default Settings;
About this issue
- Original URL
- State: closed
- Created a year ago
- Reactions: 2
- Comments: 43 (3 by maintainers)
Commits related to this issue
- fix: 🐛 Corrige la config Babel (FlatList Error) https://github.com/facebook/react-native/issues/36828 — committed to SocialGouv/1000jours by benguedj a year ago
- fix: Cannot read property 'getItem' of undefined https://github.com/facebook/react-native/issues/36828#issuecomment-1706987942 — committed to tiddly-gittly/TidGi-Mobile by linonetwo 10 months ago
- Feature: Event list screen Summary of changes: - Implemented the Event List screen - Created the Event list item component - Created tests for the Event list item component - Updated Label, RoutineLi... — committed to romaob/rn-my-week-routine by romaob 10 months ago
- Feature: Event list screen (#19) Summary of changes: - Implemented the Event List screen - Created the Event list item component - Created tests for the Event list item component - Updated Label,... — committed to romaob/rn-my-week-routine by romaob 10 months ago
- Release: 2023-09-18 (#29) * Improvements: Added more unit tests and handled some improvements. (#8) * Feature: Tests added (#10) * Improvements: Added more unit tests and handled some improveme... — committed to romaob/rn-my-week-routine by romaob 9 months ago
- Downmerge (#30) * Improvements: Added more unit tests and handled some improvements. (#8) (#9) * Release (#16) * Improvements: Added more unit tests and handled some improvements. (#8) * Fea... — committed to romaob/rn-my-week-routine by romaob 9 months ago
- Fix Flatlist render error on Android: https://github.com/facebook/react-native/issues/36828 — committed to unicornultrafoundation/u2u-mobile-wallet by vietthai244 9 months ago
- 🚑 hotfix Flatlist bug Cannot read property 'getItem' of undefined fixes the issue https://github.com/facebook/react-native/issues/36828 — committed to a-tokyo/react-native by a-tokyo 5 months ago
- Fix for #36828 - copying props into this.props — committed to Kyle772/react-native by Kyle772 5 months ago
- Fix for #36828 - props not being copied into constructor — committed to Kyle772/react-native by Kyle772 5 months ago
- [native] Patch react-native to avoid FlatList this.props error Summary: This solves [ENG-7635](https://linear.app/comm/issue/ENG-7635/cannot-read-property-getitem-of-undefined), at least temporarily.... — committed to CommE2E/comm by Ashoat 3 months ago
- [native] Patch react-native to avoid FlatList this.props error Summary: This solves [ENG-7635](https://linear.app/comm/issue/ENG-7635/cannot-read-property-getitem-of-undefined), at least temporarily.... — committed to CommE2E/comm by Ashoat 3 months ago
- [native] Patch react-native to avoid FlatList this.props error Summary: This solves [ENG-7635](https://linear.app/comm/issue/ENG-7635/cannot-read-property-getitem-of-undefined), at least temporarily.... — committed to CommE2E/comm by Ashoat 3 months ago
@mattgle I meant that you don’t need nothing to remove from babel.config.js, as ‘@babel/plugin-proposal-private-methods’ is needed by jest. You need to install @babel/plugin-transform-flow-strip-types
yarn add -D @babel/plugin-transform-flow-strip-types
ornpm install @babel/plugin-transform-flow-strip-types --save-dev
and add it to babel.config.js before @babel/plugin-proposal-private-methods and other plugins. So your babel.config.js plugins will look like thisplugins: [ ..... '@babel/plugin-transform-flow-strip-types', '@babel/plugin-proposal-class-properties' '@babel/plugin-proposal-private-methods', ..... ],
I found the problem. After removing this code from babel.config.js and removing @babel/plugin-proposal-private-methods, everything is fine.
[ '@babel/plugin-proposal-private-methods', { loose: true, }, ],
This works! Make sure you run:
npx react-native start --reset-cache
after making the changes
Im still having the same issue. Adding
this.props = props;
undersuper(props);
in the construction around L412-L420 in node_modules/react-native/Libraries/Lists/FlatList.js fixed the issue until I tried to use EAS to compile my app to put in on TestFlight.ive removed the suggested parts of my babel.config.js above but the error still persits. Completely stuck!
Since this is a common enough plugin (as is
class-properties
), I think this should be better documented and possibly fixed in the RN codebase. Please re-open!Thanks, this works for expo:
then
This saved me a lot of time, you have described this very accurately, also to add do remember to clear the cache once while starting this by :
yarn start -- --reset-cache
@infoani92 instead of removing @babel/plugin-proposal-private-methods you could install to devDependancies and add in babel plugins before @babel/plugin-proposal-private-methods (it is important) this plugin: @babel/plugin-transform-flow-strip-types. It should help
I solved it, following this step by adding the props in the constructor alterando este construtor emnode_modules/react-native/Libraries/Lists/FlatList.js
constructor(props: Props<ItemT>) { super(props); this._checkProps(this.props); if (this.props.viewabilityConfigCallbackPairs) { this._virtualizedListPairs = // rest of code } para isso (adicionando this.props = props;)
constructor(props: Props<ItemT>) { super(props); this.props = props; this._checkProps(this.props); if (this.props.viewabilityConfigCallbackPairs) { this._virtualizedListPairs = // rest of code }
Thanks to @chizhkov422 I have checked my
babel.config.js
one more time, and removerdwhich made it work again. Thanks one more time
The issue for me was using private methods in classes. For some reason, the combination of private class methods and the babel plugins to allow for the private class methods broke the react native flatlist.
changing this constructor in
node_modules/react-native/Libraries/Lists/FlatList.js
to this (adding
this.props = props;
)fixed the issue of being allowed private classes while using the following babel.config.js
obviously when I had to remove the private classes and respective plugins from the babel config when compiling as EAS downloads the node modules and doesn’t use the locally stored files. I don’t know whether this is a bug that can be fixed or whether there is a reason this can’t be changed
Hii, In this plugins , nothing to add just remove what you add all plugins you remove
On Tue, Jun 13, 2023 at 6:39 AM Allu Lavaraju @.***> wrote:
– Thanks Allu Lavaraju Sr. Software Engineer 9912661587
nothing to add just remove
On Tue, Jun 13, 2023 at 1:46 AM mattgle.eth @.***> wrote:
– Thanks Allu Lavaraju Sr. Software Engineer 9912661587
Im not sure if I’m following you, you mean to do this right?
Delete the following lines from
babel.config.js
and then run
Thank you @chizhkov422 you saved my time
Maybe this will help solve the problem
I need those plugins bro
Yeah, I have tries this previously as well, but didnt help … (((
Thank you. This worked for me too in expo.
This fixed my problem after trying many of the other suggestions. Thank you
I have the same issue here, not resolved by others suggestions
This shouldn’t have been closed. The bug is still there. I agree with 0xFadedd: https://github.com/facebook/react-native/issues/36828#issuecomment-1589555539
The local object this.props is not initialized. The fix is simply to initialize it as 0xFadedd lucidly explained last year.
Guys, I solved this issue. It was just expo catch that was giving problem. I used
npx expo . -c
on the root of the project.this is the best answer, thanks @Roman-Hladilka
good job