According to the documentation the by id selector has the following signature: selectById: (state: V, id: EntityId) => T | undefined
.
So you can call it in your component in the following way:
const Component = ({ id }) => {
const item = useSelector((state) =>
selectUserById(state, id)
);
};
This implementation of "normalization" may not work if you sort/filter entities on the server because the state would look more like:
{
data: {
entityType: {
//query is key and value is status and result
// of the api request
'sort=name&page=1': {
loading: false,
requested: true,
error: false,
stale: false,
ids: [1, 2],
},
'sort=name:desc&page=1': {
//would have loading, requested ....
ids: [2, 1],
},
data: {
//all the data (fetched so far)
'1': { id: 1 },
'2': { id: 2 },
},
},
},
};
I have not worked with the "helpers" so have to look into it as it may facilitate for server side filtering and sorting.
I also doubt it will memoize the selector:
const List = ({ items }) => (
<ul>
{items.map(({ id }) => (
<Item key={id} id={id} />
))}
</ul>
);
const Item = React.memo(function Item({ id }) {
//selectUserById is called with different id during
// a render and nothing will be memoized
const item = useSelector((state) =>
selectUserById(state, id)
);
});
I have created a short documentation on how you can use selectors created with reselect.