简体   繁体   English

如何使用带有嵌套属性的 object 传播?

[英]How to use object spread with nested properties?

I'm trying to return the following in my reducer (react-redux) and it's giving me a syntax error:我试图在我的 reducer (react-redux) 中返回以下内容,它给我一个语法错误:

return { ...state, loginForm.email: action.payload.email }

state = { loginForm: { email: '', password: '' } } so on

I have babel preset stage 0 installed and es2015.我安装了 babel preset stage 0 和 es2015。 This works fine:这很好用:

return { ..state, loginForm: action.payload }

Error you are getting because of the this key: 因为这个键你得到错误:

loginForm.email

It's not a valid object key. 它不是有效的对象键。

Write it like this: 写这样:

return { 
    ...state, 
    loginForm: {
        ...state.loginForm,
        email: action.payload.email
    } 
}

我想你想要这样的东西:

{ ...state, loginForm: { email: action.payload.email } }

In JS, a key value of an object is either a String value or a Symbol value.在 JS 中,一个 object 的键值要么是一个 String 值,要么是一个 Symbol 值。 official docs官方文档

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM