question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Use selectFromResult with normalized data

See original GitHub issue

I suspect the answer is “no”, but I would like your confirmation: it does make sense to use selectFromResult with normalized data?

Like:

export const commentaryAdapter = createEntityAdapter({
  // Assume IDs are stored in a field other than `book.id`
  selectId: commentary => commentary.attributes.id,
});

 const { selectAll: selectAllCommentaries, selectById: selectCommentaryById } =
    commentaryAdapter.getSelectors(state => {
      return selectCommentaryEvents(state);
    });

  const commentaries = useSelector(state => selectAllCommentaries(state));

const { commentaryEvents } =
    useGetMatchResultQuery(
      {
        matchId: matchId,
      },
      {
        selectFromResult: result => {
          return {
            ...result,
            commentaryEvents: commentaries,
          };
        },
      },
    );

Or, if you use normalized data, selectFromResult doesn’t make sense?

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:9

github_iconTop GitHub Comments

1reaction
markeriksoncommented, Mar 22, 2022

Yeah, so what I was saying applies here.

If you do nothing, by default const { data } = useMyQuery() will give you back the entire {ids: [], entities: {} structure you returned from transformResponse.

If you only want part of that, like a single item by its ID, then you should use selectFromResult to pick out just that one piece.

0reactions
markeriksoncommented, Mar 22, 2022

You’d want to use the entity adapter selectors. Specifically, you’d need to use the “localized” versions, generated by calling myAdapter.getSelectors() without an input selector:

https://redux-toolkit.js.org/api/createEntityAdapter#selector-functions

And then const allItemsAsArray = selectors.selectAll(res.data) in transformResponse.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Redux Essentials, Part 8: RTK Query Advanced Patterns
The official Redux Essentials tutorial: learn advanced patterns for fetching data with RTK Query.
Read more >
ReduxToolKit: correct way to use SelectFromResult options in ...
If I comment out the SelectFromResult option they are then correctly returned. export const PokemonList = () => { const { data, error,...
Read more >
Connecting RTK Query API with redux reducer and selector ...
[Solved]-Connecting RTK Query API with redux reducer and selector-Reactjs ... You missed an important bit: RTK-Query is not a normalized cache, but a...
Read more >
Redux Toolkit 1.6.0 - new RTK Query data caching API! - Reddit
RTK Query is a powerful and easy-to-use server data caching solution ... RTK Query is not a normalized store and you do not...
Read more >
Normalized Caching | urql Documentation
For our apps normalized caches can enable more sophisticated use-cases, where different API requests update data in other parts of the app and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found