Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Maps/Sets can't be viewed in devtools #632

Closed
berwyn opened this issue Dec 5, 2017 · 9 comments
Closed

Maps/Sets can't be viewed in devtools #632

berwyn opened this issue Dec 5, 2017 · 9 comments

Comments

@berwyn
Copy link

berwyn commented Dec 5, 2017

I'm submitting a...


[ ] Regression (a behavior that used to work and stopped working in a new release)
[x] Bug report  
[x] Feature request
[ ] Documentation issue or request

What is the current behavior?

As noted in ngrx/store-devtools#64 and ngrx/store-devtools#67 using an ES2015 Set or Map works well enough in ngrx, but the devtools won't display the object instead showing an empty object ({ }) when it's first set, and omitting the object in diffs. Additionally, when looking at the graph of the store, the Set/Map will show up as a node, but have no associated value.

Expected behavior:

Devtools should show that a Set/Map was used, and that it was updated as a result of an action.

Minimal reproduction of the problem with instructions:

given:

StoreDevtoolsModule.instrument({ maxAge: 50 })

and

interface MyState {
    data: Map<string, Item>;
}

any reducer that updates this field will succeed, however this property will not be displayed correctly in the devtools, and will not exist in diffs.

Version of affected browser(s),operating system(s), npm, node and ngrx:

[email protected]
[email protected]
@ngrx/[email protected]
@ngrx/[email protected]
@ngrx/[email protected]

Other information:

Based on the other bugs, it seems that the devtools extension may have a serialize argument that may alleviate this, however passing this to ngrx doesn't seem to do anything different, so I'm unsure if there's work on the ngrx side to support this.

@brandonroberts
Copy link
Member

The serialize option is already being passed to the Devtools as false. The extension itself does not show Map/Set unless you provide a toJSON patch. See zalmoxisus/redux-devtools-extension#124

@berwyn
Copy link
Author

berwyn commented Dec 14, 2017

Thanks for the pointer in the right direction, Brandon!

Just in case anyone else wants a quick solution to this, core-js can solve this by polyfilling in the ES7 updates to Map, which includes a toJSON function:

// polyfills.ts
import 'core-js/es7/map';

@vincenzovitale
Copy link

Thanks @berwyn , you saved the day! :)

@cliffbroadbent
Copy link

@berwyn - Thank you very much! Appreciate you left a note on this ticket. 🥇

@Maks-Yaremenko
Copy link

@berwyn Thank you, buddy!

@MaciejCaputa
Copy link

It looks like the {Map, Set}#toJSON method has been rejected by TC39 and removed from core-js in v3.0.0.
Shouldn't the extension handle displaying maps and sets itself?

@Zarepheth
Copy link

A manually implemented poly fill may also work: Store-DevTools #64

if (environment.envName === 'dev') {
  (Map.prototype as any).toJSON = function () {
    return JSON.parse(JSON.stringify([...this]));
  };
}

@prewk
Copy link

prewk commented Feb 13, 2020

Here's a simple one for Sets:

  (Set.prototype as any).toJSON = function() {
    return Array.from(this);
  };

@jdavault
Copy link

jdavault commented Nov 1, 2023

import { legacy_createStore as createStore, applyMiddleware } from "redux";
import reducers, { initialState } from "./reducers";
import thunkMiddleware from "redux-thunk";
import { composeWithDevTools } from "@redux-devtools/extension";

const composeEnhancers = composeWithDevTools({ serialize: true });

export const store = createStore(
reducers,
initialState,
composeEnhancers(applyMiddleware(thunkMiddleware))
);

//WORKED FOR ME ....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants