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
fix(State): Return state as a function #4
Conversation
Hi @eunjae-lee, Could I get an update on this? Tnx. |
Hi @vanhoofmaarten ! |
That explains. 😅 |
Hi @vanhoofmaarten,
`build_config.ts` says `state` IS a Function (without explicit return type). So, back in `state_builder.ts`,
So, I'm not sure if I understand your problem. |
I indeed need to provide some extra clarification, as my initial proposal did not explain the issue I was having. The reason I made this PR is because I use this package with Nuxt.js. When you use a store in Nuxt, it's default implementation is build around modules. Nuxt requires you to define your state property as a function,, because they use the Model Reuse pattern described by the Vuex documentation. I've created a CodeSandbox with vuex-dry used within a Nuxt project. When you look at the console, you see that Nuxt throws warnings regarding not declaring the state as a function. That's the main thing I want to fix in this PR. Instead of executing the vuex-dry state function and returning the object within, vuex-dry/src/module/state_builder.ts Lines 3 to 5 in 9268283
I propose just to return the function to Vuex. This fixes the warning and enables Model Reuse. vuex-dry/src/module/state_builder.ts Lines 3 to 5 in 63ff2ed
I hope everything is more clear now. Thanks! |
Thanks for the thorough explanation. Since your change brings a breaking change, I took a little different approach. https://codesandbox.io/s/serverless-cdn-1i2pk I hope this works! (0.2.1) export default Module.build({
stateAsFunction: true,
state: () => ({
list: []
}),
...
}); |
Perfect! Thank you! |
If state is a function, it is already called in this library, when in fact the function should be called in Vuex itself.
I couldn't create a test to test this behaviour, but Typescript currently states that buildState returns a type of
<any>
, whereas thebuild_config.ts
states that state should return aFunction
. This PR fixes that behaviour.This change should be implemented because of the thorough explanation found here: https://stackoverflow.com/questions/49557177/vuex-state-returned-as-function-or-object-literal
I'm happy to hear your thoughts.