Skip to content

Instantly share code, notes, and snippets.

@laiso
Created October 14, 2020 17:19
Show Gist options
  • Save laiso/d400ffaee58b2d0194e262fc7ead1306 to your computer and use it in GitHub Desktop.
Save laiso/d400ffaee58b2d0194e262fc7ead1306 to your computer and use it in GitHub Desktop.
useCounterの中身。injectしてるからVueインスタンス経由でアクセスしないといけない。アプリケーションコードは上流でprovide()してコンポーネント間で状態管理する #CodePiece
import { ref, inject, Ref } from 'vue';
export const CounterKey = Symbol()
export type Counter = {
count: Ref
increment(): void
}
export function useCounter(initialValue: number): Counter {
const count = inject(CounterKey, ref(initialValue));
return {
count,
increment(): void {
count.value +=1;
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment