Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is using ES6 classes in vue/vuex(/flux?) an anti-pattern?

I've been using Vuex, and it's adherence to only altering state through it's mutators or actions makes me think your store should only include as flat an object as you can, with only primitives types.

Some threads even prescribe normalising your data (so instead of nested object trees you have objects with arrays of id's to indicate tree relationships). This probably matches closely to your JSON api.

This makes me think that storing classes (that may have methods to alter themselves) in your flux store is an anti-pattern. Indeed even hydrating your store's data into a class seems like you're moving against the tide unless your class performs no modifications to its internal data.

Which then got me thinking, is using any class in a Vue/Vuex/Reactive/Flux an anti-pattern?

The libraries seem explicitly designed to work with plain JS objects and the general interactions you have with the API (data in, data out) makes me feel like a more functional approach (sans immutability) is what the original designers were thinking about.

It also seems be easier to write code that runs from function => test => state mutator wrapper around function.

I understand that JS objects and JS classes behave very similarly (and are basically the same thing), but my logic is if you don't design with classes in mind, then you're more likely to not pollute your state with non-flux state changes.

Is there a general consensus in the community that your flux code should be more functional and less object orientated?

like image 506
purplelulu Avatar asked Jan 24 '19 11:01

purplelulu


People also ask

What is the difference between Vue and Vuex?

Vue is a progressive Javascript framework and Vuex is the state management tool. We can use redux or flux inside Vue, But Vuex is native to the Vue.

What are the reasons for using Vuex?

Vuex is a state management pattern + library for Vue.js applications. It serves as a centralized store for all the components in an application, with rules ensuring that the state can only be mutated in a predictable fashion.

Does Vuex work with Vue 2?

You are using Vuex 4 which works with Vue 3 only. For Vue 2, you must use Vuex 3.

Is Vuex store persistent?

vuex-persist. A Typescript-ready Vuex plugin that enables you to save the state of your app to a persisted storage like Cookies or localStorage.


1 Answers

Yes. You are absolutely right in what you are thinking. State containers like Redux, Vuex are supposed to hold your data constructs and not functions. It is true that functions in JavaScript are simply objects which are callable. You can store static data on functions too. But that still doesn't qualify as pure data. It is also for this same reason that we don't put Symbols in our state containers.

Coming back to the ES classes, as long as you are using classes as POJO i.e. only to store data then you are free to use those. But why have classes if you can have simple plain objects.

Separating data from UI components and moving it into state containers has fundamental roots in functional programming. Most of the strict functional languages like Haskell, Elm, OCaml or even Elixir/Erlang work this way. This provides strong reasoning about your data flows in your application. Additionally, this is complemented by the fact that, in these languages, data is immutable. And, thus there is no place for stateful Class like construct.

With JavaScript since things are inherently mutable, the boundaries are a bit blur and it is hard to define good practices.

Finally, as a community, there is no definite consensus about using the functional way, but it seems that the community is heading towards more functional, stateless component approaches. Some of the great examples are:

  • Elm
  • ReasonML
  • Hybrids
  • swiss-element
  • Cycle.js

And now, even we have functional components in both Vue and React.

like image 193
Harshal Patil Avatar answered Oct 23 '22 19:10

Harshal Patil