Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating nested object without entity ids to denormalize #2065

Closed
disc7 opened this issue Oct 29, 2016 · 2 comments
Closed

Updating nested object without entity ids to denormalize #2065

disc7 opened this issue Oct 29, 2016 · 2 comments

Comments

@disc7
Copy link

disc7 commented Oct 29, 2016

Hi

There are no shortage of examples (2, 3) of how to handle updating nested structures in Redux, with normalizr:

I have a case where i have a small app with no plans to scale but I don't have any control over the data being loaded in, so there are no entity ids. Here's a simple example that doesn't trigger a state mutation error:

customer: {
    address: {
        addressLines: [''],
        postcode: '',
        city: ''
    }
}

This is in response to data coming back from a form input:

const field = event.target.name;
let {customer} = this.state;
let objectToUpdate = null;
let value = event.target.value;

switch(field) {
    case 'postcode':

        objectToUpdate =  {
          address: {
            addressLines: customer.address.addressLines,
            postcode: value,
            city: customer.address.city
          }
        };
        customer = Object.assign({}, customer, objectToUpdate);

    default :
        customer[field] = value;
}
return this.setState({customer: customer});

This example does trigger a state mutation error:

const field = event.target.name;
let {customer} = this.state;
let objectToClone = null;
let value = event.target.value;

switch(field) {
    case 'postcode':

        objectToClone = Object.assign({}, customer);
        objectToClone.address.postcode = value;
        customer = Object.assign({}, customer, objectToClone);

    default :
        customer[field] = value;
}
return this.setState({customer: customer});

Both examples are not easy to maintain, but given the top example passes immutability is it a valid solution?

And using a spread operator is it possible to write this in a less verbose way?

Thank you

@markerikson
Copy link
Contributor

I'm afraid I'm not really clear what you're asking. Also, this looks like a usage question, rather than a bug report or feature request. You're probably better off asking this on Stack Overflow instead.

@disc7
Copy link
Author

disc7 commented Oct 30, 2016

Posted at Stack Overflow for anyone following. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants