Reselect - does it make sense to create a remembered selector that is simply used to get part of the state?

I have a regular selector that is simply used to get part of the state:

export const getAllPosts = state => {
  return state.posts;
};

If I use reselect to wrap the selector:

import { createSelector } from 'reselect';

export const allPosts = createSelector(
  getAllPosts,
  (posts) => posts
);

Does this make any sense, such as improving performance? In my opinion, a shell is not needed.

+4
source share
3 answers

No, it makes no sense to create a memoized selector to get part of the state tree.

, connect , mapStateToProps. , , , render . , .

, , createSelector . -, . -, , . -, , , , connect, . memoization, createSelector, , , , , .

, createSelector .

, createSelector, . , , .

+1

, .

, , - , , , :

// assume state is:  {first : {second {} } }

const selectFirst = state => state.first;

const selectSecond = createSelector(
    selectFirst,
    first => first.second
);
0

, , ( ), .

, , , , , , :

export const selectAllPosts = createSelector(
  getAllPostsFromStore, // returns { 1: {...}, 2: {...} }
  (allPosts) => Object.keys(allPosts).map(key => allPosts[key])
);

, , - .

, : , .

0

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


All Articles