Best way to use Enumify enums in the store #2055
Unanswered
woodcoder
asked this question in
Help and Questions
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I've used Enumify successfully in the past to store state in vue applications written in JavaScript. As well as the enums themselves, it's very useful to be able to augment the constants with other methods / properties.
Moving to pinia, I've found the (perhaps predictable!) problems of storing Enumify enums in the state is that they:
Neither of which play well with being able to compare (===) or use the enum values as enums!
I wondered if anyone had any recommendations as to the most elegant way to handle enums like this in pinia. Some things I've tried are:
a. using
toRaw
to unwrap the Proxy before comparing - this, surprisingly, works sometimes - however, on other occasions I get a plain object, rather than the enumb. adding a getter to coerce the state to the enum, using
MyEnum.enumValueOf(state.myEnum.enumKey)
- which works, but is asymmetric with setting which needs to be done to a different property namec. and I'm now considering just using the string
.enumKey
or integer.enumOrdinal
as the state (with, perhaps optionally a getter and maybe an action to provide conversion, although again this feels horribly asymmetric)Is there a better way to do this that I've overlooked?
Beta Was this translation helpful? Give feedback.
All reactions