Search code examples
javascriptfluxredux

Redux vs. Traditional MVC


In regular MVC, it's a common pattern to have "getter" methods on a model that compute commonly derived data. For example:

class User {
   var firstName;
   var lastName;

   getFullName() {
     return firstName + " " + lastName;
   }
}

Let's assume the server JSON response is {'firstName': "Bob", 'lastName': "Smith"}.

In regular Flux, you could place such a method (getFullName()) on your store (for example, Alt solves this by allowing you to use an "exportPublicMethods" on your store).

In Redux, I'm not sure where that "getFullName()" method would live, considering it's encouraged to use regular javascript objects inside your states (i.e. in my example, only firstName and lastName would be stored, with no easy way to generate the derived full name).

What would be the best / easiest way to accomplish this?

Thanks!


Solution

  • In the documentation, a concept called a "selector" is used to map raw API data to the representation desired in the view. This is specifically for use with react-redux, but you could certainly use the same technique with any given view layer.