Looking for advice on updating multiple fields in an ImmutableJS Record efficiently.
I attempted to use withMutations, but encountered an issue where the copy I created was empty and could not be mutated:
const UserRecord = Record({
firstName: null,
lastName: null,
address: {
city: null,
zipCode: null
}
});
export function createUserRecord(data) {
return new UserRecord(fromJS(data));
}
let u = createUserRecord({
firstName: 'John',
lastName: 'Don',
address: {
city: 'New York',
zipCode: 1111
}
});
let newU = u.withMutations((uCopy) => {
// uCopy is empty (with default values)
uCopy.set('firstName', 'New FirstName');
uCopy.set('lastName', 'New LastName');
// uCopy is still empty (not mutated)
});
console.log(u.toJS()); // {firstName: "John", lastName: "Don", address: {city: null, zipCode: null}}
console.log(newU.toJS()); // {firstName: null, lastName: null, address: {city: null, zipCode: null}}
Appreciate any help!