Why doesn't (Flux) state management use static functions instead of named properties
up vote
4
down vote
favorite
I'm currently deciding between switching to react.js or vue.js from angularJS . The goal is to use it including Typescript to make sure that everything is Typesafe.
With trying out vuex and redux I have noticed that they don't use normal functions but instead use some form of string naming for functions.
Redux for example checks via a switch in reducers for a certain function
Vuex uses constant objects where the functions in that object can be called via functions which take a string and a payload
What is the reason for this? Somehow this feels like it's going to be painful when you have to refactor and feels like a way to make mistakes while programming because of typo's.
Why wouldn't a normal class with static functions do the job(or another perhaps better solution)
reactjs vue.js redux vuex state-management
add a comment |
up vote
4
down vote
favorite
I'm currently deciding between switching to react.js or vue.js from angularJS . The goal is to use it including Typescript to make sure that everything is Typesafe.
With trying out vuex and redux I have noticed that they don't use normal functions but instead use some form of string naming for functions.
Redux for example checks via a switch in reducers for a certain function
Vuex uses constant objects where the functions in that object can be called via functions which take a string and a payload
What is the reason for this? Somehow this feels like it's going to be painful when you have to refactor and feels like a way to make mistakes while programming because of typo's.
Why wouldn't a normal class with static functions do the job(or another perhaps better solution)
reactjs vue.js redux vuex state-management
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
I'm currently deciding between switching to react.js or vue.js from angularJS . The goal is to use it including Typescript to make sure that everything is Typesafe.
With trying out vuex and redux I have noticed that they don't use normal functions but instead use some form of string naming for functions.
Redux for example checks via a switch in reducers for a certain function
Vuex uses constant objects where the functions in that object can be called via functions which take a string and a payload
What is the reason for this? Somehow this feels like it's going to be painful when you have to refactor and feels like a way to make mistakes while programming because of typo's.
Why wouldn't a normal class with static functions do the job(or another perhaps better solution)
reactjs vue.js redux vuex state-management
I'm currently deciding between switching to react.js or vue.js from angularJS . The goal is to use it including Typescript to make sure that everything is Typesafe.
With trying out vuex and redux I have noticed that they don't use normal functions but instead use some form of string naming for functions.
Redux for example checks via a switch in reducers for a certain function
Vuex uses constant objects where the functions in that object can be called via functions which take a string and a payload
What is the reason for this? Somehow this feels like it's going to be painful when you have to refactor and feels like a way to make mistakes while programming because of typo's.
Why wouldn't a normal class with static functions do the job(or another perhaps better solution)
reactjs vue.js redux vuex state-management
reactjs vue.js redux vuex state-management
edited Nov 8 at 17:19
asked Nov 8 at 16:37
redlum
443
443
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
2
down vote
Both Redux and Vuex create a single store
instance from declared set of modules, that is responsible for dispatching/committing registered actions/mutations that require type to be provided as a string
value. While scaling the application, usage of actions/mutations becomes quite widespread and using plain strings will reduce state mutation predictability significantly.
Using constants, such as:
export const ADD_PORTFOLIO = 'ADD_PORTFOLIO'
allows to list all available state mutations in a separate list-like file, thus providing clear understanding of possible changes, while avoiding typos inside store modules and allowing IDE and code editors to autocomplete or highlight invalid references to constant types. Which reduces debugging time and would not be possible with plain string
s. It is not required, neither it is an invention of mentioned libraries. It works well with globally invoked events (e.g. Node EvenEmitter, Electron ipcRenderer, etc) for the same reasons.
Both Redux and Vuex store state in plain objects and require their API to be defined as a functions (each with it's own architecture), that will be merged into a single store instance. This serves purpose of providing reactive updates on state changes, warnings when state is mutated outside of mutations or dispatched action/mutation is not registered. My opinion is that this approach is efficient in absence of type safety, which both libraries are initially meant to be used without.
For example, in Vuex if you decide to call an action or mutation from another module, you would use dispatch
(for actions) or commit
(for mutations) with a type referenced as full path to the module:
dispatch('profile/addPortfolio', payload, root: true )
commit('profile/ADD_PORTFOLIO', payload, root: true )
Meaning, that both invocations should be done with store
instance methods and are expected to be registered on current store
instance, rather than importing and invoking module's action or mutation directly.
In case Typescript is integrated into the project, it is possible to use class-based structure or decorators for the store and it's modules. Again, classes will be compiled to match store library architecture, but enhances development experience.
As for Vuex, following libraries are worth checking out for using with Typescript:
- vue-property-decorator
- vuex-module-decorators
vuex-class - a helper to usevuex
decorators inside components
TLDR
Such architecture is defined by mentioned libraries for usage without type-safety, while still allowing reactivity and centralized state mutation. Constants help to ensure that actions/mutations are registered on store instance, increase predictability and provide autocomplete for code editors.
Hi MrBiscuit, Thanks a lot for the extensive explanation. What i dont yet understand thought is the fact that it must be an object with functions in it and reference to it through the function names as strings. Do you know what the plus side of it it or the reason to design it like this?
– redlum
Nov 8 at 20:15
1
Looking at source code of dispatch methods Redux, Vuex - not only do they validate and dispatch actions, but also notify all subscribers of state changes. Which is a Observer pattern. Also, such approach allows to createnamespaces
for actions/mutations, while keeping it all on a singlestore
instance.
– aBiscuit
Nov 8 at 22:08
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Both Redux and Vuex create a single store
instance from declared set of modules, that is responsible for dispatching/committing registered actions/mutations that require type to be provided as a string
value. While scaling the application, usage of actions/mutations becomes quite widespread and using plain strings will reduce state mutation predictability significantly.
Using constants, such as:
export const ADD_PORTFOLIO = 'ADD_PORTFOLIO'
allows to list all available state mutations in a separate list-like file, thus providing clear understanding of possible changes, while avoiding typos inside store modules and allowing IDE and code editors to autocomplete or highlight invalid references to constant types. Which reduces debugging time and would not be possible with plain string
s. It is not required, neither it is an invention of mentioned libraries. It works well with globally invoked events (e.g. Node EvenEmitter, Electron ipcRenderer, etc) for the same reasons.
Both Redux and Vuex store state in plain objects and require their API to be defined as a functions (each with it's own architecture), that will be merged into a single store instance. This serves purpose of providing reactive updates on state changes, warnings when state is mutated outside of mutations or dispatched action/mutation is not registered. My opinion is that this approach is efficient in absence of type safety, which both libraries are initially meant to be used without.
For example, in Vuex if you decide to call an action or mutation from another module, you would use dispatch
(for actions) or commit
(for mutations) with a type referenced as full path to the module:
dispatch('profile/addPortfolio', payload, root: true )
commit('profile/ADD_PORTFOLIO', payload, root: true )
Meaning, that both invocations should be done with store
instance methods and are expected to be registered on current store
instance, rather than importing and invoking module's action or mutation directly.
In case Typescript is integrated into the project, it is possible to use class-based structure or decorators for the store and it's modules. Again, classes will be compiled to match store library architecture, but enhances development experience.
As for Vuex, following libraries are worth checking out for using with Typescript:
- vue-property-decorator
- vuex-module-decorators
vuex-class - a helper to usevuex
decorators inside components
TLDR
Such architecture is defined by mentioned libraries for usage without type-safety, while still allowing reactivity and centralized state mutation. Constants help to ensure that actions/mutations are registered on store instance, increase predictability and provide autocomplete for code editors.
Hi MrBiscuit, Thanks a lot for the extensive explanation. What i dont yet understand thought is the fact that it must be an object with functions in it and reference to it through the function names as strings. Do you know what the plus side of it it or the reason to design it like this?
– redlum
Nov 8 at 20:15
1
Looking at source code of dispatch methods Redux, Vuex - not only do they validate and dispatch actions, but also notify all subscribers of state changes. Which is a Observer pattern. Also, such approach allows to createnamespaces
for actions/mutations, while keeping it all on a singlestore
instance.
– aBiscuit
Nov 8 at 22:08
add a comment |
up vote
2
down vote
Both Redux and Vuex create a single store
instance from declared set of modules, that is responsible for dispatching/committing registered actions/mutations that require type to be provided as a string
value. While scaling the application, usage of actions/mutations becomes quite widespread and using plain strings will reduce state mutation predictability significantly.
Using constants, such as:
export const ADD_PORTFOLIO = 'ADD_PORTFOLIO'
allows to list all available state mutations in a separate list-like file, thus providing clear understanding of possible changes, while avoiding typos inside store modules and allowing IDE and code editors to autocomplete or highlight invalid references to constant types. Which reduces debugging time and would not be possible with plain string
s. It is not required, neither it is an invention of mentioned libraries. It works well with globally invoked events (e.g. Node EvenEmitter, Electron ipcRenderer, etc) for the same reasons.
Both Redux and Vuex store state in plain objects and require their API to be defined as a functions (each with it's own architecture), that will be merged into a single store instance. This serves purpose of providing reactive updates on state changes, warnings when state is mutated outside of mutations or dispatched action/mutation is not registered. My opinion is that this approach is efficient in absence of type safety, which both libraries are initially meant to be used without.
For example, in Vuex if you decide to call an action or mutation from another module, you would use dispatch
(for actions) or commit
(for mutations) with a type referenced as full path to the module:
dispatch('profile/addPortfolio', payload, root: true )
commit('profile/ADD_PORTFOLIO', payload, root: true )
Meaning, that both invocations should be done with store
instance methods and are expected to be registered on current store
instance, rather than importing and invoking module's action or mutation directly.
In case Typescript is integrated into the project, it is possible to use class-based structure or decorators for the store and it's modules. Again, classes will be compiled to match store library architecture, but enhances development experience.
As for Vuex, following libraries are worth checking out for using with Typescript:
- vue-property-decorator
- vuex-module-decorators
vuex-class - a helper to usevuex
decorators inside components
TLDR
Such architecture is defined by mentioned libraries for usage without type-safety, while still allowing reactivity and centralized state mutation. Constants help to ensure that actions/mutations are registered on store instance, increase predictability and provide autocomplete for code editors.
Hi MrBiscuit, Thanks a lot for the extensive explanation. What i dont yet understand thought is the fact that it must be an object with functions in it and reference to it through the function names as strings. Do you know what the plus side of it it or the reason to design it like this?
– redlum
Nov 8 at 20:15
1
Looking at source code of dispatch methods Redux, Vuex - not only do they validate and dispatch actions, but also notify all subscribers of state changes. Which is a Observer pattern. Also, such approach allows to createnamespaces
for actions/mutations, while keeping it all on a singlestore
instance.
– aBiscuit
Nov 8 at 22:08
add a comment |
up vote
2
down vote
up vote
2
down vote
Both Redux and Vuex create a single store
instance from declared set of modules, that is responsible for dispatching/committing registered actions/mutations that require type to be provided as a string
value. While scaling the application, usage of actions/mutations becomes quite widespread and using plain strings will reduce state mutation predictability significantly.
Using constants, such as:
export const ADD_PORTFOLIO = 'ADD_PORTFOLIO'
allows to list all available state mutations in a separate list-like file, thus providing clear understanding of possible changes, while avoiding typos inside store modules and allowing IDE and code editors to autocomplete or highlight invalid references to constant types. Which reduces debugging time and would not be possible with plain string
s. It is not required, neither it is an invention of mentioned libraries. It works well with globally invoked events (e.g. Node EvenEmitter, Electron ipcRenderer, etc) for the same reasons.
Both Redux and Vuex store state in plain objects and require their API to be defined as a functions (each with it's own architecture), that will be merged into a single store instance. This serves purpose of providing reactive updates on state changes, warnings when state is mutated outside of mutations or dispatched action/mutation is not registered. My opinion is that this approach is efficient in absence of type safety, which both libraries are initially meant to be used without.
For example, in Vuex if you decide to call an action or mutation from another module, you would use dispatch
(for actions) or commit
(for mutations) with a type referenced as full path to the module:
dispatch('profile/addPortfolio', payload, root: true )
commit('profile/ADD_PORTFOLIO', payload, root: true )
Meaning, that both invocations should be done with store
instance methods and are expected to be registered on current store
instance, rather than importing and invoking module's action or mutation directly.
In case Typescript is integrated into the project, it is possible to use class-based structure or decorators for the store and it's modules. Again, classes will be compiled to match store library architecture, but enhances development experience.
As for Vuex, following libraries are worth checking out for using with Typescript:
- vue-property-decorator
- vuex-module-decorators
vuex-class - a helper to usevuex
decorators inside components
TLDR
Such architecture is defined by mentioned libraries for usage without type-safety, while still allowing reactivity and centralized state mutation. Constants help to ensure that actions/mutations are registered on store instance, increase predictability and provide autocomplete for code editors.
Both Redux and Vuex create a single store
instance from declared set of modules, that is responsible for dispatching/committing registered actions/mutations that require type to be provided as a string
value. While scaling the application, usage of actions/mutations becomes quite widespread and using plain strings will reduce state mutation predictability significantly.
Using constants, such as:
export const ADD_PORTFOLIO = 'ADD_PORTFOLIO'
allows to list all available state mutations in a separate list-like file, thus providing clear understanding of possible changes, while avoiding typos inside store modules and allowing IDE and code editors to autocomplete or highlight invalid references to constant types. Which reduces debugging time and would not be possible with plain string
s. It is not required, neither it is an invention of mentioned libraries. It works well with globally invoked events (e.g. Node EvenEmitter, Electron ipcRenderer, etc) for the same reasons.
Both Redux and Vuex store state in plain objects and require their API to be defined as a functions (each with it's own architecture), that will be merged into a single store instance. This serves purpose of providing reactive updates on state changes, warnings when state is mutated outside of mutations or dispatched action/mutation is not registered. My opinion is that this approach is efficient in absence of type safety, which both libraries are initially meant to be used without.
For example, in Vuex if you decide to call an action or mutation from another module, you would use dispatch
(for actions) or commit
(for mutations) with a type referenced as full path to the module:
dispatch('profile/addPortfolio', payload, root: true )
commit('profile/ADD_PORTFOLIO', payload, root: true )
Meaning, that both invocations should be done with store
instance methods and are expected to be registered on current store
instance, rather than importing and invoking module's action or mutation directly.
In case Typescript is integrated into the project, it is possible to use class-based structure or decorators for the store and it's modules. Again, classes will be compiled to match store library architecture, but enhances development experience.
As for Vuex, following libraries are worth checking out for using with Typescript:
- vue-property-decorator
- vuex-module-decorators
vuex-class - a helper to usevuex
decorators inside components
TLDR
Such architecture is defined by mentioned libraries for usage without type-safety, while still allowing reactivity and centralized state mutation. Constants help to ensure that actions/mutations are registered on store instance, increase predictability and provide autocomplete for code editors.
edited Nov 8 at 18:11
answered Nov 8 at 18:05
aBiscuit
1,2421513
1,2421513
Hi MrBiscuit, Thanks a lot for the extensive explanation. What i dont yet understand thought is the fact that it must be an object with functions in it and reference to it through the function names as strings. Do you know what the plus side of it it or the reason to design it like this?
– redlum
Nov 8 at 20:15
1
Looking at source code of dispatch methods Redux, Vuex - not only do they validate and dispatch actions, but also notify all subscribers of state changes. Which is a Observer pattern. Also, such approach allows to createnamespaces
for actions/mutations, while keeping it all on a singlestore
instance.
– aBiscuit
Nov 8 at 22:08
add a comment |
Hi MrBiscuit, Thanks a lot for the extensive explanation. What i dont yet understand thought is the fact that it must be an object with functions in it and reference to it through the function names as strings. Do you know what the plus side of it it or the reason to design it like this?
– redlum
Nov 8 at 20:15
1
Looking at source code of dispatch methods Redux, Vuex - not only do they validate and dispatch actions, but also notify all subscribers of state changes. Which is a Observer pattern. Also, such approach allows to createnamespaces
for actions/mutations, while keeping it all on a singlestore
instance.
– aBiscuit
Nov 8 at 22:08
Hi MrBiscuit, Thanks a lot for the extensive explanation. What i dont yet understand thought is the fact that it must be an object with functions in it and reference to it through the function names as strings. Do you know what the plus side of it it or the reason to design it like this?
– redlum
Nov 8 at 20:15
Hi MrBiscuit, Thanks a lot for the extensive explanation. What i dont yet understand thought is the fact that it must be an object with functions in it and reference to it through the function names as strings. Do you know what the plus side of it it or the reason to design it like this?
– redlum
Nov 8 at 20:15
1
1
Looking at source code of dispatch methods Redux, Vuex - not only do they validate and dispatch actions, but also notify all subscribers of state changes. Which is a Observer pattern. Also, such approach allows to create
namespaces
for actions/mutations, while keeping it all on a single store
instance.– aBiscuit
Nov 8 at 22:08
Looking at source code of dispatch methods Redux, Vuex - not only do they validate and dispatch actions, but also notify all subscribers of state changes. Which is a Observer pattern. Also, such approach allows to create
namespaces
for actions/mutations, while keeping it all on a single store
instance.– aBiscuit
Nov 8 at 22:08
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53212227%2fwhy-doesnt-flux-state-management-use-static-functions-instead-of-named-proper%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown