Sign in
Log inSign up

How do you turn an object in Redux state into a class/model?

Deactivated User's photo
Deactivated User
·Oct 10, 2016

Let's say that I have a data object with many fields in a Redux state:

var doc = {
    id: 1111,
    revision: 5,
    title: 'Lorem Ipsum',
    items: [{...}, {...}]
    ...
}

This object (model) has a lot derived properties. For example:

function reference(doc) {
    return doc.id + '-' + doc.revision;
}

// Another example
function totalQuantity(doc) {
  return doc.items.reduce(function(total, item) {
      return total + item.quantity;
  }, 0);
}

What's a simple way to add/use such functions where a React component receives this plain data object as a prop? I just want something really simple, my app is already very complicated :|

  • Do you construct a model?
  • Do you use helpers docHelper.reference(doc)?
  • Do you use libraries?
  • Do you write state selectors (I'm still a bit unfamiliar with them, please explain)?