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
I have babel preset stage 0 installed and es2015. This works fine:
return { ..state, loginForm: action.payload }
The spread operator only creates a new address location for the top-level elements. Any nested objects of newObject are still at the same address locations as the nested objects of object . This means that we need to apply the spread operator at every level we want make a true value copy.
Spread syntax can be used when all elements from an object or array need to be included in a new array or object, or should be applied one-by-one in a function call's arguments list.
The sub-properties of objects can be accessed by chaining together the dot or bracket notation .
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 } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With