link822 link823 link824 link825 link826 link827 link828 link829 link830 link831 link832 link833 link834 link835 link836 link837 link838 link839 link840 link841 link842 link843 link844 link845 link846 link847 link848 link849 link850 link851 link852 link853 link854 link855 link856 link857 link858 link859 link860 link861 link862 link863 link864 link865 link866 link867 link868 link869 link870 link871 link872 link873 link874 link875 link876 link877 link878 link879 link880 link881 link882 link883 link884 link885 link886 link887 link888 link889 link890 link891 link892 link893 link894 link895 link896 link897 link898 link899 link900 link901 link902 link903 link904 link905 link906 link907 link908 link909 link910 link911 link912 link913 link914 link915 link916 link917 link918 link919 link920 link921 link922 link923 link924 link925 link926 link927 link928 link929 link930 link931 link932 link933 link934 link935 link936 link937 link938 link939 link940 link941 link942 link943 link944 link945 link946 link947 link948 link949 link950 link951 link952 link953 link954 link955 link956 link957 link958

[Vue.js] Does Vue support reactivity on Map and Set data types? Subscribe to RSS

The docs for Vue.js mention the smart auto-change-tracking for plain Javascript objects:

When you pass a plain JavaScript object to a vue.js instance as its data option, Vue.js will walk through all of its properties and convert them to getter/setters using Object.defineProperty.

Since Javascript’s Map and Set datatypes are designed to be used with their in-built get/set methods, how can I get vue.js to track calls (and therefore, changes) to the internal state of Maps and Sets?

Solution :

Vue.js does not support reactivity on Map and Set data types (yet?).

The feature ticket has some discussion and this work around (by user “inca”):

Sets and Maps are not observable by Vue. In order to use those
either in v-for, or in computed properties, methods, watchers,
template expressions, etc. you need to create a serializable replica
of this structure and expose it to Vue. Here’s a naive example which
uses a simple counter for providing vue.js with information that Set is

data() {
mySetChangeTracker: 1,
mySet: new Set(),

computed: {
mySetAsList() {
// By using `mySetChangeTracker` we tell vue.js that this property depends on it,
// so it gets re-evaluated whenever `mySetChangeTracker` changes
return this.mySetChangeTracker && Array.from(this.mySet);

methods: {
add(item) {
// Trigger vue.js updates
this.mySetChangeTracker += 1;

This illustrates a kinda hacky but 100% working method for making
non-observable data reactive. Still, in real world cases I ended up
with serialized versions of Sets/Maps (e.g. you’d probably want to
store the modified versions of sets/maps in localstorage and thus
serialize them anyway), so no artificial counters/hacks were involved.

Solution 2:

As far as I know, Vue’s reactivity tracks assignations. If you perform an assignation on the set, it should track reactivity, for example:

methods: {
add(item) {
this.mySet = new Set(this.mySet.add(item));

This gives you a cleaner code, but with an obvious problem: performance.

Just pick the solution according to the needs :)