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.

Using entity adapters inside reducers in 1.5.0

See original GitHub issue

Hi, would be thankful for a little help here. From the changelog of 1.5.0 I understood that we who use our entity adapters inside reducers are covered by:

We’ve also updated createEntityAdapter’s getSelectors API to use these draft-safe selectors.

So lets define an adapter:

const tabsAdapter = createEntityAdapter<Tab>();
export const tabsSelectors = tabsAdapter.getSelectors<State>((state) => state.tabs);
export const { selectById: selectTabById } = tabsSelectors;

Then use it in some reducer:

    setSomething(state, action: PayloadAction<{ tabId: string; foo: string }>) {
      const { tabId, foo } = action.payload;
      const tab = selectTabById(state, tabId);
      if (tab) {
        tab.foo = foo;
      }
    },

Works in 1.4.0, but fails in 1.5.0 during runtime with:

TypeError: Cannot assign to read only property 'foo' of object '#<Object>'

or this one if the property was not yet set

TypeError: Cannot add property foo, object is not extensible

Could someone please explain what is the migration path here from 1.4.0 -> 1.5.0?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
smajlcommented, Feb 16, 2021

@markerikson Correct, not calling getSelectors() everytime, that was just an example, in reality we are using stored and correctly typed selector variants (because our entity adapter has an union type). Thanks for the reply, will get rid of the these selectors inside reducers and hopefully finally fully migrate to 1.5.0. 😄

1reaction
smajlcommented, Dec 10, 2020

@phryneas I see, so I handled updates the unsafe way from the very beginning. 😇 Thanks a lot for the clarification and super fast response! The update*/upsert* methods are what I needed!

Read more comments on GitHub >

github_iconTop Results From Across the Web

createEntityAdapter - Redux Toolkit
A function that generates a set of prebuilt reducers and selectors for performing CRUD operations on a normalized state structure containing ...
Read more >
22. Use Entity Adapter - Angular and NgRx - GitBook
1. Reduces boilerplate for creating reducers that manage a collection of models. · 2. Provides performant CRUD operations for managing entity collections. ·...
Read more >
More advanced access patterns for createEntityAdapter ? #799
I'm using createEntityAdapter to keep my data normalised and it's working very nicely, however, my use-case means not only do I need to...
Read more >
Angular NgRx Entity - Complete Practical Guide
In order to be able to use the other features of NgRx Entity, we need to first create an entity adapter. The adapter...
Read more >
reduxjs/toolkit - NPM Package Overview - Socket.dev
createEntityAdapter () : generates a set of reusable reducers and selectors to manage normalized data in the store; The createSelector() utility ...
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