maoberlehner/vuex-map-fields

Use Vue.set in updateField to support reactive array watching

geoidesic opened this issue · 1 comments

Why does it not use Vue.set here?

prev[key] = value;

It seems to me that if you were to replace

export function updateField(state, { path, value }) {
  path.split(/[.[\]]+/).reduce((prev, key, index, array) => {
    if (array.length === index + 1) {
      // eslint-disable-next-line no-param-reassign
      prev[key] = value;
    }

    return prev[key];
  }, state);
}

with

export function updateField(state, { path, value }) {
  Vue.set(state, path, value);
}

It would be simpler and more reactive (as setting arrays directly via indexes does not default getter watchers).

Just double checked Vue.set(state, 'some.nested.path', value); does not work. If it would, it would be a big improvement indeed!

As for the "more" reactive thing: either it is reactive or not. It would enable setting new properties on the fly instead of declaring them beforehand. I can see how for some use cases this would be a nice improvement but I'm not sure if it doesn't promote anti patterns.