简体   繁体   English

未处理的拒绝(TypeError):散布不可迭代实例的无效尝试

[英]Unhandled Rejection (TypeError): Invalid attempt to spread non-iterable instance

I get the error from the code: 我从代码中得到错误:

 state: { route: 'signin', isSignedIn: false, news: {}, weather: {}, sports: [], clothes: {}, selectedTeam: "", user: { id: '', name: '', email: '', profimg: '', images: [], tasks: [] } } newTask = task => { const { user, tasks } = this.state; this.setState({ user: { ...user, tasks: [ ...tasks, task ] } }) } 
I provided the state structure, and the function. 我提供了状态结构和功能。 What is the problem? 问题是什么? What the error means as non-iterable? 该错误表示不可迭代是什么意思?

The array tasks is inside of object user . 数组tasks在对象user内部。

You can execute a destructure statement as follow: 您可以执行以下分解语句:

const { user, tasks = [user] } = this.state

 let state = { route: 'signin', isSignedIn: false, news: {}, weather: {}, sports: [], clothes: {}, selectedTeam: "", user: { id: '', name: '', email: '', profimg: '', images: [], tasks: ['EleFromStack'] } }; let newTask = task => { const { user, tasks = [user] } = state; console.log(JSON.stringify({ user: { ...user, tasks: [ ...tasks, task ] } }, null, 2)) }; newTask('SOQuestion') 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React + Redux:未处理的拒绝(TypeError):散布不可迭代实例的无效尝试 - React + Redux: Unhandled Rejection (TypeError): Invalid attempt to spread non-iterable instance 未处理的拒绝(TypeError):传播不可迭代实例的尝试无效我该如何解决 - Unhandled Rejection (TypeError): Invalid attempt to spread non-iterable instance how can I fix 未处理的拒绝(TypeError):破坏非迭代实例的无效尝试 - Unhandled Rejection (TypeError): Invalid attempt to destructure non-iterable instance 我的应用程序运行良好,但现在显示错误 [Unhandled promise 拒绝:TypeError:传播不可迭代实例的无效尝试 - my app was working good but now shows the error [Unhandled promise rejection: TypeError: Invalid attempt to spread non-iterable instance 未处理的运行时错误类型错误:传播不可迭代实例的无效尝试 - Unhandled Runtime Error TypeError: Invalid attempt to spread non-iterable instance React Uncaught TypeError:传播不可迭代实例的无效尝试 - React Uncaught TypeError: Invalid attempt to spread non-iterable instance 类型错误:传播不可迭代实例和合成事件的尝试无效 - TypeError: Invalid attempt to spread non-iterable instance and Synthetic Events TypeError:传播不可迭代实例的无效尝试 - TypeError: Invalid attempt to spread non-iterable instance 传播不可迭代实例的尝试无效 - Invalid attempt to spread non-iterable instance 将项目添加到 Reducer 中的空数组中会给出错误“类型错误:传播不可迭代实例的尝试无效”。 - Add Items into Empty array in Reducer give Error 'TypeError: Invalid attempt to spread non-iterable instance.'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM