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

devtools: accessing serialize option #249

Closed
JacobJaffe opened this issue Nov 29, 2020 · 3 comments · Fixed by #540
Closed

devtools: accessing serialize option #249

JacobJaffe opened this issue Nov 29, 2020 · 3 comments · Fixed by #540
Labels
enhancement New feature or request help wanted Please someone help on this

Comments

@JacobJaffe
Copy link

Hello! I've just started using zustand, previously having used primarily redux. Big fan so far!

In setting up the devtools, I'm having trouble tying to configure the devtools. Specifically, I'm trying to enable the serialize option, so that I can view a Map in the tools nicely.

Example of a store I'm trying to see with devtools:

const foo: Entity = {
  id: "abc",
  // ...
};

type State = {
  entities: Map<EntityId, Entity>;
};

const store = () => ({
  entities: new Map<EntityId, Entity>([[foo.id, foo]]),
});

const useStore = create<State>(devtools(store));

In a redux configuration I would do this with:

configureStore({
  // ...
  devTools: {
    serialize: {
      options: true
    }
  }
});

Is there a similar way to access the devtool options from zustand/middleware.devtools ?


For anyone also facing this, my current solution is an old hack from redux before the devTools.serialize option:

(Map.prototype as any).toJSON = function () {
  return JSON.parse(JSON.stringify([...this]));
};
@dai-shi
Copy link
Member

dai-shi commented Nov 29, 2020

Thanks for opening this up!
I don't think such an option is supported in the current zustand/middleware.devtools.

Anyone wants to investigate?

@dai-shi dai-shi added enhancement New feature or request help wanted Please someone help on this labels Nov 29, 2020
@cmlarsen
Copy link

This is kinda gross and not at all throughly tested, but to display Maps/Sets I used patch-package to patch the devtools middleware with polyfill for Map and Set

// zustand/middlware.js
var devtools = function devtools(fn, prefix) {
  if (process.env.NODE_ENV !== 'production') {
    if (!Map.prototype.toJSON) {
      Map.prototype.toJSON = function() {
        var obj = {};
        this.forEach((value, key) => obj[key] = value);
        return obj;
      }
    }
    if (!Set.prototype.toJSON) {
      Set.prototype.toJSON = function() {
        return Array.from(this);
      };
    }
  }
// ... the rest of the devtools middleware
}

@dai-shi
Copy link
Member

dai-shi commented Jan 1, 2021

That's a nice hack.
Though, I guess we could simply enable the serialize option and let the work done in the DevTools Extension?

marcoSven added a commit to marcoSven/zustand that referenced this issue Aug 19, 2021
## Why is this change needed?
Unable to display Maps/Sets

## How does it address the issue?
optional serialize date, regex, undefined, nan, infinity,
error, symbol, map & set

## breaking change prev "prefix" option
prefix?: string -> options?: { name?: string }

https://github.com/zalmoxisus/redux-devtools-extension/blob/master/docs/API/Arguments.md#serialize

pmndrs#249
@dai-shi dai-shi linked a pull request Aug 20, 2021 that will close this issue
dai-shi pushed a commit that referenced this issue Aug 26, 2021
* Allow optional REDUX DEVTOOLS "options" object

## Why is this change needed?
Unable to display Maps/Sets

## How does it address the issue?
optional serialize date, regex, undefined, nan, infinity,
error, symbol, map & set

## breaking change prev "prefix" option
prefix?: string -> options?: { name?: string }

https://github.com/zalmoxisus/redux-devtools-extension/blob/master/docs/API/Arguments.md#serialize

#249

* backward compatible with "prefix" store name option

* Update readme.md
SD11892 pushed a commit to SD11892/zustand that referenced this issue Mar 15, 2023
* Allow optional REDUX DEVTOOLS "options" object

## Why is this change needed?
Unable to display Maps/Sets

## How does it address the issue?
optional serialize date, regex, undefined, nan, infinity,
error, symbol, map & set

## breaking change prev "prefix" option
prefix?: string -> options?: { name?: string }

https://github.com/zalmoxisus/redux-devtools-extension/blob/master/docs/API/Arguments.md#serialize

pmndrs/zustand#249

* backward compatible with "prefix" store name option

* Update readme.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Please someone help on this
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants