0
votes

I use Redux-form change function to change the state of the form. if this attribute exists, it will append the value directly to the state, if it doesn't exist, it will add this attribute with the value to the state. Is that possible to delete this key-value pair in the state?

if (language === 'English') {
  change('languages', 'English');
} else if (typeof language === 'Unknown') {
 *** delete languages: English from the state ***
}

var language = 'English'
expected result: { languages: English }

var language = 'Unknow'
expected result: {}
You can use spread to do so without mutation: const { languages, ...newState } = stateVariableHere then newState will not have the property. But I would recommend against this: you want state to have a consistent shape. So nullify languages instead of removing. - Andrew Li
Agree, why not just nullify languages field or set it to undefined? - faithfull