I am using reselect lib in my React project.
I've created Posts
selector which works fine. Here's the code
// selectors.ts
const getPosts = (state: RootState) => state.posts.posts;
export const getPostsSelector = createSelector(getPosts, (posts) => posts);
And I call it on my page like that
// SomePage.tsx
const posts = useSelector(getPostsSelector);
Now I need to get a post by id. I thought to do it like that:
// selectors.ts
const getPostDetail = (state: RootState) =>
state.posts.posts.entities[ID];
export const getPostsById = createSelector(
getPostDetail,
(detail) => detail
);
And call it on the page:
const PostDetail = useSelector(getPostsById);
I have two questions:
- Is it right approach to get single post?
- If it is, how to pass ID of the post if not, how to handle this the right way?
postDetailMap
call on each render? – Flue