Why Vue's mixin pattern falls short

A Vue component is just a JavaScript object holding the functionality it needs — data, methods, computed and similar options. When several components need the same functionality, those shared options can be pulled out into a separate module and attached to a consumer via the mixins config property. Vue then merges the component's own options with those of every mixin at runtime.

// MyMixin.js
export default {
  data: () => ({
    mySharedDataProperty: null
  }),
  methods: {
    mySharedMethod () { ... }
  }
}
// ConsumingComponent.js
import MyMixin from "./MyMixin.js";


export default {
  mixins: [MyMixin],
  data: () => ({
    myLocalDataProperty: null
  }),
  methods: {
    myLocalMethod () { ... }
  }
}

Dan Abramov's 2016 post "Mixins Considered Harmful" argued that reusing logic through mixins in React is an anti-pattern. The same criticisms carry over to Vue, and they are the reason the Composition API exists.

Name clashes and unpredictable merges

Because merging happens at runtime, overlapping option names are resolved by a merge strategy. By default — and the rules can be configured — local options win over mixin options. Lifecycle hooks of the same type are the exception: they are collected into an array and all run in sequence. This rarely throws, but tracking named properties across components and multiple mixins quickly becomes unwieldy, especially when third-party mixins arrive as npm packages carrying their own property names.

Hidden coupling

A mixin has no hierarchical link to the component using it, so the dependency flows both ways: a component may read a property the mixin defines, and a mixin may assume the component provides one (a validation mixin, for instance, expecting an input value in order to run its own validate method). Rename such a variable during a refactor and nothing flags it — not the component, not the linter — until it fails at runtime. With many mixins in play, figuring out which one a local property belongs to means searching them all by hand.

How the Composition API works

The alternative models a component's functionality — state, methods, computed values — as JavaScript variables returned from a new setup function. A "counter" component written the classic way translates directly into this form.

// Counter.vue
import { ref, computed } from "vue";


export default {
  setup() {
    const count = ref(0);
    const double = computed(() => count.value * 2)
    function increment() {
      count.value++;
    }
    return {
      count,
      double,
      increment
    }
  }
}

ref declares a reactive variable behaving much like a data property, and computed plays the equivalent role. A method such as increment carries no reactivity, so it can be a plain function. Reactive variables from ref must be objects to keep their reactivity while being passed around, which is why the value is read and written through the value sub-property. Everything defined is returned from setup; the two versions of the component are functionally identical.

The API ships as a core feature of Vue 3 and is usable in Vue 2 through the @vue/composition-api npm plugin.

Pulling logic into functions

The counter's logic can move into a module named useCounter — prefixing with "use" is the convention.

// useCounter.js
import { ref, computed } from "vue";


export default function () {
  const count = ref(0);
  const double = computed(() => count.value * 2)
  function increment() {
    count.value++;
  }
  return {
    count,
    double,
    increment
  }
}

Consuming it means importing the module and calling it as a function, then returning what it hands back from setup.

// MyComponent.js
import useCounter from "./useCounter.js";

export default {
  setup() {
    const { count, double, increment } = useCounter();
    return {
      count,
      double,
      increment
    }
  }
}

What this fixes

Collisions behave like ordinary variables

State and methods returned from a composition function are named explicitly, so a clash is resolved exactly as it would be for any JavaScript variable, even when several composition functions are mixed into one component.

export default {
  setup () {
    const { someVar1, someMethod1 } = useCompFunction1();
    const { someVar2, someMethod2 } = useCompFunction2();
    return {
      someVar1,
      someMethod1,
      someVar2,
      someMethod2
    }
  }
}

Dependencies become visible

A composition function can still rely on a local variable from the consuming component, but that variable has to be passed in explicitly — the coupling is now in the call signature rather than buried in runtime merging.

import useCompFunction from "./useCompFunction";


export default {
  setup () {
    // some local value the a composition function needs to use
    const myLocalVal = ref(0);


    // it must be explicitly passed as an argument
    const { ... } = useCompFunction(myLocalVal);
  }
}

Sharing code by merging objects looks harmless but makes functionality fragile and hard to reason about. The strongest point of the Composition API is that it lets Vue rely on safeguards native to JavaScript — passing arguments to functions and the module system — instead of inventing its own. That does not make it better in every case: for most work the classic API remains perfectly adequate, and it is when code reuse is on the table that the Composition API clearly wins.