Replies: 1 comment 1 reply
-
He could just
which is an example straight from the Hooks documentation and as such perfectly fine
A selector selects from the store. Perfectly fine to call it that.
This is syntactic sugar that you can use but technically has no difference. Banana, banana, potato, potato. You decide what you prefer. There is no technical reason to prefer one over the other
Logic in selectors is perfectly fine, the point of selectors is to be able to compute derived data |
Beta Was this translation helpful? Give feedback.
-
Hi,
I have a question about Redux toolkit.
In our team, we have a discussion about how we can create a selector function in Redux Toolkit together with Slice creation.
One of our Slice (approximately) looks like below:
One of our colleague-A adds a new selector function like below in the slice:
export const selectProjectMemberById = (state, id) => selectById(state, id);
Then he uses that in a react component like below:
So, the question is:
Do you see any problem of creating a selector like the above way in the slice and then consuming it in the component like the above way?
One of the colleague (colleague-A) is augmenting that
selectProjectMemberById
actually is not a selector as it doesn't followexport type Selector<S, R> = (state: S) => R;
structure. It followstype NewSelector<S, R> = (state: S, id: Any) => R;
structure and thereforeselectProjectMemberById
is not actually a selector.Other colleague (colleague-B) is augmenting that
selectProjectMemberById
is also a selector as Redux toolkit has also a selector which isselectById
and it followstype NewSelector<S, R> = (state: S, id: Any) => R;
structure.Then colleague-A is augmenting that type structure
type NewSelector<S, R> = (state: S, id: Any) => R;
(selectById) is an entity selector but not an real selector and therefore it is not ok to say it a selector.Colleague-A, is also saying that it is not good to use useSelector like the below way:
const getSelectedProjectMember = useSelector(state => selectById(state, selectedProjectMemberName));
It should be used like the below way:
useSelector(selectProjectMemberById(selectedProjectMemberName));
If it is not used this [[[[useSelector(selectProjectMemberById(selectedProjectMemberName));]]] way then one can write more logic like below: (Colleague-A is saying it is not good to write more logic inside the useSelector.)
Therefore, I am thinking to hear how community is thinking about this discussion. Any feedback will be greatly appreciated.
Beta Was this translation helpful? Give feedback.
All reactions