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)?
Hassle-free blogging platform that developers and teams love.
  • Docs by Hashnode
    New
  • Blogs
  • AI Markdown Editor
  • GraphQL APIs
  • Open source Starter-kit

© Hashnode 2024 — LinearBytes Inc.

Privacy PolicyTermsCode of Conduct