Skip to content

Commit

Permalink
feat(hooks): add useMountedState hook
Browse files Browse the repository at this point in the history
  • Loading branch information
u3u committed Aug 6, 2019
1 parent f600ab8 commit 1e70d5e
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/__tests__/useMountedState.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import Vue from 'vue';
import { onMounted } from 'vue-function-api';
import useMountedState from '../useMountedState';
import renderHook from '../util/renderHook';

describe('useMountedState', () => {
it('should be defined', () => {
expect(useMountedState).toBeDefined();
});

it('should return true on mounted', () => {
renderHook(() => {
const isMounted = useMountedState();
expect(isMounted.value).toBe(false);

onMounted(async () => {
await Vue.nextTick();
expect(isMounted.value).toBe(true);
});
});
});
});
13 changes: 13 additions & 0 deletions src/useMountedState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Vue from 'vue';
import { value, onMounted } from 'vue-function-api';

export default function useMountedState() {
const isMounted = value(false);

onMounted(async () => {
await Vue.nextTick();
isMounted.value = true;
});

return isMounted;
}

0 comments on commit 1e70d5e

Please sign in to comment.