I'm trying to load a few items on the screen from the backend:
async function loadIncidents() {
try {
const response = await api.get("incidents");
const theData = response.data;
console.log(theData);
setIncidents(theData);
console.log(incidents);
} catch (e) {
console.error(e.message);
}
}
useEffect(() => {
loadIncidents();
}, []);
It does work and I can see my data in logged, but only when the Flatlist, responsible to render each item, is not commented it doesn't populate my array and shows me an error:
Invalid attempt to spread non-iterable instance. In order to be iterable, non-array objects must have a Symbol.iterator method. - node_modules@babel\runtime\helpers\nonIterableSpread.js:2:22 in _nonIterableSpread - node_modules@babel\runtime\helpers\toConsumableArray.js:10:111 in _toConsumableArray * src\pages\Incidents\index.js:54:6 in Incidents - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:10696:27 in renderWithHooks - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:12842:6 in updateFunctionComponent - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:307:15 in invokeGuardedCallbackImpl - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:531:36 in invokeGuardedCallback - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:20488:8 in beginWork$$1 - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:19370:24 in performUnitOfWork - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:19347:39 in workLoopSync - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:18997:22 in renderRoot * [native code]:null in renderRoot - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:18709:28 in runRootCallback * [native code]:null in runRootCallback - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:5642:32 in runWithPriority$argument_1 - node_modules\scheduler\cjs\scheduler.development.js:643:23 in unstable_runWithPriority - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:5638:22 in flushSyncCallbackQueueImpl - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:5627:28 in flushSyncCallbackQueue - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:18556:30 in scheduleUpdateOnFiber - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:11484:17 in dispatchAction * [native code]:null in dispatchAction * src\pages\Incidents\index.js:29:18 in loadIncidents - node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch - node_modules\regenerator-runtime\runtime.js:274:30 in invoke - node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch - node_modules\regenerator-runtime\runtime.js:135:28 in invoke - node_modules\regenerator-runtime\runtime.js:145:19 in PromiseImpl.resolve.then$argument_0 - node_modules\promise\setimmediate\core.js:37:14 in tryCallOne - node_modules\promise\setimmediate\core.js:123:25 in setImmediate$argument_0
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:146:14 in _callTimer - node_modules\react-native\Libraries\Core\Timers\JSTimers.js:194:17 in _callImmediatesPass - node_modules\react-native\Libraries\Core\Timers\JSTimers.js:458:30 in callImmediates * [native code]:null in callImmediates - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:407:6 in __callImmediates - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:143:6 in __guard$argument_0 - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 in __guard - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:142:17 in __guard$argument_0 * [native code]:null in flushedQueue * [native code]:null in callFunctionReturnFlushedQueueWarning: %s: Error boundaries should implement getDerivedStateFromError(). In that method, return a state update to display an error message or fallback UI., RootErrorBoundary - node_modules\react-native\Libraries\YellowBox\YellowBox.js:63:8 in console.error - node_modules\expo\build\environment\muteWarnings.fx.js:27:24 in error - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:645:36 in warningWithoutStack - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:18056:16 in callback - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:7595:16 in callCallback - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:7639:19 in commitUpdateEffects - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:7630:22 in commitUpdateQueue - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:17116:10 in commitLifeCycles - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:20002:23 in commitLayoutEffects - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:307:15 in invokeGuardedCallbackImpl - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:531:36 in invokeGuardedCallback - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:19768:10 in commitRootImpl * [native code]:null in commitRootImpl - node_modules\scheduler\cjs\scheduler.development.js:643:23 in unstable_runWithPriority - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:19590:4 in commitRoot * [native code]:null in commitRoot - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:18709:28 in runRootCallback * [native code]:null in runRootCallback - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:5642:32 in runWithPriority$argument_1 - node_modules\scheduler\cjs\scheduler.development.js:643:23 in unstable_runWithPriority - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:5638:22 in flushSyncCallbackQueueImpl - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:5627:28 in flushSyncCallbackQueue - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:18556:30 in scheduleUpdateOnFiber - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:11484:17 in dispatchAction * [native code]:null in dispatchAction * src\pages\Incidents\index.js:29:18 in loadIncidents - node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch - node_modules\regenerator-runtime\runtime.js:274:30 in invoke - node_modules\regenerator-runtime\runtime.js:45:44 in tryCatch - node_modules\regenerator-runtime\runtime.js:135:28 in invoke - node_modules\regenerator-runtime\runtime.js:145:19 in PromiseImpl.resolve.then$argument_0 - node_modules\promise\setimmediate\core.js:37:14 in tryCallOne - node_modules\promise\setimmediate\core.js:123:25 in setImmediate$argument_0
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:146:14 in _callTimer - node_modules\react-native\Libraries\Core\Timers\JSTimers.js:194:17 in _callImmediatesPass - node_modules\react-native\Libraries\Core\Timers\JSTimers.js:458:30 in callImmediates * [native code]:null in callImmediates - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:407:6 in __callImmediates - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:143:6 in __guard$argument_0 - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 in __guard r - node_modules\react-native\Libraries\Core\Timers\JSTimers.js:194:17 in _callImmediatesPass - node_modules\react-native\Libraries\Core\Timers\JSTimers.js:458:30 in callImmediates * [native code]:null in callImmediates - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:407:6 in __callImmediates - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:143:6 in __guard$argument_0 - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 in __guard - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:142:17 in __guard$argument_0 * [native code]:null in flushedQueue * [native code]:null in callFunctionReturnFlushedQueue
It looks like the Flatlist is preventing my function loadIncidents() from populating my array, when Flatlist is present and calling my variable the array return empty. I tried a few ways to debug it but won't work. Here's the Flatlist, there's nothing special:
<FlatList
data={incidents}
style={styles.incidentList}
keyExtractor={(incident) => String(incident.id)}
showsVerticalScrollIndicator={false}
renderItem={({ item: incident }) => (
<View style={styles.incident}>
<Text style={styles.incidentProperty}>NGO:</Text>
<Text style={styles.incidentValue}>{incident.name}</Text>
</View>
)}
/>