【发布时间】:2020-07-30 12:54:19
【问题描述】:
我正在尝试从后端加载屏幕上的一些项目:
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();
}, []);
它确实有效,我可以在日志中看到我的数据,但只有当负责呈现每个项目的平面列表没有被评论时,它才不会填充我的数组并向我显示错误:
传播不可迭代实例的尝试无效。为了 可迭代的非数组对象必须有 Symbol.iterator 方法。 - node_modules@babel\runtime\helpers\nonIterableSpread.js:2:22 在 _nonIterableSpread - node_modules@babel\runtime\helpers\toConsumableArray.js:10:111 在 _toConsumableArray * src\pages\Incidents\index.js:54:6 in Incidents - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:10696:27 在 renderWithHooks 中 - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:12842:6 在更新函数组件中 - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:307:15 在 invokeGuardedCallbackImpl - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:531:36 在调用GuardedCallback - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:20488:8 在开始工作$$1 - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:19370:24 在 performUnitOfWork - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:19347:39 在 workLoopSync - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:18997:22 在渲染根 * [本机代码]:renderRoot中的null - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:18709:28 在 runRootCallback * [本机代码]:runRootCallback 中的 null - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:5642:32 在 runWithPriority$argument_1 - 不稳定的runWithPriority中的node_modules\scheduler\cjs\scheduler.development.js:643:23 - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:5638:22 在 flushSyncCallbackQueueImpl - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:5627:28 在flushSyncCallbackQueue - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:18556:30 在 scheduleUpdateOnFiber - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:11484:17 在 dispatchAction * [本机代码]:dispatchAction 中的 null * src\pages\Incidents\index.js:29:18 在 loadIncidents - node_modules\regenerator-runtime\runtime.js:45:44 在 tryCatch - 调用中的 node_modules\regenerator-runtime\runtime.js:274:30 - node_modules\regenerator-runtime\runtime.js:45:44 在 tryCatch - 调用中的 node_modules\regenerator-runtime\runtime.js:135:28 - PromiseImpl.resolve.then$argument_0 中的 node_modules\regenerator-runtime\runtime.js:145:19 - node_modules\promise\setimmediate\core.js:37:14 在 tryCallOne - setImmediate$argument_0 中的 node_modules\promise\setimmediate\core.js:123:25
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:146:14 在 _callTimer - _callImmediatesPass 中的 node_modules\react-native\Libraries\Core\Timers\JSTimers.js:194:17 - node_modules\react-native\Libraries\Core\Timers\JSTimers.js:458:30 在 callImmediates * [本机代码]:callImmediates 中的 null - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:407:6 在 __callImmediates - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:143:6 在 __guard$argument_0 - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 在 __guard - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:142:17 在 __guard$argument_0 * [本机代码]:flushedQueue中的null * [native code]:null in callFunctionReturnFlushedQueue警告:%s:应实现错误边界 getDerivedStateFromError()。在该方法中,将状态更新返回到 显示错误消息或回退 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 错误 - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:645:36 在警告WithoutStack - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:18056:16 在回调中 - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:7595:16 在回调回调 - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:7639:19 在 commitUpdateEffects - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:7630:22 在提交更新队列中 - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:17116:10 在提交生命周期中 - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:20002:23 在 commitLayoutEffects - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:307:15 在 invokeGuardedCallbackImpl - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:531:36 在调用GuardedCallback - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:19768:10 在 commitRootImpl * [本机代码]:commitRootImpl 中的 null - 不稳定的runWithPriority中的node_modules\scheduler\cjs\scheduler.development.js:643:23 - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:19590:4 在 commitRoot * [本机代码]:commitRoot 中的 null - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:18709:28 在 runRootCallback * [本机代码]:runRootCallback 中的 null - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:5642:32 在 runWithPriority$argument_1 - 不稳定的runWithPriority中的node_modules\scheduler\cjs\scheduler.development.js:643:23 - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:5638:22 在 flushSyncCallbackQueueImpl - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:5627:28 在flushSyncCallbackQueue - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:18556:30 在 scheduleUpdateOnFiber - node_modules\react-native\Libraries\Renderer\implementations\ReactNativeRenderer-dev.js:11484:17 在 dispatchAction * [本机代码]:dispatchAction 中的 null * src\pages\Incidents\index.js:29:18 在 loadIncidents - node_modules\regenerator-runtime\runtime.js:45:44 在 tryCatch - 调用中的 node_modules\regenerator-runtime\runtime.js:274:30 - node_modules\regenerator-runtime\runtime.js:45:44 在 tryCatch - 调用中的 node_modules\regenerator-runtime\runtime.js:135:28 - PromiseImpl.resolve.then$argument_0 中的 node_modules\regenerator-runtime\runtime.js:145:19 - node_modules\promise\setimmediate\core.js:37:14 在 tryCallOne - setImmediate$argument_0 中的 node_modules\promise\setimmediate\core.js:123:25
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:146:14 在 _callTimer - _callImmediatesPass 中的 node_modules\react-native\Libraries\Core\Timers\JSTimers.js:194:17 - node_modules\react-native\Libraries\Core\Timers\JSTimers.js:458:30 在 callImmediates * [本机代码]:callImmediates 中的 null - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:407:6 在 __callImmediates - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:143:6 在 __guard$argument_0 - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 在 __guard r - _callImmediatesPass 中的 node_modules\react-native\Libraries\Core\Timers\JSTimers.js:194:17 - node_modules\react-native\Libraries\Core\Timers\JSTimers.js:458:30 在 callImmediates * [本机代码]:callImmediates 中的 null - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:407:6 在 __callImmediates - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:143:6 在 __guard$argument_0 - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:384:10 在 __guard - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:142:17 在 __guard$argument_0 * [本机代码]:flushedQueue中的null * [native code]:null in callFunctionReturnFlushedQueue
看起来 Flatlist 正在阻止我的函数 loadIncidents() 填充我的数组,当 Flatlist 存在并调用我的变量时,数组返回空。我尝试了几种方法来调试它,但都不起作用。 这是 Flatlist,没什么特别的:
<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>
)}
/>
【问题讨论】:
标签: javascript reactjs react-native react-native-android react-native-flatlist