V
V
Vadim Stepanenko2021-10-26 13:37:45
React
Vadim Stepanenko, 2021-10-26 13:37:45

Did I put a spoke in my wheel by switching to reselect?

Hey! there is an application on react + redux.
store is organized as follows:

spoiler

const store = {
     dictionary: {
          1: {
               id: 1,
               title: 'post 1',
               ...
          },
          5: {
               id: 5,
               title: 'post 5,
               ...
          },
     },
     blog: [1, 5, ...],  // id постов которые отображаются в блоге (берутся из dictionary)
     informer: [1, ....] // id постов которые отображаются в информере (берутся из dictionary)
}


То есть имеется ассоциативный "словарь" постов, в котором хранятся все посты. И для каждого раздела, который использует посты есть массив с id постов


For example, I used to get blog posts like this:
// В компоненте
const section = useSelector((state) => state.router.section); 'blog' или 'informer'
const dictionary = useSelector((state) => state.posts.dictionary);
const posts = useSelector((state) => state.dictionary[section]);
....
posts.map((post) => (<Post key={post.id} {...dictionary[post.id]} />);


then I connected reselect and did it like this:
// В компоненте
const posts = useSelector(selectPosts);
...
posts.map((post) => (<Post key={post.id} {...dictionary[post.id]} />);

// selectActiveLooksList.js
export const selectSection = (state) => state.router.section;

export const selectDictionary = (state) => state.posts.dictionary;

export const selectPosts = createSelector(
  [selectSection, selectDictionary],
  (section, dictionary) => dictionary[section]
);


Why does it seem to me that I have put spokes in my wheels?
createSelector caches the result, which I don’t really need (calculations like .map(), .filter(), etc. are not used to get posts, but the dictionary[section] array is simply accessed, and with each section change, the result will be new cached

Which implementation is more acceptable (first or second)?

Answer the question

In order to leave comments, you need to log in

6 answer(s)
T
twolegs, 2021-10-26
@twolegs

In your situation, reselect is redundant - you don't need to memoize selector results. This is not a spoke in the wheels of course, but at the moment it is absolutely superfluous.

F
Frontend developer, 2021-10-26
@markak

In your case, no calculations are made in the selector, and, accordingly, performance is not improved. You're just caching what's already in the dictionary. Hence there is a memory leak.

I
Ivan Cherny, 2015-01-15
@Makswell134

netbins-denver connection is like a grape-bicycle)
after it stopped working pkhp? Is it just this script that doesn't work? what os? what server is deployed?

O
Optimus, 2015-01-15
Pyan @marrk2

No php support on hosting, prohibited in htaccess

L
lnked, 2015-01-15
@lnked

Download denwer , very easy to set up!
run your file through denwer (or any other webserver)

S
Silm, 2015-01-15
@Silm

What is the url in the address bar?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question