Question #140

Author: admin
tags: Vue  
useSomeComposable.js
import { ref } from 'vue';

export function useSomeComposable() {
  const counter = ref(1);

  function increaseCounter() {
    counter.value += 1;
  }

  return {
    counter,
    increaseCounter,
  };
}
SomeComponent1.vue and SomeComponent2.vue (both have the same code):
<template>
  <span>
    {{ counter }}
  </span>
</template>

<script setup>
import { useSomeComposable } from './useSomeComposable';

const { counter, increaseCounter } = useSomeComposable();
increaseCounter();
</script>
Parent component:
<template>
  <div>
    <SomeComponent1 />
    and
    <SomeComponent2 />
  </div>
</template>

<script setup>
import SomeComponent1 from '../components/SomeComponent1.vue';
import SomeComponent2 from '../components/SomeComponent2.vue';
</script>
What will be rendered?
1 and 1
1 and 2
2 and 1
2 and 2
2 and 3
3 and 3
counter is defined inside useSomeComposable function.
Each useSomeComposable() call creates new counter and new increaseCounter, so SomeComponent1 and SomeComponent2 have their own counter.
This is a classic closure.
Rate the difficulty of the question:
easyhard