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
}
}
Answer from Mayank Shukla on Stack OverflowError 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
}
}
In JS, a key value of an object is either a String value or a Symbol value. official docs
How to avoid overwriting nested object with spread operator?
arrays - How to spread a nested object in Javascript? - Stack Overflow
Using Spread operator to update the state in react
does redux toolkit really make nested object property updates as easy as this?
b is overwritten:
const shared = { a : { b : 1 } }
const added = { ...shared, a : { c : 2 } }
console.log(added);Actual use case is below. I was testing the above in JSBin because I thought this might be a concern.
How can I avoid overwriting b (or voice in my real example)? I want to set the ssmlGender in shared options, but not the languageCode.
I'm thinking I might have to set properties one by one using dot or bracket notation? Or is there a more concise way?
const sharedOptions = {
, voice : { languageCode : "", ssmlGender : voiceGender.male }
, audioConfig : {audioEncoding : "OGG_OPUS"}
}
const foreignAudioOptions = {
...sharedOptions
, input : {text : ""}
}
const englishAudioOptions = {
...sharedOptions
, input : {text : ""}
}Not sure if this is an issue with the way you test your actual problem, but you need to spread your object into an object - in the console.log call I'm not sure what you want the end result to be but you if you spread into an object it should work:
try {
console.log(
{...{
autre_panne: { nbr_op_sav: 12, percentage_op_sav: 0 },
fonctionnement_bluetooth: { nbr_op_sav: 13, percentage_op_sav: 0 },
memoire_interne: { nbr_op_sav: 15, percentage_op_sav: 0 },
non_precise: { nbr_op_sav: 15, percentage_op_sav: 0 },
panne_audio: { nbr_op_sav: 68, percentage_op_sav: 0 },
piece_aspect_mecanique: { nbr_op_sav: 78, percentage_op_sav: 0 },
probleme_charge: { nbr_op_sav: 2, percentage_op_sav: 0 },
}}
);
} catch (error) {
console.log("Error spreading");
console.log(error);
}
By "it should work", I mean that you should be able to print the object (that is actually spread into a new object).
You can try:
const myData = {<the data>}
console.log(...Object.entries(myData).flat());
Object.entries converts each key-value pair into arrays within an array.
flat() spreads each array into the main array. ...Object destructure the final array.