React Native报错:Requested keys of a value that is not an object 求助
Hey there, let's break down what's going on here and get your ListView working with those product objects.
First: You're passing the wrong data to createDataSource
The error points straight to this.createDataSource(this.props) — and that's likely the core issue. From your description, awaitingOrder is a specific array field inside your props, right? But you're passing the entire props object (which is an object, not an array) to the method.
When you manually test with ['hello','hello'], you're passing a valid array, which is why it works. But when you pass this.props, the ListView's DataSource tries to process an object like it's an array, which triggers that "Requested keys of a value that is not an object" error.
Fix this first: Change your call to:
this.createDataSource(this.props.awaitingOrder)
Second: Make sure createDataSource handles object arrays properly
If your current createDataSource looks like this (built for string arrays):
createDataSource(data) { return new ListView.DataSource({ rowHasChanged: (r1, r2) => r1 !== r2, }).cloneWithRows(data); }
It'll work for strings, but for objects, the rowHasChanged logic needs to check actual content (since two objects with identical data will have different references). Update it to compare the product field instead, plus add a safety net for invalid data:
createDataSource(data) { // Ensure we always pass an array to avoid type errors const safeData = Array.isArray(data) ? data : []; return new ListView.DataSource({ rowHasChanged: (r1, r2) => r1.product !== r2.product, }).cloneWithRows(safeData); }
Third: Update your renderRow to handle objects
If your renderRow was built for strings (like <Text>{rowData}</Text>), it'll just show [object Object] for your product entries. Update it to access the product property:
renderRow(rowData) { return <Text>{rowData.product}</Text>; }
Quick recap of steps to test:
- Swap
this.propsforthis.props.awaitingOrderin yourcreateDataSourcecall - Add the safety check and update
rowHasChangedincreateDataSource - Adjust
renderRowto display theproductfield from each object
That should resolve the error and get your ListView rendering the product data correctly.
内容的提问来源于stack exchange,提问作者Sarmad Shah

