如何使用具有嵌套属性的对象传播? [英] How to use object spread with nested properties?
本文介绍了如何使用具有嵌套属性的对象传播?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在我的减速器 (react-redux) 中返回以下内容,但它给了我一个语法错误:
I'm trying to return the following in my reducer (react-redux) and it's giving me a syntax error:
return { ...state, loginForm.email: action.payload.email }
state = { loginForm: { email: '', password: '' } } so on
我已经安装了 babel 预设阶段 0 和 es2015.这工作正常:
I have babel preset stage 0 installed and es2015. This works fine:
return { ..state, loginForm: action.payload }
推荐答案
由于此键而导致的错误:
Error you are getting because of the this key:
loginForm.email
它不是有效的对象键.
这样写:
return {
...state,
loginForm: {
...state.loginForm,
email: action.payload.email
}
}
这篇关于如何使用具有嵌套属性的对象传播?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文