Where to handle computed properties using Redux?

when using Redux, the store should be the only source of truth and not have redundancy. Suppose that part of the store represents people who have a name and age. A human class in traditional object-oriented programming might look something like this:

class Person {
    constructor(first, last, birthday) {
        this.first = first;
        this.last = last;
        this.birthday = birthday;
    get_fullname() { // ... //}
    get_age() { // ... //}
}

However, methods are not allowed on objects in the Redux repository. So where should these "methods" be implemented?

+4
source share
2 answers

There are two ways:

  • In gearboxes.

    , " ". , ( , , )

  • .

    - , . . . redux .

    , , , , .

+2

connect .

function getFullName(state) {
  return `${state.first} ${state.last}`;
}

function mapStateToProps(state) {
  return {
    fullName: getFullName(state)
  };
}

connect(mapStateToProps)(MyComponent);

Reselect, , Redux.

, , , , , , .

, , .

+3

Source: https://habr.com/ru/post/1660634/


All Articles