[英]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 ] } }) }
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.